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 nvme_auth_hmac_ctx hmac; 565 u8 digest[NVME_AUTH_MAX_DIGEST_SIZE]; 566 size_t hash_len = nvme_auth_hmac_hash_len(hmac_id); 567 char *enc; 568 size_t enc_len; 569 int ret; 570 571 if (WARN_ON(!subsysnqn || !hostnqn)) 572 return -EINVAL; 573 574 if (hash_len == 0) { 575 pr_warn("%s: invalid hash algorithm %d\n", 576 __func__, hmac_id); 577 return -EINVAL; 578 } 579 580 switch (hash_len) { 581 case 32: 582 enc_len = 44; 583 break; 584 case 48: 585 enc_len = 64; 586 break; 587 default: 588 pr_warn("%s: invalid hash algorithm '%s'\n", 589 __func__, nvme_auth_hmac_name(hmac_id)); 590 return -EINVAL; 591 } 592 593 enc = kzalloc(enc_len + 1, GFP_KERNEL); 594 if (!enc) { 595 ret = -ENOMEM; 596 goto out; 597 } 598 599 ret = nvme_auth_hmac_init(&hmac, hmac_id, psk, psk_len); 600 if (ret) 601 goto out; 602 nvme_auth_hmac_update(&hmac, hostnqn, strlen(hostnqn)); 603 nvme_auth_hmac_update(&hmac, " ", 1); 604 nvme_auth_hmac_update(&hmac, subsysnqn, strlen(subsysnqn)); 605 nvme_auth_hmac_update(&hmac, " NVMe-over-Fabrics", 18); 606 nvme_auth_hmac_final(&hmac, digest); 607 608 ret = base64_encode(digest, hash_len, enc, true, BASE64_STD); 609 if (ret < enc_len) { 610 ret = -ENOKEY; 611 goto out; 612 } 613 *ret_digest = enc; 614 ret = 0; 615 616 out: 617 if (ret) 618 kfree_sensitive(enc); 619 memzero_explicit(digest, sizeof(digest)); 620 return ret; 621 } 622 EXPORT_SYMBOL_GPL(nvme_auth_generate_digest); 623 624 /** 625 * hkdf_expand_label - HKDF-Expand-Label (RFC 8846 section 7.1) 626 * @hmac_tfm: hash context keyed with pseudorandom key 627 * @label: ASCII label without "tls13 " prefix 628 * @labellen: length of @label 629 * @context: context bytes 630 * @contextlen: length of @context 631 * @okm: output keying material 632 * @okmlen: length of @okm 633 * 634 * Build the TLS 1.3 HkdfLabel structure and invoke hkdf_expand(). 635 * 636 * Returns 0 on success with output keying material stored in @okm, 637 * or a negative errno value otherwise. 638 */ 639 static int hkdf_expand_label(struct crypto_shash *hmac_tfm, 640 const u8 *label, unsigned int labellen, 641 const u8 *context, unsigned int contextlen, 642 u8 *okm, unsigned int okmlen) 643 { 644 int err; 645 u8 *info; 646 unsigned int infolen; 647 const char *tls13_prefix = "tls13 "; 648 unsigned int prefixlen = strlen(tls13_prefix); 649 650 if (WARN_ON(labellen > (255 - prefixlen))) 651 return -EINVAL; 652 if (WARN_ON(contextlen > 255)) 653 return -EINVAL; 654 655 infolen = 2 + (1 + prefixlen + labellen) + (1 + contextlen); 656 info = kzalloc(infolen, GFP_KERNEL); 657 if (!info) 658 return -ENOMEM; 659 660 /* HkdfLabel.Length */ 661 put_unaligned_be16(okmlen, info); 662 663 /* HkdfLabel.Label */ 664 info[2] = prefixlen + labellen; 665 memcpy(info + 3, tls13_prefix, prefixlen); 666 memcpy(info + 3 + prefixlen, label, labellen); 667 668 /* HkdfLabel.Context */ 669 info[3 + prefixlen + labellen] = contextlen; 670 memcpy(info + 4 + prefixlen + labellen, context, contextlen); 671 672 err = hkdf_expand(hmac_tfm, info, infolen, okm, okmlen); 673 kfree_sensitive(info); 674 return err; 675 } 676 677 /** 678 * nvme_auth_derive_tls_psk - Derive TLS PSK 679 * @hmac_id: Hash function identifier 680 * @psk: generated input PSK 681 * @psk_len: size of @psk 682 * @psk_digest: TLS PSK digest 683 * @ret_psk: Pointer to the resulting TLS PSK 684 * 685 * Derive a TLS PSK as specified in TP8018 Section 3.6.1.3: 686 * TLS PSK and PSK identity Derivation 687 * 688 * The TLS PSK shall be derived as follows from an input PSK 689 * (i.e., either a retained PSK or a generated PSK) and a PSK 690 * identity using the HKDF-Extract and HKDF-Expand-Label operations 691 * (refer to RFC 5869 and RFC 8446) where the hash function is the 692 * one specified by the hash specifier of the PSK identity: 693 * 1. PRK = HKDF-Extract(0, Input PSK); and 694 * 2. TLS PSK = HKDF-Expand-Label(PRK, "nvme-tls-psk", PskIdentityContext, L), 695 * where PskIdentityContext is the hash identifier indicated in 696 * the PSK identity concatenated to a space character and to the 697 * Base64 PSK digest (i.e., "<hash> <PSK digest>") and L is the 698 * output size in bytes of the hash function (i.e., 32 for SHA-256 699 * and 48 for SHA-384). 700 * 701 * Returns 0 on success with a valid psk pointer in @ret_psk or a negative 702 * error number otherwise. 703 */ 704 int nvme_auth_derive_tls_psk(int hmac_id, const u8 *psk, size_t psk_len, 705 const char *psk_digest, u8 **ret_psk) 706 { 707 struct crypto_shash *hmac_tfm; 708 const char *hmac_name; 709 const char *label = "nvme-tls-psk"; 710 static const u8 default_salt[NVME_AUTH_MAX_DIGEST_SIZE]; 711 size_t prk_len; 712 const char *ctx; 713 u8 *prk, *tls_key; 714 int ret; 715 716 hmac_name = nvme_auth_hmac_name(hmac_id); 717 if (!hmac_name) { 718 pr_warn("%s: invalid hash algorithm %d\n", 719 __func__, hmac_id); 720 return -EINVAL; 721 } 722 if (hmac_id == NVME_AUTH_HASH_SHA512) { 723 pr_warn("%s: unsupported hash algorithm %s\n", 724 __func__, hmac_name); 725 return -EINVAL; 726 } 727 728 if (psk_len != nvme_auth_hmac_hash_len(hmac_id)) { 729 pr_warn("%s: unexpected psk_len %zu\n", __func__, psk_len); 730 return -EINVAL; 731 } 732 733 hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0); 734 if (IS_ERR(hmac_tfm)) 735 return PTR_ERR(hmac_tfm); 736 737 prk_len = crypto_shash_digestsize(hmac_tfm); 738 prk = kzalloc(prk_len, GFP_KERNEL); 739 if (!prk) { 740 ret = -ENOMEM; 741 goto out_free_shash; 742 } 743 744 if (WARN_ON(prk_len > NVME_AUTH_MAX_DIGEST_SIZE)) { 745 ret = -EINVAL; 746 goto out_free_prk; 747 } 748 ret = hkdf_extract(hmac_tfm, psk, psk_len, 749 default_salt, prk_len, prk); 750 if (ret) 751 goto out_free_prk; 752 753 ret = crypto_shash_setkey(hmac_tfm, prk, prk_len); 754 if (ret) 755 goto out_free_prk; 756 757 ctx = kasprintf(GFP_KERNEL, "%02d %s", hmac_id, psk_digest); 758 if (!ctx) { 759 ret = -ENOMEM; 760 goto out_free_prk; 761 } 762 763 tls_key = kzalloc(psk_len, GFP_KERNEL); 764 if (!tls_key) { 765 ret = -ENOMEM; 766 goto out_free_ctx; 767 } 768 ret = hkdf_expand_label(hmac_tfm, 769 label, strlen(label), 770 ctx, strlen(ctx), 771 tls_key, psk_len); 772 if (ret) { 773 kfree(tls_key); 774 goto out_free_ctx; 775 } 776 *ret_psk = tls_key; 777 778 out_free_ctx: 779 kfree(ctx); 780 out_free_prk: 781 kfree(prk); 782 out_free_shash: 783 crypto_free_shash(hmac_tfm); 784 785 return ret; 786 } 787 EXPORT_SYMBOL_GPL(nvme_auth_derive_tls_psk); 788 789 MODULE_DESCRIPTION("NVMe Authentication framework"); 790 MODULE_LICENSE("GPL v2"); 791