]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
SHA-1: safer endianity
authorPavel Tvrdík <pawel.tvrdik@gmail.cz>
Wed, 22 Apr 2015 15:42:04 +0000 (17:42 +0200)
committerPavel Tvrdík <pawel.tvrdik@gmail.cz>
Wed, 22 Apr 2015 15:45:14 +0000 (17:45 +0200)
lib/sha1.c
lib/unaligned.h

index cd73c52ee039831fa46db490106ad44015a201a8..f2b55f583577222fa95e0d9ee7dda1b47f33003c 100644 (file)
@@ -47,12 +47,12 @@ transform(sha1_context *hd, const byte *data)
   e = hd->h4;
 
 #ifdef CPU_BIG_ENDIAN
-  memcpy( x, data, 64 );
+  memcpy(x, data, 64);
 #else
   {
     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 */;
 
@@ -260,10 +260,10 @@ sha1_final(sha1_context *hd)
   hd->buf[61] = lsb >> 16;
   hd->buf[62] = lsb >>  8;
   hd->buf[63] = lsb       ;
-  transform( hd, hd->buf );
+  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);
index eea9eabe924eb4e430b1889c13f6dd57092d4c8f..e1d61aa08932b12465b29436dcfb68d938bf48f3 100644 (file)
@@ -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