0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 18:21:54 +00:00

SHA-1: safer endianity

This commit is contained in:
Pavel Tvrdík 2015-04-22 17:42:04 +02:00
parent 57453a3c5f
commit dd78aae55c
2 changed files with 6 additions and 51 deletions

View File

@ -47,12 +47,12 @@ transform(sha1_context *hd, const byte *data)
e = hd->h4; e = hd->h4;
#ifdef CPU_BIG_ENDIAN #ifdef CPU_BIG_ENDIAN
memcpy( x, data, 64 ); memcpy(x, data, 64);
#else #else
{ {
int i; int i;
for (i=0; i<16; i++) for (i=0; i<16; i++)
x[i] = get_u32_be(data+4*i); x[i] = htonl(*((u32*)(data+4*i)));
} }
#endif #endif
@ -219,7 +219,7 @@ byte *
sha1_final(sha1_context *hd) sha1_final(sha1_context *hd)
{ {
u32 t, msb, lsb; u32 t, msb, lsb;
byte *p; u32 *p;
sha1_update(hd, NULL, 0); /* flush */; sha1_update(hd, NULL, 0); /* flush */;
@ -260,10 +260,10 @@ sha1_final(sha1_context *hd)
hd->buf[61] = lsb >> 16; hd->buf[61] = lsb >> 16;
hd->buf[62] = lsb >> 8; hd->buf[62] = lsb >> 8;
hd->buf[63] = lsb ; hd->buf[63] = lsb ;
transform( hd, hd->buf ); transform(hd, hd->buf);
p = hd->buf; p = (u32*) hd->buf;
#define X(a) do { put_u32_be(p, hd->h##a); p += 4; } while(0) #define X(a) do { *(p++) = ntohl(hd->h##a); } while(0)
X(0); X(0);
X(1); X(1);
X(2); X(2);

View File

@ -50,49 +50,4 @@ put_u32(void *p, u32 x)
memcpy(p, &x, 4); memcpy(p, &x, 4);
} }
/* Big endian format */
#if defined(CPU_BIG_ENDIAN)
static inline uint get_u16_be(const void *p) { return *(u16 *)p; } /** Read 16-bit integer value from an unaligned sequence of 2 bytes (big-endian version). **/
static inline u32 get_u32_be(const void *p) { return *(u32 *)p; } /** Read 32-bit integer value from an unaligned sequence of 4 bytes (big-endian version). **/
static inline u64 get_u64_be(const void *p) { return *(u64 *)p; } /** Read 64-bit integer value from an unaligned sequence of 8 bytes (big-endian version). **/
static inline void put_u16_be(void *p, uint x) { *(u16 *)p = x; } /** Write 16-bit integer value to an unaligned sequence of 2 bytes (big-endian version). **/
static inline void put_u32_be(void *p, u32 x) { *(u32 *)p = x; } /** Write 32-bit integer value to an unaligned sequence of 4 bytes (big-endian version). **/
static inline void put_u64_be(void *p, u64 x) { *(u64 *)p = x; } /** Write 64-bit integer value to an unaligned sequence of 8 bytes (big-endian version). **/
#else
static inline uint get_u16_be(const void *p)
{
const byte *c = (const byte *)p;
return (c[0] << 8) | c[1];
}
static inline u32 get_u32_be(const void *p)
{
const byte *c = (const byte *)p;
return (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
}
static inline u64 get_u64_be(const void *p)
{
return ((u64) get_u32_be(p) << 32) | get_u32_be((const byte *)p+4);
}
static inline void put_u16_be(void *p, uint x)
{
byte *c = (byte *)p;
c[0] = x >> 8;
c[1] = x;
}
static inline void put_u32_be(void *p, u32 x)
{
byte *c = (byte *)p;
c[0] = x >> 24;
c[1] = x >> 16;
c[2] = x >> 8;
c[3] = x;
}
static inline void put_u64_be(void *p, u64 x)
{
put_u32_be(p, x >> 32);
put_u32_be((byte *)p+4, x);
}
#endif
#endif #endif