Commit dd78aae5 authored by Pavel Tvrdík's avatar Pavel Tvrdík
Browse files

SHA-1: safer endianity

parent 57453a3c
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ transform(sha1_context *hd, const byte *data)
  {
    int i;
    for (i=0; i<16; i++)
      x[i] = get_u32_be(data+4*i);
      x[i] = htonl(*((u32*)(data+4*i)));
  }
#endif

@@ -219,7 +219,7 @@ byte *
sha1_final(sha1_context *hd)
{
  u32 t, msb, lsb;
  byte *p;
  u32 *p;

  sha1_update(hd, NULL, 0); /* flush */;

@@ -262,8 +262,8 @@ sha1_final(sha1_context *hd)
  hd->buf[63] = lsb	   ;
  transform(hd, hd->buf);

  p = hd->buf;
#define X(a) do { put_u32_be(p, hd->h##a); p += 4; } while(0)
  p = (u32*) hd->buf;
#define X(a) do { *(p++) = ntohl(hd->h##a); } while(0)
  X(0);
  X(1);
  X(2);
+0 −45
Original line number Diff line number Diff line
@@ -50,49 +50,4 @@ put_u32(void *p, u32 x)
  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