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 size_t psk_len = nvme_auth_hmac_hash_len(hmac_id); 501 struct nvme_auth_hmac_ctx hmac; 502 u8 *psk; 503 int ret; 504 505 if (!c1 || !c2) 506 return -EINVAL; 507 508 ret = nvme_auth_hmac_init(&hmac, hmac_id, skey, skey_len); 509 if (ret) 510 return ret; 511 psk = kzalloc(psk_len, GFP_KERNEL); 512 if (!psk) { 513 memzero_explicit(&hmac, sizeof(hmac)); 514 return -ENOMEM; 515 } 516 nvme_auth_hmac_update(&hmac, c1, hash_len); 517 nvme_auth_hmac_update(&hmac, c2, hash_len); 518 nvme_auth_hmac_final(&hmac, psk); 519 *ret_psk = psk; 520 *ret_len = psk_len; 521 return 0; 522 } 523 EXPORT_SYMBOL_GPL(nvme_auth_generate_psk); 524 525 /** 526 * nvme_auth_generate_digest - Generate TLS PSK digest 527 * @hmac_id: Hash function identifier 528 * @psk: Generated input PSK 529 * @psk_len: Length of @psk 530 * @subsysnqn: NQN of the subsystem 531 * @hostnqn: NQN of the host 532 * @ret_digest: Pointer to the returned digest 533 * 534 * Generate a TLS PSK digest as specified in TP8018 Section 3.6.1.3: 535 * TLS PSK and PSK identity Derivation 536 * 537 * The PSK digest shall be computed by encoding in Base64 (refer to RFC 538 * 4648) the result of the application of the HMAC function using the hash 539 * function specified in item 4 above (ie the hash function of the cipher 540 * suite associated with the PSK identity) with the PSK as HMAC key to the 541 * concatenation of: 542 * - the NQN of the host (i.e., NQNh) not including the null terminator; 543 * - a space character; 544 * - the NQN of the NVM subsystem (i.e., NQNc) not including the null 545 * terminator; 546 * - a space character; and 547 * - the seventeen ASCII characters "NVMe-over-Fabrics" 548 * (i.e., <PSK digest> = Base64(HMAC(PSK, NQNh || " " || NQNc || " " || 549 * "NVMe-over-Fabrics"))). 550 * The length of the PSK digest depends on the hash function used to compute 551 * it as follows: 552 * - If the SHA-256 hash function is used, the resulting PSK digest is 44 553 * characters long; or 554 * - If the SHA-384 hash function is used, the resulting PSK digest is 64 555 * characters long. 556 * 557 * Returns 0 on success with a valid digest pointer in @ret_digest, or a 558 * negative error number on failure. 559 */ 560 int nvme_auth_generate_digest(u8 hmac_id, const u8 *psk, size_t psk_len, 561 const char *subsysnqn, const char *hostnqn, 562 char **ret_digest) 563 { 564 struct crypto_shash *tfm; 565 SHASH_DESC_ON_STACK(shash, tfm); 566 u8 *digest; 567 char *enc; 568 const char *hmac_name; 569 size_t digest_len, hmac_len; 570 int ret; 571 572 if (WARN_ON(!subsysnqn || !hostnqn)) 573 return -EINVAL; 574 575 hmac_name = nvme_auth_hmac_name(hmac_id); 576 if (!hmac_name) { 577 pr_warn("%s: invalid hash algorithm %d\n", 578 __func__, hmac_id); 579 return -EINVAL; 580 } 581 582 switch (nvme_auth_hmac_hash_len(hmac_id)) { 583 case 32: 584 hmac_len = 44; 585 break; 586 case 48: 587 hmac_len = 64; 588 break; 589 default: 590 pr_warn("%s: invalid hash algorithm '%s'\n", 591 __func__, hmac_name); 592 return -EINVAL; 593 } 594 595 enc = kzalloc(hmac_len + 1, GFP_KERNEL); 596 if (!enc) 597 return -ENOMEM; 598 599 tfm = crypto_alloc_shash(hmac_name, 0, 0); 600 if (IS_ERR(tfm)) { 601 ret = PTR_ERR(tfm); 602 goto out_free_enc; 603 } 604 605 digest_len = crypto_shash_digestsize(tfm); 606 digest = kzalloc(digest_len, GFP_KERNEL); 607 if (!digest) { 608 ret = -ENOMEM; 609 goto out_free_tfm; 610 } 611 612 shash->tfm = tfm; 613 ret = crypto_shash_setkey(tfm, psk, psk_len); 614 if (ret) 615 goto out_free_digest; 616 617 ret = crypto_shash_init(shash); 618 if (ret) 619 goto out_free_digest; 620 621 ret = crypto_shash_update(shash, hostnqn, strlen(hostnqn)); 622 if (ret) 623 goto out_free_digest; 624 625 ret = crypto_shash_update(shash, " ", 1); 626 if (ret) 627 goto out_free_digest; 628 629 ret = crypto_shash_update(shash, subsysnqn, strlen(subsysnqn)); 630 if (ret) 631 goto out_free_digest; 632 633 ret = crypto_shash_update(shash, " NVMe-over-Fabrics", 18); 634 if (ret) 635 goto out_free_digest; 636 637 ret = crypto_shash_final(shash, digest); 638 if (ret) 639 goto out_free_digest; 640 641 ret = base64_encode(digest, digest_len, enc, true, BASE64_STD); 642 if (ret < hmac_len) { 643 ret = -ENOKEY; 644 goto out_free_digest; 645 } 646 *ret_digest = enc; 647 ret = 0; 648 649 out_free_digest: 650 kfree_sensitive(digest); 651 out_free_tfm: 652 crypto_free_shash(tfm); 653 out_free_enc: 654 if (ret) 655 kfree_sensitive(enc); 656 657 return ret; 658 } 659 EXPORT_SYMBOL_GPL(nvme_auth_generate_digest); 660 661 /** 662 * hkdf_expand_label - HKDF-Expand-Label (RFC 8846 section 7.1) 663 * @hmac_tfm: hash context keyed with pseudorandom key 664 * @label: ASCII label without "tls13 " prefix 665 * @labellen: length of @label 666 * @context: context bytes 667 * @contextlen: length of @context 668 * @okm: output keying material 669 * @okmlen: length of @okm 670 * 671 * Build the TLS 1.3 HkdfLabel structure and invoke hkdf_expand(). 672 * 673 * Returns 0 on success with output keying material stored in @okm, 674 * or a negative errno value otherwise. 675 */ 676 static int hkdf_expand_label(struct crypto_shash *hmac_tfm, 677 const u8 *label, unsigned int labellen, 678 const u8 *context, unsigned int contextlen, 679 u8 *okm, unsigned int okmlen) 680 { 681 int err; 682 u8 *info; 683 unsigned int infolen; 684 const char *tls13_prefix = "tls13 "; 685 unsigned int prefixlen = strlen(tls13_prefix); 686 687 if (WARN_ON(labellen > (255 - prefixlen))) 688 return -EINVAL; 689 if (WARN_ON(contextlen > 255)) 690 return -EINVAL; 691 692 infolen = 2 + (1 + prefixlen + labellen) + (1 + contextlen); 693 info = kzalloc(infolen, GFP_KERNEL); 694 if (!info) 695 return -ENOMEM; 696 697 /* HkdfLabel.Length */ 698 put_unaligned_be16(okmlen, info); 699 700 /* HkdfLabel.Label */ 701 info[2] = prefixlen + labellen; 702 memcpy(info + 3, tls13_prefix, prefixlen); 703 memcpy(info + 3 + prefixlen, label, labellen); 704 705 /* HkdfLabel.Context */ 706 info[3 + prefixlen + labellen] = contextlen; 707 memcpy(info + 4 + prefixlen + labellen, context, contextlen); 708 709 err = hkdf_expand(hmac_tfm, info, infolen, okm, okmlen); 710 kfree_sensitive(info); 711 return err; 712 } 713 714 /** 715 * nvme_auth_derive_tls_psk - Derive TLS PSK 716 * @hmac_id: Hash function identifier 717 * @psk: generated input PSK 718 * @psk_len: size of @psk 719 * @psk_digest: TLS PSK digest 720 * @ret_psk: Pointer to the resulting TLS PSK 721 * 722 * Derive a TLS PSK as specified in TP8018 Section 3.6.1.3: 723 * TLS PSK and PSK identity Derivation 724 * 725 * The TLS PSK shall be derived as follows from an input PSK 726 * (i.e., either a retained PSK or a generated PSK) and a PSK 727 * identity using the HKDF-Extract and HKDF-Expand-Label operations 728 * (refer to RFC 5869 and RFC 8446) where the hash function is the 729 * one specified by the hash specifier of the PSK identity: 730 * 1. PRK = HKDF-Extract(0, Input PSK); and 731 * 2. TLS PSK = HKDF-Expand-Label(PRK, "nvme-tls-psk", PskIdentityContext, L), 732 * where PskIdentityContext is the hash identifier indicated in 733 * the PSK identity concatenated to a space character and to the 734 * Base64 PSK digest (i.e., "<hash> <PSK digest>") and L is the 735 * output size in bytes of the hash function (i.e., 32 for SHA-256 736 * and 48 for SHA-384). 737 * 738 * Returns 0 on success with a valid psk pointer in @ret_psk or a negative 739 * error number otherwise. 740 */ 741 int nvme_auth_derive_tls_psk(int hmac_id, const u8 *psk, size_t psk_len, 742 const char *psk_digest, u8 **ret_psk) 743 { 744 struct crypto_shash *hmac_tfm; 745 const char *hmac_name; 746 const char *label = "nvme-tls-psk"; 747 static const u8 default_salt[NVME_AUTH_MAX_DIGEST_SIZE]; 748 size_t prk_len; 749 const char *ctx; 750 u8 *prk, *tls_key; 751 int ret; 752 753 hmac_name = nvme_auth_hmac_name(hmac_id); 754 if (!hmac_name) { 755 pr_warn("%s: invalid hash algorithm %d\n", 756 __func__, hmac_id); 757 return -EINVAL; 758 } 759 if (hmac_id == NVME_AUTH_HASH_SHA512) { 760 pr_warn("%s: unsupported hash algorithm %s\n", 761 __func__, hmac_name); 762 return -EINVAL; 763 } 764 765 if (psk_len != nvme_auth_hmac_hash_len(hmac_id)) { 766 pr_warn("%s: unexpected psk_len %zu\n", __func__, psk_len); 767 return -EINVAL; 768 } 769 770 hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0); 771 if (IS_ERR(hmac_tfm)) 772 return PTR_ERR(hmac_tfm); 773 774 prk_len = crypto_shash_digestsize(hmac_tfm); 775 prk = kzalloc(prk_len, GFP_KERNEL); 776 if (!prk) { 777 ret = -ENOMEM; 778 goto out_free_shash; 779 } 780 781 if (WARN_ON(prk_len > NVME_AUTH_MAX_DIGEST_SIZE)) { 782 ret = -EINVAL; 783 goto out_free_prk; 784 } 785 ret = hkdf_extract(hmac_tfm, psk, psk_len, 786 default_salt, prk_len, prk); 787 if (ret) 788 goto out_free_prk; 789 790 ret = crypto_shash_setkey(hmac_tfm, prk, prk_len); 791 if (ret) 792 goto out_free_prk; 793 794 ctx = kasprintf(GFP_KERNEL, "%02d %s", hmac_id, psk_digest); 795 if (!ctx) { 796 ret = -ENOMEM; 797 goto out_free_prk; 798 } 799 800 tls_key = kzalloc(psk_len, GFP_KERNEL); 801 if (!tls_key) { 802 ret = -ENOMEM; 803 goto out_free_ctx; 804 } 805 ret = hkdf_expand_label(hmac_tfm, 806 label, strlen(label), 807 ctx, strlen(ctx), 808 tls_key, psk_len); 809 if (ret) { 810 kfree(tls_key); 811 goto out_free_ctx; 812 } 813 *ret_psk = tls_key; 814 815 out_free_ctx: 816 kfree(ctx); 817 out_free_prk: 818 kfree(prk); 819 out_free_shash: 820 crypto_free_shash(hmac_tfm); 821 822 return ret; 823 } 824 EXPORT_SYMBOL_GPL(nvme_auth_derive_tls_psk); 825 826 MODULE_DESCRIPTION("NVMe Authentication framework"); 827 MODULE_LICENSE("GPL v2"); 828