Skip to content

Commit

Permalink
WIP: get rid of UB
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Dmytruk <[email protected]>
  • Loading branch information
SergiiDmytruk committed Apr 17, 2024
1 parent 9db046c commit 8ef5b89
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
32 changes: 16 additions & 16 deletions sha1sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ static inline u32 rol( u32 x, int n)
typedef struct {
u32 count;
u32 h[5];
union {
unsigned char buf[SHA1_BLOCK_SIZE];
struct {
unsigned char pad[SHA1_BLOCK_SIZE - sizeof(u64)];
/* Must be volatile, otherwise compiler over-optimizes it away. */
volatile u64 ml;
};
};
u8 buf[64];
} SHA1_CONTEXT;

static void sha1_init( SHA1_CONTEXT *hd )
Expand Down Expand Up @@ -74,7 +67,7 @@ static u32 sha1_blend(u32 *x, unsigned int i)
*/
static void sha1_transform(SHA1_CONTEXT *hd, const void *_data)
{
const u32 *data = _data;
const u8 *data = _data;
u32 a,b,c,d,e;
u32 x[16];
int i;
Expand All @@ -87,7 +80,11 @@ static void sha1_transform(SHA1_CONTEXT *hd, const void *_data)
e = hd->h[4];

for ( i = 0; i < 16; ++i )
x[i] = cpu_to_be32(data[i]);
{
u32 tmp;
memcpy(&tmp, &data[i*sizeof(u32)], sizeof(u32));
x[i] = be32_to_cpu(tmp);
}


#define K1 0x5A827999L
Expand Down Expand Up @@ -185,23 +182,26 @@ sha1_final(SHA1_CONTEXT *hd, u8 hash[SHA1_DIGEST_SIZE])
/* Start padding */
hd->buf[partial++] = 0x80;

if ( partial > sizeof(hd->pad) )
if ( partial > 56 )
{
/* Need one extra block - pad to 64 */
memset(hd->buf + partial, 0, sizeof(hd->buf) - partial);
memset(hd->buf + partial, 0, 64 - partial);
sha1_transform(hd, hd->buf);
partial = 0;
}
/* Pad to 56 */
memset(hd->pad + partial, 0, sizeof(hd->pad) - partial);
memset(hd->buf + partial, 0, 56 - partial);

/* append the 64 bit count */
hd->ml = cpu_to_be64((u64)hd->count << 3);
u64 ml = cpu_to_be64((u64)hd->count << 3);
memcpy(hd->buf + 56, &ml, sizeof(ml));
sha1_transform(hd, hd->buf);

u32 *p = (void *)hash;
for ( int i = 0; i < 5; ++i )
p[i] = be32_to_cpu(hd->h[i]);
{
u32 be = cpu_to_be32(hd->h[i]);
memcpy(&hash[i*sizeof(u32)], &be, sizeof(u32));
}
}

void sha1sum(u8 hash[static SHA1_DIGEST_SIZE], const void *ptr, u32 len)
Expand Down
31 changes: 16 additions & 15 deletions sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@
struct sha256_state {
u32 state[SHA256_DIGEST_SIZE / 4];
u32 count;
union {
u8 buf[SHA256_BLOCK_SIZE];
struct {
u8 pad[SHA256_BLOCK_SIZE - sizeof(u64)];
/* Must be volatile, otherwise compiler over-optimizes it away. */
volatile u64 ml;
};
};
u8 buf[SHA256_BLOCK_SIZE];
};

static inline u32 ror32(u32 word, unsigned int shift)
Expand Down Expand Up @@ -85,14 +78,18 @@ static const u32 K[] = {

static void sha256_transform(u32 *state, const void *_input)
{
const u32 *input = _input;
const u8 *input = _input;
u32 a, b, c, d, e, f, g, h, t1, t2;
u32 W[16];
int i;

/* load the input */
for ( i = 0; i < 16; i++ )
W[i] = be32_to_cpu(input[i]);
{
u32 tmp;
memcpy(&tmp, &input[i*sizeof(u32)], sizeof(u32));
W[i] = be32_to_cpu(tmp);
}

/* load the state into our registers */
a = state[0]; b = state[1]; c = state[2]; d = state[3];
Expand Down Expand Up @@ -171,29 +168,33 @@ static void sha256_once(struct sha256_state *sctx, const void *data, u32 len)

static void sha256_final(struct sha256_state *sctx, void *_dst)
{
u32 *dst = _dst;
u8 *dst = _dst;
unsigned int i, partial = sctx->count & 0x3f;

/* Start padding */
sctx->buf[partial++] = 0x80;

if ( partial > sizeof(sctx->pad) )
if ( partial > 56 )
{
/* Need one extra block - pad to 64 */
memset(sctx->buf + partial, 0, sizeof(sctx->buf) - partial);
sha256_transform(sctx->state, sctx->buf);
partial = 0;
}
/* Pad to 56 */
memset(sctx->pad + partial, 0, sizeof(sctx->pad) - partial);
memset(sctx->buf + partial, 0, 56 - partial);

/* Append the 64 bit count */
sctx->ml = cpu_to_be64((u64)sctx->count << 3);
u64 ml = cpu_to_be64((u64)sctx->count << 3);
memcpy(sctx->buf + 56, &ml, sizeof(ml));
sha256_transform(sctx->state, sctx->buf);

/* Store state in digest */
for ( i = 0; i < 8; i++ )
dst[i] = cpu_to_be32(sctx->state[i]);
{
u32 be = cpu_to_be32(sctx->state[i]);
memcpy(&dst[i*sizeof(u32)], &be, sizeof(u32));
}
}

void sha256sum(u8 hash[static SHA256_DIGEST_SIZE], const void *data, u32 len)
Expand Down

0 comments on commit 8ef5b89

Please sign in to comment.