Skip to content

Commit

Permalink
crc/sha512: fix missing finalize part of sha512 hash
Browse files Browse the repository at this point in the history
The sha512 implementation was broken, make an attempt at actually
making it work...

Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
axboe committed Feb 17, 2025
1 parent 1ceaf63 commit 3ee5319
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
57 changes: 57 additions & 0 deletions crc/sha512.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions crc/sha512.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3ee5319

Please sign in to comment.