Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

nvme-auth: target: use crypto library in nvmet_auth_host_hash()

For the HMAC computation in nvmet_auth_host_hash(), use the crypto
library instead of crypto_shash. This is simpler, faster, and more
reliable. Notably, this eliminates the crypto transformation object
allocation for every call, which was very slow.

Acked-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Keith Busch <kbusch@kernel.org>

authored by

Eric Biggers and committed by
Keith Busch
e501533f efe8df9f

+28 -62
+28 -62
drivers/nvme/target/auth.c
··· 283 283 int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, 284 284 unsigned int shash_len) 285 285 { 286 - struct crypto_shash *shash_tfm; 287 - SHASH_DESC_ON_STACK(shash, shash_tfm); 286 + struct nvme_auth_hmac_ctx hmac; 288 287 struct nvmet_ctrl *ctrl = req->sq->ctrl; 289 - const char *hash_name; 290 288 u8 *challenge = req->sq->dhchap_c1; 291 289 struct nvme_dhchap_key *transformed_key; 292 290 u8 buf[4]; 293 291 int ret; 294 292 295 - hash_name = nvme_auth_hmac_name(ctrl->shash_id); 296 - if (!hash_name) { 297 - pr_warn("Hash ID %d invalid\n", ctrl->shash_id); 298 - return -EINVAL; 299 - } 300 - 301 - shash_tfm = crypto_alloc_shash(hash_name, 0, 0); 302 - if (IS_ERR(shash_tfm)) { 303 - pr_err("failed to allocate shash %s\n", hash_name); 304 - return PTR_ERR(shash_tfm); 305 - } 306 - 307 - if (shash_len != crypto_shash_digestsize(shash_tfm)) { 308 - pr_err("%s: hash len mismatch (len %d digest %d)\n", 309 - __func__, shash_len, 310 - crypto_shash_digestsize(shash_tfm)); 311 - ret = -EINVAL; 312 - goto out_free_tfm; 313 - } 314 - 315 293 transformed_key = nvme_auth_transform_key(ctrl->host_key, 316 294 ctrl->hostnqn); 317 - if (IS_ERR(transformed_key)) { 318 - ret = PTR_ERR(transformed_key); 319 - goto out_free_tfm; 320 - } 295 + if (IS_ERR(transformed_key)) 296 + return PTR_ERR(transformed_key); 321 297 322 - ret = crypto_shash_setkey(shash_tfm, transformed_key->key, 298 + ret = nvme_auth_hmac_init(&hmac, ctrl->shash_id, transformed_key->key, 323 299 transformed_key->len); 324 300 if (ret) 325 301 goto out_free_response; 302 + 303 + if (shash_len != nvme_auth_hmac_hash_len(ctrl->shash_id)) { 304 + pr_err("%s: hash len mismatch (len %u digest %zu)\n", __func__, 305 + shash_len, nvme_auth_hmac_hash_len(ctrl->shash_id)); 306 + ret = -EINVAL; 307 + goto out_free_response; 308 + } 326 309 327 310 if (ctrl->dh_gid != NVME_AUTH_DHGROUP_NULL) { 328 311 challenge = kmalloc(shash_len, GFP_KERNEL); ··· 319 336 req->sq->dhchap_c1, 320 337 challenge, shash_len); 321 338 if (ret) 322 - goto out; 339 + goto out_free_challenge; 323 340 } 324 341 325 342 pr_debug("ctrl %d qid %d host response seq %u transaction %d\n", 326 343 ctrl->cntlid, req->sq->qid, req->sq->dhchap_s1, 327 344 req->sq->dhchap_tid); 328 345 329 - shash->tfm = shash_tfm; 330 - ret = crypto_shash_init(shash); 331 - if (ret) 332 - goto out; 333 - ret = crypto_shash_update(shash, challenge, shash_len); 334 - if (ret) 335 - goto out; 346 + nvme_auth_hmac_update(&hmac, challenge, shash_len); 347 + 336 348 put_unaligned_le32(req->sq->dhchap_s1, buf); 337 - ret = crypto_shash_update(shash, buf, 4); 338 - if (ret) 339 - goto out; 349 + nvme_auth_hmac_update(&hmac, buf, 4); 350 + 340 351 put_unaligned_le16(req->sq->dhchap_tid, buf); 341 - ret = crypto_shash_update(shash, buf, 2); 342 - if (ret) 343 - goto out; 352 + nvme_auth_hmac_update(&hmac, buf, 2); 353 + 344 354 *buf = req->sq->sc_c; 345 - ret = crypto_shash_update(shash, buf, 1); 346 - if (ret) 347 - goto out; 348 - ret = crypto_shash_update(shash, "HostHost", 8); 349 - if (ret) 350 - goto out; 355 + nvme_auth_hmac_update(&hmac, buf, 1); 356 + nvme_auth_hmac_update(&hmac, "HostHost", 8); 351 357 memset(buf, 0, 4); 352 - ret = crypto_shash_update(shash, ctrl->hostnqn, strlen(ctrl->hostnqn)); 353 - if (ret) 354 - goto out; 355 - ret = crypto_shash_update(shash, buf, 1); 356 - if (ret) 357 - goto out; 358 - ret = crypto_shash_update(shash, ctrl->subsys->subsysnqn, 359 - strlen(ctrl->subsys->subsysnqn)); 360 - if (ret) 361 - goto out; 362 - ret = crypto_shash_final(shash, response); 363 - out: 358 + nvme_auth_hmac_update(&hmac, ctrl->hostnqn, strlen(ctrl->hostnqn)); 359 + nvme_auth_hmac_update(&hmac, buf, 1); 360 + nvme_auth_hmac_update(&hmac, ctrl->subsys->subsysnqn, 361 + strlen(ctrl->subsys->subsysnqn)); 362 + nvme_auth_hmac_final(&hmac, response); 363 + ret = 0; 364 + out_free_challenge: 364 365 if (challenge != req->sq->dhchap_c1) 365 366 kfree(challenge); 366 367 out_free_response: 368 + memzero_explicit(&hmac, sizeof(hmac)); 367 369 nvme_auth_free_key(transformed_key); 368 - out_free_tfm: 369 - crypto_free_shash(shash_tfm); 370 370 return ret; 371 371 } 372 372