[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: sha1 alignment bug



On Wed, 25 Apr 2001, Randolph Chung wrote:

> In sha1.h, there are a few member variables defined as:
> 
> unsigned char Buffer[64];
> unsigned char State[5*4];
> unsigned char Count[2*4];
> 
> the code goes on to address these as uint32_t * 's... on 64-bit
> architectures this causes very bad unaligned accesses.

Alpha certainly works fine.

Someone is doing screwy alignment, you can see that all the starts of all
those buffers are aligned by 4 and they are at the start of the structure,
and structures are 'aligned by size'. Thus any casting is assured to be
aligned by size. 

uint32_t's are not permitted by 8 bytes long and are not permitted to
require 8 byte alignment. If you compiler/libc has these problems it's
broken somehow, and its likely if you run the SHA-1 tester in tests/ that
it will fail.

> i've fixed this in cvs by adding appropriate attribute settings to these
> buffers. i wonder if there are other ones like these in the code?

MD5 has the only other scheme like that.

If this is infact a problem (I assume you are getting traps) then making
a union with each and unsigned long will correct it in a portable way.

It is possible that the lack of anything but byte alignment members in the
structure is reducing the alignment constraints GCC is using...

Jason



Reply to: