Thread: pgcrypto BYTE_ENDIAN fix

pgcrypto BYTE_ENDIAN fix

From
Marko Kreen
Date:
* When postgres.h does not define BYTE_ENDIAN pgcrypto
  produces garbage.

I learned the hard way that

    #if UNDEFINED_1 == UNDEFINED_2
    #error "gcc is idiot"
    #endif

prints "gcc is idiot" ...

Affected are MD5/SHA1 in internal library, and also HMAC-MD5/HMAC-SHA1/
crypt-md5 which use them.  Blowfish is ok, also Rijndael on at
least x86.

Big thanks to Daniel Holtzman who send me a build log which
contained warning:

    md5.c:246: warning: `X' defined but not used

Yes, gcc is that helpful...

Please apply this.

--
marko


Index: contrib/pgcrypto/blf.c
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/blf.c,v
retrieving revision 1.3
diff -u -r1.3 blf.c
--- contrib/pgcrypto/blf.c    5 Nov 2001 17:46:23 -0000    1.3
+++ contrib/pgcrypto/blf.c    29 Nov 2001 11:50:37 -0000
@@ -41,6 +41,8 @@
  */

 #include <postgres.h>
+#include "px.h"
+
 #include "blf.h"

 #undef inline
Index: contrib/pgcrypto/crypt-blowfish.c
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/crypt-blowfish.c,v
retrieving revision 1.6
diff -u -r1.6 crypt-blowfish.c
--- contrib/pgcrypto/crypt-blowfish.c    5 Nov 2001 17:46:23 -0000    1.6
+++ contrib/pgcrypto/crypt-blowfish.c    29 Nov 2001 11:50:10 -0000
@@ -32,7 +32,9 @@

 #include "postgres.h"

+#include "px.h"
 #include "px-crypt.h"
+
 #define __set_errno(v)

 #ifndef __set_errno
Index: contrib/pgcrypto/crypt-des.c
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/crypt-des.c,v
retrieving revision 1.7
diff -u -r1.7 crypt-des.c
--- contrib/pgcrypto/crypt-des.c    20 Nov 2001 21:10:38 -0000    1.7
+++ contrib/pgcrypto/crypt-des.c    29 Nov 2001 11:40:16 -0000
@@ -60,6 +60,7 @@

 #include "postgres.h"

+#include "px.h"
 #include "px-crypt.h"

 /* for ntohl/htonl */
Index: contrib/pgcrypto/crypt-gensalt.c
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/crypt-gensalt.c,v
retrieving revision 1.3
diff -u -r1.3 crypt-gensalt.c
--- contrib/pgcrypto/crypt-gensalt.c    25 Oct 2001 05:49:19 -0000    1.3
+++ contrib/pgcrypto/crypt-gensalt.c    29 Nov 2001 11:40:16 -0000
@@ -12,6 +12,7 @@

 #include "postgres.h"

+#include "px.h"
 #include "px-crypt.h"

 #include <errno.h>
Index: contrib/pgcrypto/md5.c
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/md5.c,v
retrieving revision 1.8
diff -u -r1.8 md5.c
--- contrib/pgcrypto/md5.c    25 Oct 2001 05:49:19 -0000    1.8
+++ contrib/pgcrypto/md5.c    29 Nov 2001 11:40:53 -0000
@@ -31,6 +31,7 @@
  */

 #include "postgres.h"
+#include "px.h"

 #include "md5.h"

Index: contrib/pgcrypto/px.h
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/px.h,v
retrieving revision 1.5
diff -u -r1.5 px.h
--- contrib/pgcrypto/px.h    20 Nov 2001 15:50:53 -0000    1.5
+++ contrib/pgcrypto/px.h    29 Nov 2001 11:41:41 -0000
@@ -32,6 +32,15 @@
 #ifndef __PX_H
 #define __PX_H

+#ifdef HAVE_ENDIAN_H
+#include <endian.h>
+#endif
+
+#ifndef BYTE_ORDER
+#error BYTE_ORDER must be defined as LITTLE_ENDIAN or BIG_ENDIAN
+#endif
+
+
 #if 1

 #define px_alloc(s) palloc(s)
Index: contrib/pgcrypto/rijndael.c
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/rijndael.c,v
retrieving revision 1.7
diff -u -r1.7 rijndael.c
--- contrib/pgcrypto/rijndael.c    20 Nov 2001 15:50:53 -0000    1.7
+++ contrib/pgcrypto/rijndael.c    29 Nov 2001 11:49:13 -0000
@@ -39,6 +39,7 @@
 */

 #include <postgres.h>
+#include "px.h"

 #include "rijndael.h"

Index: contrib/pgcrypto/sha1.c
===================================================================
RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/sha1.c,v
retrieving revision 1.10
diff -u -r1.10 sha1.c
--- contrib/pgcrypto/sha1.c    5 Nov 2001 17:46:23 -0000    1.10
+++ contrib/pgcrypto/sha1.c    29 Nov 2001 11:42:03 -0000
@@ -36,6 +36,7 @@
  */

 #include "postgres.h"
+#include "px.h"

 #include "sha1.h"


Re: pgcrypto BYTE_ENDIAN fix

From
Bruce Momjian
Date:
Patch applied.  Thanks.

---------------------------------------------------------------------------


>
> * When postgres.h does not define BYTE_ENDIAN pgcrypto
>   produces garbage.
>
> I learned the hard way that
>
>     #if UNDEFINED_1 == UNDEFINED_2
>     #error "gcc is idiot"
>     #endif
>
> prints "gcc is idiot" ...
>
> Affected are MD5/SHA1 in internal library, and also HMAC-MD5/HMAC-SHA1/
> crypt-md5 which use them.  Blowfish is ok, also Rijndael on at
> least x86.
>
> Big thanks to Daniel Holtzman who send me a build log which
> contained warning:
>
>     md5.c:246: warning: `X' defined but not used
>
> Yes, gcc is that helpful...
>
> Please apply this.
>
> --
> marko
>
>
> Index: contrib/pgcrypto/blf.c
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/blf.c,v
> retrieving revision 1.3
> diff -u -r1.3 blf.c
> --- contrib/pgcrypto/blf.c    5 Nov 2001 17:46:23 -0000    1.3
> +++ contrib/pgcrypto/blf.c    29 Nov 2001 11:50:37 -0000
> @@ -41,6 +41,8 @@
>   */
>
>  #include <postgres.h>
> +#include "px.h"
> +
>  #include "blf.h"
>
>  #undef inline
> Index: contrib/pgcrypto/crypt-blowfish.c
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/crypt-blowfish.c,v
> retrieving revision 1.6
> diff -u -r1.6 crypt-blowfish.c
> --- contrib/pgcrypto/crypt-blowfish.c    5 Nov 2001 17:46:23 -0000    1.6
> +++ contrib/pgcrypto/crypt-blowfish.c    29 Nov 2001 11:50:10 -0000
> @@ -32,7 +32,9 @@
>
>  #include "postgres.h"
>
> +#include "px.h"
>  #include "px-crypt.h"
> +
>  #define __set_errno(v)
>
>  #ifndef __set_errno
> Index: contrib/pgcrypto/crypt-des.c
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/crypt-des.c,v
> retrieving revision 1.7
> diff -u -r1.7 crypt-des.c
> --- contrib/pgcrypto/crypt-des.c    20 Nov 2001 21:10:38 -0000    1.7
> +++ contrib/pgcrypto/crypt-des.c    29 Nov 2001 11:40:16 -0000
> @@ -60,6 +60,7 @@
>
>  #include "postgres.h"
>
> +#include "px.h"
>  #include "px-crypt.h"
>
>  /* for ntohl/htonl */
> Index: contrib/pgcrypto/crypt-gensalt.c
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/crypt-gensalt.c,v
> retrieving revision 1.3
> diff -u -r1.3 crypt-gensalt.c
> --- contrib/pgcrypto/crypt-gensalt.c    25 Oct 2001 05:49:19 -0000    1.3
> +++ contrib/pgcrypto/crypt-gensalt.c    29 Nov 2001 11:40:16 -0000
> @@ -12,6 +12,7 @@
>
>  #include "postgres.h"
>
> +#include "px.h"
>  #include "px-crypt.h"
>
>  #include <errno.h>
> Index: contrib/pgcrypto/md5.c
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/md5.c,v
> retrieving revision 1.8
> diff -u -r1.8 md5.c
> --- contrib/pgcrypto/md5.c    25 Oct 2001 05:49:19 -0000    1.8
> +++ contrib/pgcrypto/md5.c    29 Nov 2001 11:40:53 -0000
> @@ -31,6 +31,7 @@
>   */
>
>  #include "postgres.h"
> +#include "px.h"
>
>  #include "md5.h"
>
> Index: contrib/pgcrypto/px.h
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/px.h,v
> retrieving revision 1.5
> diff -u -r1.5 px.h
> --- contrib/pgcrypto/px.h    20 Nov 2001 15:50:53 -0000    1.5
> +++ contrib/pgcrypto/px.h    29 Nov 2001 11:41:41 -0000
> @@ -32,6 +32,15 @@
>  #ifndef __PX_H
>  #define __PX_H
>
> +#ifdef HAVE_ENDIAN_H
> +#include <endian.h>
> +#endif
> +
> +#ifndef BYTE_ORDER
> +#error BYTE_ORDER must be defined as LITTLE_ENDIAN or BIG_ENDIAN
> +#endif
> +
> +
>  #if 1
>
>  #define px_alloc(s) palloc(s)
> Index: contrib/pgcrypto/rijndael.c
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/rijndael.c,v
> retrieving revision 1.7
> diff -u -r1.7 rijndael.c
> --- contrib/pgcrypto/rijndael.c    20 Nov 2001 15:50:53 -0000    1.7
> +++ contrib/pgcrypto/rijndael.c    29 Nov 2001 11:49:13 -0000
> @@ -39,6 +39,7 @@
>  */
>
>  #include <postgres.h>
> +#include "px.h"
>
>  #include "rijndael.h"
>
> Index: contrib/pgcrypto/sha1.c
> ===================================================================
> RCS file: /opt/cvs/pgsql/pgsql/contrib/pgcrypto/sha1.c,v
> retrieving revision 1.10
> diff -u -r1.10 sha1.c
> --- contrib/pgcrypto/sha1.c    5 Nov 2001 17:46:23 -0000    1.10
> +++ contrib/pgcrypto/sha1.c    29 Nov 2001 11:42:03 -0000
> @@ -36,6 +36,7 @@
>   */
>
>  #include "postgres.h"
> +#include "px.h"
>
>  #include "sha1.h"
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026