crc/sha512: fix missing finalize part of sha512 hash
authorJens Axboe <axboe@kernel.dk>
Mon, 17 Feb 2025 23:30:07 +0000 (16:30 -0700)
committerJens Axboe <axboe@kernel.dk>
Mon, 17 Feb 2025 23:30:07 +0000 (16:30 -0700)
The sha512 implementation was broken, make an attempt at actually
making it work...

Signed-off-by: Jens Axboe <axboe@kernel.dk>
crc/sha512.c
crc/sha512.h
verify.c

index f599cdcc82061bbce70e68f83fde957fc40a741c..78e64ba2ef07a1982971a5af4c901bcfc72a38a9 100644 (file)
@@ -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;
+       }
+}
index 5adf6271cd4fbb0aa331a656e64bd653a2b3752f..dd26d8aa148924442c2ca7bd6db8d6a2704006c9 100644 (file)
@@ -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
index 4dbf8d56225dc1b3aa1dbde83bb5da0a21732929..570c888f01bb7e1d694f9c8640dc8e599b209184 100644 (file)
--- 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)