introduce-private-md5-sha1.patch
authorAndy Green <andy@warmcat.com>
Sun, 23 Jan 2011 17:47:08 +0000 (17:47 +0000)
committerAndy Green <andy@warmcat.com>
Sun, 23 Jan 2011 17:47:08 +0000 (17:47 +0000)
Signed-off-by: Andy Green <andy@warmcat.com>
configure
configure.ac
lib/Makefile.am
lib/Makefile.in
lib/md5.c [new file with mode: 0644]
lib/sha-1.c [new file with mode: 0644]
libwebsockets-api-doc.html

index 123de5b..3e0ce28 100755 (executable)
--- a/configure
+++ b/configure
@@ -616,6 +616,8 @@ ac_subst_vars='am__EXEEXT_FALSE
 am__EXEEXT_TRUE
 LTLIBOBJS
 LIBOBJS
+LIBCRYPTO_FALSE
+LIBCRYPTO_TRUE
 CPP
 OTOOL64
 OTOOL
@@ -736,6 +738,7 @@ with_sysroot
 enable_libtool_lock
 enable_openssl
 enable_nofork
+enable_libcrypto
 '
       ac_precious_vars='build_alias
 host_alias
@@ -1380,6 +1383,7 @@ Optional Features:
   --disable-libtool-lock  avoid locking (might break parallel builds)
   --enable-openssl  Enables https support and needs openssl libs
   --enable-nofork  Disables fork-related options
+  --enable-libcrypto  Use libcrypto MD5 and SHA1 implemntations
 
 Optional Packages:
   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
@@ -11959,7 +11963,9 @@ fi
 
 
 
-
+#
+#
+#
 # Check whether --enable-openssl was given.
 if test "${enable_openssl+set}" = set; then :
   enableval=$enable_openssl;  openssl=yes
@@ -12016,6 +12022,9 @@ fi
 CFLAGS="$CFLAGS -DLWS_OPENSSL_SUPPORT"
 fi
 
+#
+#
+#
 # Check whether --enable-nofork was given.
 if test "${enable_nofork+set}" = set; then :
   enableval=$enable_nofork;  nofork=yes
 
 fi
 
+#
+#
+#
+# Check whether --enable-libcrypto was given.
+if test "${enable_libcrypto+set}" = set; then :
+  enableval=$enable_libcrypto;  libcrypto=yes
+
+fi
+
+
+if test "x$libcrypto" = "xyes" ; then
+CFLAGS="$CFLAGS -DLWS_LIBCRYPTO"
+LDFLAGS="$LDFLAGS -lcrypto"
+fi
+ if test x$libcrypto = xyes; then
+  LIBCRYPTO_TRUE=
+  LIBCRYPTO_FALSE='#'
+else
+  LIBCRYPTO_TRUE='#'
+  LIBCRYPTO_FALSE=
+fi
+
+
 
 
 # Checks for header files.
@@ -12563,6 +12595,10 @@ if test -z "${am__fastdepCC_TRUE}" && test -z "${am__fastdepCC_FALSE}"; then
   as_fn_error $? "conditional \"am__fastdepCC\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
 fi
+if test -z "${LIBCRYPTO_TRUE}" && test -z "${LIBCRYPTO_FALSE}"; then
+  as_fn_error $? "conditional \"LIBCRYPTO\" was never defined.
+Usually this means the macro was only invoked conditionally." "$LINENO" 5
+fi
 
 : "${CONFIG_STATUS=./config.status}"
 ac_write_fail=0
index 2ccdd68..019b3f9 100644 (file)
@@ -17,7 +17,9 @@ AC_PROG_INSTALL
 AC_PROG_MAKE_SET
 AC_CONFIG_MACRO_DIR([m4])
 
-
+#
+#
+#
 AC_ARG_ENABLE(openssl,
  [  --enable-openssl  Enables https support and needs openssl libs],
  [ openssl=yes
@@ -28,6 +30,9 @@ AC_CHECK_LIB([ssl], [SSL_library_init])
 CFLAGS="$CFLAGS -DLWS_OPENSSL_SUPPORT"
 fi
 
+#
+#
+#
 AC_ARG_ENABLE(nofork,
  [  --enable-nofork  Disables fork-related options],
  [ nofork=yes
@@ -39,6 +44,20 @@ else
 AC_FUNC_FORK
 fi
 
+#
+#
+#
+AC_ARG_ENABLE(libcrypto,
+ [  --enable-libcrypto  Use libcrypto MD5 and SHA1 implemntations],
+ [ libcrypto=yes
+ ])
+
+if test "x$libcrypto" = "xyes" ; then
+CFLAGS="$CFLAGS -DLWS_LIBCRYPTO"
+LDFLAGS="$LDFLAGS -lcrypto"
+fi
+AM_CONDITIONAL(LIBCRYPTO, test x$libcrypto = xyes)
+
 
 
 # Checks for header files.
index fb0f239..2280bdc 100644 (file)
@@ -7,8 +7,13 @@ dist_libwebsockets_la_SOURCES=libwebsockets.c \
                                base64-decode.c \
                                client-handshake.c \
                                private-libwebsockets.h
+if LIBCRYPTO
+else
+dist_libwebsockets_la_SOURCES += md5.c sha-1.c
+endif
+
 libwebsockets_la_CFLAGS=-Wall -Werror -std=gnu99 -pedantic -rdynamic -fPIC -c
-libwebsockets_la_LDFLAGS=-version-info 0:2 -lcrypto
+libwebsockets_la_LDFLAGS=-version-info 0:2
 
 all-local:
         ../scripts/kernel-doc -html \
index 62dc216..9f58996 100644 (file)
@@ -35,6 +35,7 @@ PRE_UNINSTALL = :
 POST_UNINSTALL = :
 build_triplet = @build@
 host_triplet = @host@
+@LIBCRYPTO_FALSE@am__append_1 = md5.c sha-1.c
 subdir = lib
 DIST_COMMON = $(include_HEADERS) $(srcdir)/Makefile.am \
        $(srcdir)/Makefile.in
@@ -70,10 +71,15 @@ am__base_list = \
 am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)"
 LTLIBRARIES = $(lib_LTLIBRARIES)
 libwebsockets_la_LIBADD =
+am__dist_libwebsockets_la_SOURCES_DIST = libwebsockets.c handshake.c \
+       parsers.c libwebsockets.h base64-decode.c client-handshake.c \
+       private-libwebsockets.h md5.c sha-1.c
+@LIBCRYPTO_FALSE@am__objects_1 = libwebsockets_la-md5.lo \
+@LIBCRYPTO_FALSE@      libwebsockets_la-sha-1.lo
 dist_libwebsockets_la_OBJECTS = libwebsockets_la-libwebsockets.lo \
        libwebsockets_la-handshake.lo libwebsockets_la-parsers.lo \
        libwebsockets_la-base64-decode.lo \
-       libwebsockets_la-client-handshake.lo
+       libwebsockets_la-client-handshake.lo $(am__objects_1)
 libwebsockets_la_OBJECTS = $(dist_libwebsockets_la_OBJECTS)
 libwebsockets_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
        $(LIBTOOLFLAGS) --mode=link $(CCLD) $(libwebsockets_la_CFLAGS) \
@@ -92,7 +98,7 @@ LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
        --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
        $(LDFLAGS) -o $@
 SOURCES = $(dist_libwebsockets_la_SOURCES)
-DIST_SOURCES = $(dist_libwebsockets_la_SOURCES)
+DIST_SOURCES = $(am__dist_libwebsockets_la_SOURCES_DIST)
 HEADERS = $(include_HEADERS)
 ETAGS = etags
 CTAGS = ctags
@@ -212,16 +218,11 @@ top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
 lib_LTLIBRARIES = libwebsockets.la
 include_HEADERS = libwebsockets.h
-dist_libwebsockets_la_SOURCES = libwebsockets.c \
-                               handshake.c \
-                               parsers.c \
-                               libwebsockets.h \
-                               base64-decode.c \
-                               client-handshake.c \
-                               private-libwebsockets.h
-
+dist_libwebsockets_la_SOURCES = libwebsockets.c handshake.c parsers.c \
+       libwebsockets.h base64-decode.c client-handshake.c \
+       private-libwebsockets.h $(am__append_1)
 libwebsockets_la_CFLAGS = -Wall -Werror -std=gnu99 -pedantic -rdynamic -fPIC -c
-libwebsockets_la_LDFLAGS = -version-info 0:2 -lcrypto
+libwebsockets_la_LDFLAGS = -version-info 0:2
 all: all-am
 
 .SUFFIXES:
@@ -300,7 +301,9 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libwebsockets_la-client-handshake.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libwebsockets_la-handshake.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libwebsockets_la-libwebsockets.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libwebsockets_la-md5.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libwebsockets_la-parsers.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libwebsockets_la-sha-1.Plo@am__quote@
 
 .c.o:
 @am__fastdepCC_TRUE@   $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
@@ -358,6 +361,20 @@ libwebsockets_la-client-handshake.lo: client-handshake.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libwebsockets_la_CFLAGS) $(CFLAGS) -c -o libwebsockets_la-client-handshake.lo `test -f 'client-handshake.c' || echo '$(srcdir)/'`client-handshake.c
 
+libwebsockets_la-md5.lo: md5.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libwebsockets_la_CFLAGS) $(CFLAGS) -MT libwebsockets_la-md5.lo -MD -MP -MF $(DEPDIR)/libwebsockets_la-md5.Tpo -c -o libwebsockets_la-md5.lo `test -f 'md5.c' || echo '$(srcdir)/'`md5.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libwebsockets_la-md5.Tpo $(DEPDIR)/libwebsockets_la-md5.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='md5.c' object='libwebsockets_la-md5.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libwebsockets_la_CFLAGS) $(CFLAGS) -c -o libwebsockets_la-md5.lo `test -f 'md5.c' || echo '$(srcdir)/'`md5.c
+
+libwebsockets_la-sha-1.lo: sha-1.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libwebsockets_la_CFLAGS) $(CFLAGS) -MT libwebsockets_la-sha-1.lo -MD -MP -MF $(DEPDIR)/libwebsockets_la-sha-1.Tpo -c -o libwebsockets_la-sha-1.lo `test -f 'sha-1.c' || echo '$(srcdir)/'`sha-1.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libwebsockets_la-sha-1.Tpo $(DEPDIR)/libwebsockets_la-sha-1.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='sha-1.c' object='libwebsockets_la-sha-1.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libwebsockets_la_CFLAGS) $(CFLAGS) -c -o libwebsockets_la-sha-1.lo `test -f 'sha-1.c' || echo '$(srcdir)/'`sha-1.c
+
 mostlyclean-libtool:
        -rm -f *.lo
 
diff --git a/lib/md5.c b/lib/md5.c
new file mode 100644 (file)
index 0000000..042db3b
--- /dev/null
+++ b/lib/md5.c
@@ -0,0 +1,217 @@
+/*
+ * Modified from Polarssl here
+ * http://polarssl.org/show_source?file=md5
+ * under GPL2 or later
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+
+#define GET_ULONG_LE(n, b, i)                        \
+{                                                    \
+       (n) = ((unsigned long)(b)[i])                \
+       | ((unsigned long)(b)[(i) + 1] <<  8)        \
+       | ((unsigned long)(b)[(i) + 2] << 16)        \
+       | ((unsigned long)(b)[(i) + 3] << 24);       \
+}
+
+#define PUT_ULONG_LE(n, b, i)                       \
+{                                                   \
+       (b)[i] = (unsigned char)(n);                 \
+       (b)[(i) + 1] = (unsigned char)((n) >>  8);   \
+       (b)[(i) + 2] = (unsigned char)((n) >> 16);   \
+       (b)[(i) + 3] = (unsigned char)((n) >> 24);   \
+}
+
+static const unsigned char md5_padding[64] = {
+       0x80, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0,
+};
+
+static const unsigned long state_init[] = {
+       0, 0, 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476
+};
+
+static void
+md5_process(unsigned long *state, const unsigned char *data)
+{
+       unsigned long X[16], A, B, C, D;
+       int v;
+
+       for (v = 0; v < 16; v++)
+               GET_ULONG_LE(X[v], data, v << 2);
+
+#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
+
+#define P(a, b, c, d, k, s, t) { a += F(b, c, d) + X[k] + t; a = S(a, s) + b; }
+
+       A = state[0];
+       B = state[1];
+       C = state[2];
+       D = state[3];
+
+#define F(x, y, z) (z ^ (x & (y ^ z)))
+
+       P(A, B, C, D,  0,  7, 0xD76AA478);
+       P(D, A, B, C,  1, 12, 0xE8C7B756);
+       P(C, D, A, B,  2, 17, 0x242070DB);
+       P(B, C, D, A,  3, 22, 0xC1BDCEEE);
+       P(A, B, C, D,  4,  7, 0xF57C0FAF);
+       P(D, A, B, C,  5, 12, 0x4787C62A);
+       P(C, D, A, B,  6, 17, 0xA8304613);
+       P(B, C, D, A,  7, 22, 0xFD469501);
+       P(A, B, C, D,  8,  7, 0x698098D8);
+       P(D, A, B, C,  9, 12, 0x8B44F7AF);
+       P(C, D, A, B, 10, 17, 0xFFFF5BB1);
+       P(B, C, D, A, 11, 22, 0x895CD7BE);
+       P(A, B, C, D, 12,  7, 0x6B901122);
+       P(D, A, B, C, 13, 12, 0xFD987193);
+       P(C, D, A, B, 14, 17, 0xA679438E);
+       P(B, C, D, A, 15, 22, 0x49B40821);
+
+#undef F
+
+#define F(x, y, z) (y ^ (z & (x ^ y)))
+
+       P(A, B, C, D,  1,  5, 0xF61E2562);
+       P(D, A, B, C,  6,  9, 0xC040B340);
+       P(C, D, A, B, 11, 14, 0x265E5A51);
+       P(B, C, D, A,  0, 20, 0xE9B6C7AA);
+       P(A, B, C, D,  5,  5, 0xD62F105D);
+       P(D, A, B, C, 10,  9, 0x02441453);
+       P(C, D, A, B, 15, 14, 0xD8A1E681);
+       P(B, C, D, A,  4, 20, 0xE7D3FBC8);
+       P(A, B, C, D,  9,  5, 0x21E1CDE6);
+       P(D, A, B, C, 14,  9, 0xC33707D6);
+       P(C, D, A, B,  3, 14, 0xF4D50D87);
+       P(B, C, D, A,  8, 20, 0x455A14ED);
+       P(A, B, C, D, 13,  5, 0xA9E3E905);
+       P(D, A, B, C,  2,  9, 0xFCEFA3F8);
+       P(C, D, A, B,  7, 14, 0x676F02D9);
+       P(B, C, D, A, 12, 20, 0x8D2A4C8A);
+
+#undef F
+
+#define F(x, y, z) (x ^ y ^ z)
+
+       P(A, B, C, D,  5,  4, 0xFFFA3942);
+       P(D, A, B, C,  8, 11, 0x8771F681);
+       P(C, D, A, B, 11, 16, 0x6D9D6122);
+       P(B, C, D, A, 14, 23, 0xFDE5380C);
+       P(A, B, C, D,  1,  4, 0xA4BEEA44);
+       P(D, A, B, C,  4, 11, 0x4BDECFA9);
+       P(C, D, A, B,  7, 16, 0xF6BB4B60);
+       P(B, C, D, A, 10, 23, 0xBEBFBC70);
+       P(A, B, C, D, 13,  4, 0x289B7EC6);
+       P(D, A, B, C,  0, 11, 0xEAA127FA);
+       P(C, D, A, B,  3, 16, 0xD4EF3085);
+       P(B, C, D, A,  6, 23, 0x04881D05);
+       P(A, B, C, D,  9,  4, 0xD9D4D039);
+       P(D, A, B, C, 12, 11, 0xE6DB99E5);
+       P(C, D, A, B, 15, 16, 0x1FA27CF8);
+       P(B, C, D, A,  2, 23, 0xC4AC5665);
+
+#undef F
+
+#define F(x, y, z) (y ^ (x | ~z))
+
+       P(A, B, C, D,  0,  6, 0xF4292244);
+       P(D, A, B, C,  7, 10, 0x432AFF97);
+       P(C, D, A, B, 14, 15, 0xAB9423A7);
+       P(B, C, D, A,  5, 21, 0xFC93A039);
+       P(A, B, C, D, 12,  6, 0x655B59C3);
+       P(D, A, B, C,  3, 10, 0x8F0CCC92);
+       P(C, D, A, B, 10, 15, 0xFFEFF47D);
+       P(B, C, D, A,  1, 21, 0x85845DD1);
+       P(A, B, C, D,  8,  6, 0x6FA87E4F);
+       P(D, A, B, C, 15, 10, 0xFE2CE6E0);
+       P(C, D, A, B,  6, 15, 0xA3014314);
+       P(B, C, D, A, 13, 21, 0x4E0811A1);
+       P(A, B, C, D,  4,  6, 0xF7537E82);
+       P(D, A, B, C, 11, 10, 0xBD3AF235);
+       P(C, D, A, B,  2, 15, 0x2AD7D2BB);
+       P(B, C, D, A,  9, 21, 0xEB86D391);
+
+#undef F
+
+       state[0] += A;
+       state[1] += B;
+       state[2] += C;
+       state[3] += D;
+}
+
+static
+void md5_update(unsigned long *state, unsigned char *buffer,
+                                          const unsigned char *input, int ilen)
+{
+       int fill;
+       unsigned long left;
+
+       if (ilen <= 0)
+               return;
+
+       left = state[0] & 0x3F;
+       fill = 64 - left;
+
+       state[0] += ilen;
+       state[0] &= 0xFFFFFFFF;
+
+       if (state[0] < (unsigned long)ilen)
+               state[1]++;
+
+       if (left && ilen >= fill) {
+               memcpy(buffer + left, input, fill);
+               md5_process(&state[2], buffer);
+               input += fill;
+               ilen -= fill;
+               left = 0;
+       }
+
+       while (ilen >= 64) {
+               md5_process(&state[2], input);
+               input += 64;
+               ilen -= 64;
+       }
+
+       if (ilen > 0)
+               memcpy(buffer + left, input, ilen);
+}
+
+void
+MD5(const unsigned char *input, int ilen, unsigned char *output)
+{
+       unsigned long last, padn;
+       unsigned long high, low;
+       unsigned char msglen[8];
+       unsigned long state[6];
+       unsigned char buffer[64];
+
+       memcpy(&state[0], &state_init[0], sizeof(state_init));
+
+       md5_update(state, buffer, input, ilen);
+
+       high = (state[0] >> 29) | (state[1] <<  3);
+       low  = state[0] <<  3;
+
+       PUT_ULONG_LE(low, msglen, 0);
+       PUT_ULONG_LE(high, msglen, 4);
+
+       last = state[0] & 0x3F;
+       padn = (last < 56) ? (56 - last) : (120 - last);
+
+       md5_update(state, buffer, md5_padding, padn);
+       md5_update(state, buffer, msglen, 8);
+
+       PUT_ULONG_LE(state[2], output, 0);
+       PUT_ULONG_LE(state[3], output, 4);
+       PUT_ULONG_LE(state[4], output, 8);
+       PUT_ULONG_LE(state[5], output, 12);
+}
+
diff --git a/lib/sha-1.c b/lib/sha-1.c
new file mode 100644 (file)
index 0000000..1bec395
--- /dev/null
@@ -0,0 +1,295 @@
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+/*
+ * FIPS pub 180-1: Secure Hash Algorithm (SHA-1)
+ * based on: http://csrc.nist.gov/fips/fip180-1.txt
+ * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
+ */
+
+#include <sys/types.h>
+#include <sys/cdefs.h>
+#include <sys/time.h>
+#include <string.h>
+
+struct sha1_ctxt {
+       union {
+               u_int8_t        b8[20];
+               u_int32_t       b32[5];
+       } h;
+       union {
+               u_int8_t        b8[8];
+               u_int64_t       b64[1];
+       } c;
+       union {
+               u_int8_t        b8[64];
+               u_int32_t       b32[16];
+       } m;
+       u_int8_t        count;
+};
+
+/* sanity check */
+#if BYTE_ORDER != BIG_ENDIAN
+# if BYTE_ORDER != LITTLE_ENDIAN
+#  define unsupported 1
+# endif
+#endif
+
+#ifndef unsupported
+
+/* constant table */
+static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
+#define        K(t)    _K[(t) / 20]
+
+#define        F0(b, c, d)     (((b) & (c)) | ((~(b)) & (d)))
+#define        F1(b, c, d)     (((b) ^ (c)) ^ (d))
+#define        F2(b, c, d)     (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
+#define        F3(b, c, d)     (((b) ^ (c)) ^ (d))
+
+#define        S(n, x)         (((x) << (n)) | ((x) >> (32 - n)))
+
+#define        H(n)    (ctxt->h.b32[(n)])
+#define        COUNT   (ctxt->count)
+#define        BCOUNT  (ctxt->c.b64[0] / 8)
+#define        W(n)    (ctxt->m.b32[(n)])
+
+#define        PUTBYTE(x)      { \
+       ctxt->m.b8[(COUNT % 64)] = (x);         \
+       COUNT++;                                \
+       COUNT %= 64;                            \
+       ctxt->c.b64[0] += 8;                    \
+       if (COUNT % 64 == 0)                    \
+               sha1_step(ctxt);                \
+     }
+
+#define        PUTPAD(x)       { \
+       ctxt->m.b8[(COUNT % 64)] = (x);         \
+       COUNT++;                                \
+       COUNT %= 64;                            \
+       if (COUNT % 64 == 0)                    \
+               sha1_step(ctxt);                \
+     }
+
+static void sha1_step __P((struct sha1_ctxt *));
+
+static void
+sha1_step(struct sha1_ctxt *ctxt)
+{
+       u_int32_t       a, b, c, d, e;
+       size_t t, s;
+       u_int32_t       tmp;
+
+#if BYTE_ORDER == LITTLE_ENDIAN
+       struct sha1_ctxt tctxt;
+
+       memcpy(&tctxt.m.b8[0], &ctxt->m.b8[0], 64);
+       ctxt->m.b8[0] = tctxt.m.b8[3]; ctxt->m.b8[1] = tctxt.m.b8[2];
+       ctxt->m.b8[2] = tctxt.m.b8[1]; ctxt->m.b8[3] = tctxt.m.b8[0];
+       ctxt->m.b8[4] = tctxt.m.b8[7]; ctxt->m.b8[5] = tctxt.m.b8[6];
+       ctxt->m.b8[6] = tctxt.m.b8[5]; ctxt->m.b8[7] = tctxt.m.b8[4];
+       ctxt->m.b8[8] = tctxt.m.b8[11]; ctxt->m.b8[9] = tctxt.m.b8[10];
+       ctxt->m.b8[10] = tctxt.m.b8[9]; ctxt->m.b8[11] = tctxt.m.b8[8];
+       ctxt->m.b8[12] = tctxt.m.b8[15]; ctxt->m.b8[13] = tctxt.m.b8[14];
+       ctxt->m.b8[14] = tctxt.m.b8[13]; ctxt->m.b8[15] = tctxt.m.b8[12];
+       ctxt->m.b8[16] = tctxt.m.b8[19]; ctxt->m.b8[17] = tctxt.m.b8[18];
+       ctxt->m.b8[18] = tctxt.m.b8[17]; ctxt->m.b8[19] = tctxt.m.b8[16];
+       ctxt->m.b8[20] = tctxt.m.b8[23]; ctxt->m.b8[21] = tctxt.m.b8[22];
+       ctxt->m.b8[22] = tctxt.m.b8[21]; ctxt->m.b8[23] = tctxt.m.b8[20];
+       ctxt->m.b8[24] = tctxt.m.b8[27]; ctxt->m.b8[25] = tctxt.m.b8[26];
+       ctxt->m.b8[26] = tctxt.m.b8[25]; ctxt->m.b8[27] = tctxt.m.b8[24];
+       ctxt->m.b8[28] = tctxt.m.b8[31]; ctxt->m.b8[29] = tctxt.m.b8[30];
+       ctxt->m.b8[30] = tctxt.m.b8[29]; ctxt->m.b8[31] = tctxt.m.b8[28];
+       ctxt->m.b8[32] = tctxt.m.b8[35]; ctxt->m.b8[33] = tctxt.m.b8[34];
+       ctxt->m.b8[34] = tctxt.m.b8[33]; ctxt->m.b8[35] = tctxt.m.b8[32];
+       ctxt->m.b8[36] = tctxt.m.b8[39]; ctxt->m.b8[37] = tctxt.m.b8[38];
+       ctxt->m.b8[38] = tctxt.m.b8[37]; ctxt->m.b8[39] = tctxt.m.b8[36];
+       ctxt->m.b8[40] = tctxt.m.b8[43]; ctxt->m.b8[41] = tctxt.m.b8[42];
+       ctxt->m.b8[42] = tctxt.m.b8[41]; ctxt->m.b8[43] = tctxt.m.b8[40];
+       ctxt->m.b8[44] = tctxt.m.b8[47]; ctxt->m.b8[45] = tctxt.m.b8[46];
+       ctxt->m.b8[46] = tctxt.m.b8[45]; ctxt->m.b8[47] = tctxt.m.b8[44];
+       ctxt->m.b8[48] = tctxt.m.b8[51]; ctxt->m.b8[49] = tctxt.m.b8[50];
+       ctxt->m.b8[50] = tctxt.m.b8[49]; ctxt->m.b8[51] = tctxt.m.b8[48];
+       ctxt->m.b8[52] = tctxt.m.b8[55]; ctxt->m.b8[53] = tctxt.m.b8[54];
+       ctxt->m.b8[54] = tctxt.m.b8[53]; ctxt->m.b8[55] = tctxt.m.b8[52];
+       ctxt->m.b8[56] = tctxt.m.b8[59]; ctxt->m.b8[57] = tctxt.m.b8[58];
+       ctxt->m.b8[58] = tctxt.m.b8[57]; ctxt->m.b8[59] = tctxt.m.b8[56];
+       ctxt->m.b8[60] = tctxt.m.b8[63]; ctxt->m.b8[61] = tctxt.m.b8[62];
+       ctxt->m.b8[62] = tctxt.m.b8[61]; ctxt->m.b8[63] = tctxt.m.b8[60];
+#endif
+
+       a = H(0); b = H(1); c = H(2); d = H(3); e = H(4);
+
+       for (t = 0; t < 20; t++) {
+               s = t & 0x0f;
+               if (t >= 16) {
+                       W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
+               }
+               tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
+               e = d; d = c; c = S(30, b); b = a; a = tmp;
+       }
+       for (t = 20; t < 40; t++) {
+               s = t & 0x0f;
+               W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
+               tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t);
+               e = d; d = c; c = S(30, b); b = a; a = tmp;
+       }
+       for (t = 40; t < 60; t++) {
+               s = t & 0x0f;
+               W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
+               tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t);
+               e = d; d = c; c = S(30, b); b = a; a = tmp;
+       }
+       for (t = 60; t < 80; t++) {
+               s = t & 0x0f;
+               W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
+               tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t);
+               e = d; d = c; c = S(30, b); b = a; a = tmp;
+       }
+
+       H(0) = H(0) + a;
+       H(1) = H(1) + b;
+       H(2) = H(2) + c;
+       H(3) = H(3) + d;
+       H(4) = H(4) + e;
+
+       bzero(&ctxt->m.b8[0], 64);
+}
+
+/*------------------------------------------------------------*/
+
+void
+sha1_init(struct sha1_ctxt *ctxt)
+{
+       bzero(ctxt, sizeof(struct sha1_ctxt));
+       H(0) = 0x67452301;
+       H(1) = 0xefcdab89;
+       H(2) = 0x98badcfe;
+       H(3) = 0x10325476;
+       H(4) = 0xc3d2e1f0;
+}
+
+void
+sha1_pad(struct sha1_ctxt *ctxt)
+{
+       size_t padlen;          /*pad length in bytes*/
+       size_t padstart;
+
+       PUTPAD(0x80);
+
+       padstart = COUNT % 64;
+       padlen = 64 - padstart;
+       if (padlen < 8) {
+               bzero(&ctxt->m.b8[padstart], padlen);
+               COUNT += padlen;
+               COUNT %= 64;
+               sha1_step(ctxt);
+               padstart = COUNT % 64;  /* should be 0 */
+               padlen = 64 - padstart; /* should be 64 */
+       }
+       bzero(&ctxt->m.b8[padstart], padlen - 8);
+       COUNT += (padlen - 8);
+       COUNT %= 64;
+#if BYTE_ORDER == BIG_ENDIAN
+       PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]);
+       PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]);
+       PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]);
+       PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]);
+#else
+       PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]);
+       PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]);
+       PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]);
+       PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]);
+#endif
+}
+
+void
+sha1_loop(struct sha1_ctxt *ctxt, const u_int8_t *input, size_t len)
+{
+       size_t gaplen;
+       size_t gapstart;
+       size_t off;
+       size_t copysiz;
+
+       off = 0;
+
+       while (off < len) {
+               gapstart = COUNT % 64;
+               gaplen = 64 - gapstart;
+
+               copysiz = (gaplen < len - off) ? gaplen : len - off;
+               memcpy(&ctxt->m.b8[gapstart], &input[off], copysiz);
+               COUNT += copysiz;
+               COUNT %= 64;
+               ctxt->c.b64[0] += copysiz * 8;
+               if (COUNT % 64 == 0)
+                       sha1_step(ctxt);
+               off += copysiz;
+       }
+}
+
+void
+sha1_result(struct sha1_ctxt *ctxt, caddr_t digest0)
+{
+       u_int8_t *digest;
+
+       digest = (u_int8_t *)digest0;
+       sha1_pad(ctxt);
+#if BYTE_ORDER == BIG_ENDIAN
+       memcpy(digest, &ctxt->h.b8[0], 20);
+#else
+       digest[0] = ctxt->h.b8[3]; digest[1] = ctxt->h.b8[2];
+       digest[2] = ctxt->h.b8[1]; digest[3] = ctxt->h.b8[0];
+       digest[4] = ctxt->h.b8[7]; digest[5] = ctxt->h.b8[6];
+       digest[6] = ctxt->h.b8[5]; digest[7] = ctxt->h.b8[4];
+       digest[8] = ctxt->h.b8[11]; digest[9] = ctxt->h.b8[10];
+       digest[10] = ctxt->h.b8[9]; digest[11] = ctxt->h.b8[8];
+       digest[12] = ctxt->h.b8[15]; digest[13] = ctxt->h.b8[14];
+       digest[14] = ctxt->h.b8[13]; digest[15] = ctxt->h.b8[12];
+       digest[16] = ctxt->h.b8[19]; digest[17] = ctxt->h.b8[18];
+       digest[18] = ctxt->h.b8[17]; digest[19] = ctxt->h.b8[16];
+#endif
+}
+
+/*
+ * This should look and work like the libcrypto implementation
+ */
+
+unsigned char *
+SHA1(const unsigned char *d, size_t n, unsigned char *md)
+{
+       struct sha1_ctxt ctx;
+
+       sha1_init(&ctx);
+       sha1_loop(&ctx, d, n);
+       sha1_result(&ctx, (caddr_t)md);
+
+       return md;
+}
+
+#endif /*unsupported*/
+
index c8a3477..0016684 100644 (file)
@@ -317,7 +317,7 @@ asking to upgrade the connection to a websocket
 one.  This is a chance to serve http content,
 for example, to send a script to the client
 which will then open the websockets connection.
-<tt><b>in</b></tt> points to the URI path requested and 
+<tt><b>in</b></tt> points to the URI path requested and
 <b>libwebsockets_serve_http_file</b> makes it very
 simple to send back a file to the client.
 </blockquote>