From: Jens Axboe Date: Mon, 17 Feb 2025 23:30:07 +0000 (-0700) Subject: crc/sha512: fix missing finalize part of sha512 hash X-Git-Tag: fio-3.39~1 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=3ee531989f41b242b73d9f6cb33186a686a7133c;p=fio.git crc/sha512: fix missing finalize part of sha512 hash The sha512 implementation was broken, make an attempt at actually making it work... Signed-off-by: Jens Axboe --- diff --git a/crc/sha512.c b/crc/sha512.c index f599cdcc..78e64ba2 100644 --- a/crc/sha512.c +++ b/crc/sha512.c @@ -195,3 +195,60 @@ void fio_sha512_update(struct fio_sha512_ctx *sctx, const uint8_t *data, /* erase our data */ memset(sctx->W, 0, sizeof(sctx->W)); } + +void fio_sha512_final(struct fio_sha512_ctx *sctx) +{ + uint8_t *hash = sctx->buf; + static uint8_t padding[128] = { 0x80, }; + unsigned int index, pad_len; + uint8_t bits[128]; + uint64_t t2; + uint32_t t; + int i, j; + + index = pad_len = t = i = j = 0; + t2 = 0; + + /* Save number of bits */ + t = sctx->count[0]; + bits[15] = t; t>>=8; + bits[14] = t; t>>=8; + bits[13] = t; t>>=8; + bits[12] = t; + t = sctx->count[1]; + bits[11] = t; t>>=8; + bits[10] = t; t>>=8; + bits[9 ] = t; t>>=8; + bits[8 ] = t; + t = sctx->count[2]; + bits[7 ] = t; t>>=8; + bits[6 ] = t; t>>=8; + bits[5 ] = t; t>>=8; + bits[4 ] = t; + t = sctx->count[3]; + bits[3 ] = t; t>>=8; + bits[2 ] = t; t>>=8; + bits[1 ] = t; t>>=8; + bits[0 ] = t; + + /* Pad out to 112 mod 128. */ + index = (sctx->count[0] >> 3) & 0x7f; + pad_len = (index < 112) ? (112 - index) : ((128+112) - index); + fio_sha512_update(sctx, padding, pad_len); + + /* Append length (before padding) */ + fio_sha512_update(sctx, bits, 16); + + /* Store state in digest */ + for (i = j = 0; i < 8; i++, j += 8) { + t2 = sctx->state[i]; + hash[j+7] = (char)t2 & 0xff; t2>>=8; + hash[j+6] = (char)t2 & 0xff; t2>>=8; + hash[j+5] = (char)t2 & 0xff; t2>>=8; + hash[j+4] = (char)t2 & 0xff; t2>>=8; + hash[j+3] = (char)t2 & 0xff; t2>>=8; + hash[j+2] = (char)t2 & 0xff; t2>>=8; + hash[j+1] = (char)t2 & 0xff; t2>>=8; + hash[j ] = (char)t2 & 0xff; + } +} diff --git a/crc/sha512.h b/crc/sha512.h index 5adf6271..dd26d8aa 100644 --- a/crc/sha512.h +++ b/crc/sha512.h @@ -12,5 +12,6 @@ struct fio_sha512_ctx { void fio_sha512_init(struct fio_sha512_ctx *); void fio_sha512_update(struct fio_sha512_ctx *, const uint8_t *, unsigned int); +void fio_sha512_final(struct fio_sha512_ctx *sctx); #endif diff --git a/verify.c b/verify.c index 4dbf8d56..570c888f 100644 --- a/verify.c +++ b/verify.c @@ -537,6 +537,7 @@ static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc) fio_sha512_init(&sha512_ctx); fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr)); + fio_sha512_final(&sha512_ctx); if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) return 0; @@ -1092,6 +1093,7 @@ static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) fio_sha512_init(&sha512_ctx); fio_sha512_update(&sha512_ctx, p, len); + fio_sha512_final(&sha512_ctx); } static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)