Skip to content

Commit 070bb43

Browse files
ebiggersMike Snitzer
authored andcommitted
dm integrity: use crypto_shash_digest() in sb_mac()
Simplify sb_mac() by using crypto_shash_digest() instead of an init+update+final sequence. This should also improve performance. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Mike Snitzer <snitzer@kernel.org>
1 parent 6d0ee3b commit 070bb43

1 file changed

Lines changed: 10 additions & 20 deletions

File tree

drivers/md/dm-integrity.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -493,42 +493,32 @@ static int sb_mac(struct dm_integrity_c *ic, bool wr)
493493
{
494494
SHASH_DESC_ON_STACK(desc, ic->journal_mac);
495495
int r;
496-
unsigned int size = crypto_shash_digestsize(ic->journal_mac);
496+
unsigned int mac_size = crypto_shash_digestsize(ic->journal_mac);
497+
__u8 *sb = (__u8 *)ic->sb;
498+
__u8 *mac = sb + (1 << SECTOR_SHIFT) - mac_size;
497499

498-
if (sizeof(struct superblock) + size > 1 << SECTOR_SHIFT) {
500+
if (sizeof(struct superblock) + mac_size > 1 << SECTOR_SHIFT) {
499501
dm_integrity_io_error(ic, "digest is too long", -EINVAL);
500502
return -EINVAL;
501503
}
502504

503505
desc->tfm = ic->journal_mac;
504506

505-
r = crypto_shash_init(desc);
506-
if (unlikely(r < 0)) {
507-
dm_integrity_io_error(ic, "crypto_shash_init", r);
508-
return r;
509-
}
510-
511-
r = crypto_shash_update(desc, (__u8 *)ic->sb, (1 << SECTOR_SHIFT) - size);
512-
if (unlikely(r < 0)) {
513-
dm_integrity_io_error(ic, "crypto_shash_update", r);
514-
return r;
515-
}
516-
517507
if (likely(wr)) {
518-
r = crypto_shash_final(desc, (__u8 *)ic->sb + (1 << SECTOR_SHIFT) - size);
508+
r = crypto_shash_digest(desc, sb, mac - sb, mac);
519509
if (unlikely(r < 0)) {
520-
dm_integrity_io_error(ic, "crypto_shash_final", r);
510+
dm_integrity_io_error(ic, "crypto_shash_digest", r);
521511
return r;
522512
}
523513
} else {
524-
__u8 result[HASH_MAX_DIGESTSIZE];
514+
__u8 actual_mac[HASH_MAX_DIGESTSIZE];
525515

526-
r = crypto_shash_final(desc, result);
516+
r = crypto_shash_digest(desc, sb, mac - sb, actual_mac);
527517
if (unlikely(r < 0)) {
528-
dm_integrity_io_error(ic, "crypto_shash_final", r);
518+
dm_integrity_io_error(ic, "crypto_shash_digest", r);
529519
return r;
530520
}
531-
if (memcmp((__u8 *)ic->sb + (1 << SECTOR_SHIFT) - size, result, size)) {
521+
if (memcmp(mac, actual_mac, mac_size)) {
532522
dm_integrity_io_error(ic, "superblock mac", -EILSEQ);
533523
dm_audit_log_target(DM_MSG_PREFIX, "mac-superblock", ic->ti, 0);
534524
return -EILSEQ;

0 commit comments

Comments
 (0)