]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/sha1.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / liblutil / sha1.c
index a0df8ac95b2a0aca78d819f3c783a312883f5c2e..bf1b6032940fa79d2af7907dd1953260f39a633a 100644 (file)
@@ -1,6 +1,24 @@
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2012 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* This work was derived from code developed by Steve Reid and
+ * adapted for use in OpenLDAP by Kurt D. Zeilenga.
+ */
+
+
 /*     Acquired from:
  *     $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $       */
-
 /*
  * SHA-1 in C
  * By Steve Reid <steve@edmweb.com>
  * A million repetitions of "a"
  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  */
-
+/*
+ * This code assumes uint32 is 32 bits and char is 8 bits
+ */
 
 #include "portable.h"
+#include <ac/param.h>
 #include <ac/string.h>
-
-/* include socket.h to get sys/types.h and/or winsock2.h */
 #include <ac/socket.h>
-
-#if defined(HAVE_SYS_PARAM_H)
-#include <sys/param.h>
-#endif
+#include <ac/bytes.h>
 
 #include "lutil_sha1.h"
 
+#ifdef LUTIL_SHA1_BYTES
+
+/* undefining this will cause pointer alignment errors */
 #define SHA1HANDSOFF           /* Copies data before messing with it. */
 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
 
  * I got the idea of expanding during the round function from SSLeay
  */
 #if BYTE_ORDER == LITTLE_ENDIAN
-# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
-    |(rol(block->l[i],8)&0x00FF00FF))
+# define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \
+    |(rol(block[i],8)&0x00FF00FF))
 #else
-# define blk0(i) block->l[i]
+# define blk0(i) block[i]
 #endif
-#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
-    ^block->l[(i+2)&15]^block->l[i&15],1))
+#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
+    ^block[(i+2)&15]^block[i&15],1))
 
 /*
  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
@@ -61,16 +80,12 @@ void
 lutil_SHA1Transform( uint32 *state, const unsigned char *buffer )
 {
     uint32 a, b, c, d, e;
-    typedef union {
-       unsigned char c[64];
-       u_int l[16];
-    } CHAR64LONG16;
 
 #ifdef SHA1HANDSOFF
-    CHAR64LONG16 block[1];
-    (void)memcpy(block, buffer, 64);
+    uint32 block[16];
+    (void)AC_MEMCPY(block, buffer, 64);
 #else
-    CHAR64LONG16 *block = (CHAR64LONG16 *)buffer;
+    uint32 *block = (u_int32 *) buffer;
 #endif
 
     /* Copy context->state[] to working vars */
@@ -148,7 +163,7 @@ lutil_SHA1Update(
        context->count[1] += (len>>29)+1;
     j = (j >> 3) & 63;
     if ((j + len) > 63) {
-       (void)memcpy(&context->buffer[j], data, (i = 64-j));
+       (void)AC_MEMCPY(&context->buffer[j], data, (i = 64-j));
        lutil_SHA1Transform(context->state, context->buffer);
        for ( ; i + 63 < len; i += 64)
            lutil_SHA1Transform(context->state, &data[i]);
@@ -156,7 +171,7 @@ lutil_SHA1Update(
     } else {
        i = 0;
     }
-    (void)memcpy(&context->buffer[j], &data[i], len - i);
+    (void)AC_MEMCPY(&context->buffer[j], &data[i], len - i);
 }
 
 
@@ -200,7 +215,7 @@ static char rcsid[] = "$OpenBSD: sha1hl.c,v 1.1 1997/07/12 20:06:03 millert Exp
 #endif /* LIBC_SCCS and not lint */
 
 #include <stdio.h>
-#include <stdlib.h>
+#include <ac/stdlib.h>
 
 #include <ac/errno.h>
 #include <ac/unistd.h>
@@ -208,9 +223,6 @@ static char rcsid[] = "$OpenBSD: sha1hl.c,v 1.1 1997/07/12 20:06:03 millert Exp
 #ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
 #endif
-#ifdef HAVE_SYS_UIO_H
-#include <sys/uio.h>
-#endif
 
 #ifdef HAVE_IO_H
 #include <io.h>
@@ -272,3 +284,5 @@ lutil_SHA1Data( const unsigned char *data, size_t len, char *buf )
     lutil_SHA1Update(&ctx, data, len);
     return(lutil_SHA1End(&ctx, buf));
 }
+
+#endif