Skip to content

Commit 3ee5319

Browse files
committed
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 <[email protected]>
1 parent 1ceaf63 commit 3ee5319

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

crc/sha512.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,60 @@ void fio_sha512_update(struct fio_sha512_ctx *sctx, const uint8_t *data,
195195
/* erase our data */
196196
memset(sctx->W, 0, sizeof(sctx->W));
197197
}
198+
199+
void fio_sha512_final(struct fio_sha512_ctx *sctx)
200+
{
201+
uint8_t *hash = sctx->buf;
202+
static uint8_t padding[128] = { 0x80, };
203+
unsigned int index, pad_len;
204+
uint8_t bits[128];
205+
uint64_t t2;
206+
uint32_t t;
207+
int i, j;
208+
209+
index = pad_len = t = i = j = 0;
210+
t2 = 0;
211+
212+
/* Save number of bits */
213+
t = sctx->count[0];
214+
bits[15] = t; t>>=8;
215+
bits[14] = t; t>>=8;
216+
bits[13] = t; t>>=8;
217+
bits[12] = t;
218+
t = sctx->count[1];
219+
bits[11] = t; t>>=8;
220+
bits[10] = t; t>>=8;
221+
bits[9 ] = t; t>>=8;
222+
bits[8 ] = t;
223+
t = sctx->count[2];
224+
bits[7 ] = t; t>>=8;
225+
bits[6 ] = t; t>>=8;
226+
bits[5 ] = t; t>>=8;
227+
bits[4 ] = t;
228+
t = sctx->count[3];
229+
bits[3 ] = t; t>>=8;
230+
bits[2 ] = t; t>>=8;
231+
bits[1 ] = t; t>>=8;
232+
bits[0 ] = t;
233+
234+
/* Pad out to 112 mod 128. */
235+
index = (sctx->count[0] >> 3) & 0x7f;
236+
pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
237+
fio_sha512_update(sctx, padding, pad_len);
238+
239+
/* Append length (before padding) */
240+
fio_sha512_update(sctx, bits, 16);
241+
242+
/* Store state in digest */
243+
for (i = j = 0; i < 8; i++, j += 8) {
244+
t2 = sctx->state[i];
245+
hash[j+7] = (char)t2 & 0xff; t2>>=8;
246+
hash[j+6] = (char)t2 & 0xff; t2>>=8;
247+
hash[j+5] = (char)t2 & 0xff; t2>>=8;
248+
hash[j+4] = (char)t2 & 0xff; t2>>=8;
249+
hash[j+3] = (char)t2 & 0xff; t2>>=8;
250+
hash[j+2] = (char)t2 & 0xff; t2>>=8;
251+
hash[j+1] = (char)t2 & 0xff; t2>>=8;
252+
hash[j ] = (char)t2 & 0xff;
253+
}
254+
}

crc/sha512.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ struct fio_sha512_ctx {
1212

1313
void fio_sha512_init(struct fio_sha512_ctx *);
1414
void fio_sha512_update(struct fio_sha512_ctx *, const uint8_t *, unsigned int);
15+
void fio_sha512_final(struct fio_sha512_ctx *sctx);
1516

1617
#endif

verify.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
537537

538538
fio_sha512_init(&sha512_ctx);
539539
fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
540+
fio_sha512_final(&sha512_ctx);
540541

541542
if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
542543
return 0;
@@ -1092,6 +1093,7 @@ static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
10921093

10931094
fio_sha512_init(&sha512_ctx);
10941095
fio_sha512_update(&sha512_ctx, p, len);
1096+
fio_sha512_final(&sha512_ctx);
10951097
}
10961098

10971099
static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)

0 commit comments

Comments
 (0)