updated spec enabled ssl
[profile/ivi/libwebsockets.git] / lib / sha-1.c
index 1bec395..6020c22 100644 (file)
  */
 
 #include <sys/types.h>
+#ifdef WIN32
+
+#ifndef BIG_ENDIAN
+#define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
+#endif
+#ifndef LITTLE_ENDIAN
+#define LITTLE_ENDIAN 1234
+#endif
+#ifndef BYTE_ORDER
+#define BYTE_ORDER LITTLE_ENDIAN
+#endif
+
+typedef unsigned __int64 u_int64_t;
+
+#undef __P
+#ifndef __P
+#if __STDC__
+#define __P(protos) protos
+#else
+#define __P(protos) ()
+#endif
+#endif
+
+#define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
+
+#else
+#include <sys/stat.h>
 #include <sys/cdefs.h>
 #include <sys/time.h>
+#endif
+
 #include <string.h>
 
 struct sha1_ctxt {
        union {
-               u_int8_t        b8[20];
-               u_int32_t       b32[5];
+               unsigned char           b8[20];
+               unsigned int            b32[5];
        } h;
        union {
-               u_int8_t        b8[8];
-               u_int64_t       b64[1];
+               unsigned char           b8[8];
+               u_int64_t               b64[1];
        } c;
        union {
-               u_int8_t        b8[64];
-               u_int32_t       b32[16];
+               unsigned char           b8[64];
+               unsigned int            b32[16];
        } m;
-       u_int8_t        count;
+       unsigned char                   count;
 };
 
 /* sanity check */
@@ -63,7 +92,8 @@ struct sha1_ctxt {
 #ifndef unsupported
 
 /* constant table */
-static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
+static const unsigned int _K[] =
+                       { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
 #define        K(t)    _K[(t) / 20]
 
 #define        F0(b, c, d)     (((b) & (c)) | ((~(b)) & (d)))
@@ -85,7 +115,7 @@ static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
        ctxt->c.b64[0] += 8;                    \
        if (COUNT % 64 == 0)                    \
                sha1_step(ctxt);                \
-     }
+       }
 
 #define        PUTPAD(x)       { \
        ctxt->m.b8[(COUNT % 64)] = (x);         \
@@ -93,16 +123,15 @@ static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
        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;
+       unsigned int    a, b, c, d, e, tmp;
        size_t t, s;
-       u_int32_t       tmp;
 
 #if BYTE_ORDER == LITTLE_ENDIAN
        struct sha1_ctxt tctxt;
@@ -146,27 +175,31 @@ sha1_step(struct sha1_ctxt *ctxt)
 
        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));
-               }
+               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));
+               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));
+               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));
+               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;
        }
@@ -228,7 +261,7 @@ sha1_pad(struct sha1_ctxt *ctxt)
 }
 
 void
-sha1_loop(struct sha1_ctxt *ctxt, const u_int8_t *input, size_t len)
+sha1_loop(struct sha1_ctxt *ctxt, const unsigned char *input, size_t len)
 {
        size_t gaplen;
        size_t gapstart;
@@ -253,11 +286,11 @@ sha1_loop(struct sha1_ctxt *ctxt, const u_int8_t *input, size_t len)
 }
 
 void
-sha1_result(struct sha1_ctxt *ctxt, caddr_t digest0)
+sha1_result(struct sha1_ctxt *ctxt, void *digest0)
 {
-       u_int8_t *digest;
+       unsigned char *digest;
 
-       digest = (u_int8_t *)digest0;
+       digest = (unsigned char *)digest0;
        sha1_pad(ctxt);
 #if BYTE_ORDER == BIG_ENDIAN
        memcpy(digest, &ctxt->h.b8[0], 20);
@@ -286,10 +319,9 @@ SHA1(const unsigned char *d, size_t n, unsigned char *md)
 
        sha1_init(&ctx);
        sha1_loop(&ctx, d, n);
-       sha1_result(&ctx, (caddr_t)md);
+       sha1_result(&ctx, (void *)md);
 
        return md;
 }
 
 #endif /*unsupported*/
-