Crypto build cleanup: remove INTERNAL_SHA1
authorJohannes Berg <johannes@sipsolutions.net>
Tue, 28 Jul 2009 17:47:04 +0000 (20:47 +0300)
committerJouni Malinen <j@w1.fi>
Tue, 28 Jul 2009 17:47:04 +0000 (20:47 +0300)
Instead of using a define and conditional building of sha1.c parts,
move the internal-SHA-1 into a separate file.

eap_example/Makefile
hostapd/Makefile
src/crypto/sha1-internal.c [new file with mode: 0644]
src/crypto/sha1.c
src/utils/build_config.h
wpa_supplicant/Makefile
wpa_supplicant/vs2005/wpa_passphrase/wpa_passphrase.vcproj

index c3e3256..5c11a07 100644 (file)
@@ -34,7 +34,7 @@ OBJS_both += ../src/utils/wpabuf.o
 OBJS_both += ../src/crypto/md5.o
 OBJS_both += ../src/crypto/rc4.o
 OBJS_both += ../src/crypto/md4.o
-OBJS_both += ../src/crypto/sha1.o
+OBJS_both += ../src/crypto/sha1.o ../src/crypto/sha1-internal.o
 OBJS_both += ../src/crypto/des.o
 OBJS_both += ../src/crypto/aes_wrap.o
 OBJS_both += ../src/crypto/aes.o
@@ -121,7 +121,6 @@ CFLAGS += -DCONFIG_TLS_INTERNAL
 CFLAGS += -DCONFIG_CRYPTO_INTERNAL
 CFLAGS += -DCONFIG_INTERNAL_X509
 CFLAGS += -DINTERNAL_AES
-CFLAGS += -DINTERNAL_SHA1
 CFLAGS += -DINTERNAL_SHA256
 CFLAGS += -DINTERNAL_MD5
 CFLAGS += -DINTERNAL_MD4
index 84d33aa..6983e7a 100644 (file)
@@ -535,7 +535,7 @@ ifdef CONFIG_INTERNAL_AES
 CFLAGS += -DINTERNAL_AES
 endif
 ifdef CONFIG_INTERNAL_SHA1
-CFLAGS += -DINTERNAL_SHA1
+OBJS += ../src/crypto/sha1-internal.o
 endif
 ifdef CONFIG_INTERNAL_SHA256
 CFLAGS += -DINTERNAL_SHA256
@@ -646,6 +646,9 @@ hostapd_cli: $(OBJS_c)
        $(CC) -o hostapd_cli $(OBJS_c)
 
 NOBJS = nt_password_hash.o ../src/crypto/ms_funcs.o ../src/crypto/sha1.o ../src/crypto/rc4.o ../src/crypto/md5.o
+ifdef CONFIG_INTERNAL_SHA1
+NOBJS += ../src/crypto/sha1-internal.o
+endif
 NOBJS += ../src/crypto/crypto_openssl.o ../src/utils/os_$(CONFIG_OS).o
 ifdef TLS_FUNCS
 LIBS_n += -lcrypto
diff --git a/src/crypto/sha1-internal.c b/src/crypto/sha1-internal.c
new file mode 100644 (file)
index 0000000..702b861
--- /dev/null
@@ -0,0 +1,373 @@
+/*
+ * SHA1 hash implementation and interface functions
+ * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Alternatively, this software may be distributed under the terms of BSD
+ * license.
+ *
+ * See README and COPYING for more details.
+ */
+
+#include "includes.h"
+
+#include "common.h"
+#include "sha1.h"
+#include "md5.h"
+#include "crypto.h"
+
+struct SHA1Context {
+       u32 state[5];
+       u32 count[2];
+       unsigned char buffer[64];
+};
+
+typedef struct SHA1Context SHA1_CTX;
+
+#ifndef CONFIG_CRYPTO_INTERNAL
+static void SHA1Init(struct SHA1Context *context);
+static void SHA1Update(struct SHA1Context *context, const void *data, u32 len);
+static void SHA1Final(unsigned char digest[20], struct SHA1Context *context);
+#endif /* CONFIG_CRYPTO_INTERNAL */
+static void SHA1Transform(u32 state[5], const unsigned char buffer[64]);
+
+
+/**
+ * sha1_vector - SHA-1 hash for data vector
+ * @num_elem: Number of elements in the data vector
+ * @addr: Pointers to the data areas
+ * @len: Lengths of the data blocks
+ * @mac: Buffer for the hash
+ */
+void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
+                u8 *mac)
+{
+       SHA1_CTX ctx;
+       size_t i;
+
+       SHA1Init(&ctx);
+       for (i = 0; i < num_elem; i++)
+               SHA1Update(&ctx, addr[i], len[i]);
+       SHA1Final(mac, &ctx);
+}
+
+
+#ifndef CONFIG_NO_FIPS186_2_PRF
+int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
+{
+       u8 xkey[64];
+       u32 t[5], _t[5];
+       int i, j, m, k;
+       u8 *xpos = x;
+       u32 carry;
+
+       if (seed_len > sizeof(xkey))
+               seed_len = sizeof(xkey);
+
+       /* FIPS 186-2 + change notice 1 */
+
+       os_memcpy(xkey, seed, seed_len);
+       os_memset(xkey + seed_len, 0, 64 - seed_len);
+       t[0] = 0x67452301;
+       t[1] = 0xEFCDAB89;
+       t[2] = 0x98BADCFE;
+       t[3] = 0x10325476;
+       t[4] = 0xC3D2E1F0;
+
+       m = xlen / 40;
+       for (j = 0; j < m; j++) {
+               /* XSEED_j = 0 */
+               for (i = 0; i < 2; i++) {
+                       /* XVAL = (XKEY + XSEED_j) mod 2^b */
+
+                       /* w_i = G(t, XVAL) */
+                       os_memcpy(_t, t, 20);
+                       SHA1Transform(_t, xkey);
+                       _t[0] = host_to_be32(_t[0]);
+                       _t[1] = host_to_be32(_t[1]);
+                       _t[2] = host_to_be32(_t[2]);
+                       _t[3] = host_to_be32(_t[3]);
+                       _t[4] = host_to_be32(_t[4]);
+                       os_memcpy(xpos, _t, 20);
+
+                       /* XKEY = (1 + XKEY + w_i) mod 2^b */
+                       carry = 1;
+                       for (k = 19; k >= 0; k--) {
+                               carry += xkey[k] + xpos[k];
+                               xkey[k] = carry & 0xff;
+                               carry >>= 8;
+                       }
+
+                       xpos += SHA1_MAC_LEN;
+               }
+               /* x_j = w_0|w_1 */
+       }
+
+       return 0;
+}
+#endif /* CONFIG_NO_FIPS186_2_PRF */
+
+
+/* ===== start - public domain SHA1 implementation ===== */
+
+/*
+SHA-1 in C
+By Steve Reid <sreid@sea-to-sky.net>
+100% Public Domain
+
+-----------------
+Modified 7/98 
+By James H. Brown <jbrown@burgoyne.com>
+Still 100% Public Domain
+
+Corrected a problem which generated improper hash values on 16 bit machines
+Routine SHA1Update changed from
+       void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int
+len)
+to
+       void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned
+long len)
+
+The 'len' parameter was declared an int which works fine on 32 bit machines.
+However, on 16 bit machines an int is too small for the shifts being done
+against
+it.  This caused the hash function to generate incorrect values if len was
+greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
+
+Since the file IO in main() reads 16K at a time, any file 8K or larger would
+be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
+"a"s).
+
+I also changed the declaration of variables i & j in SHA1Update to 
+unsigned long from unsigned int for the same reason.
+
+These changes should make no difference to any 32 bit implementations since
+an
+int and a long are the same size in those environments.
+
+--
+I also corrected a few compiler warnings generated by Borland C.
+1. Added #include <process.h> for exit() prototype
+2. Removed unused variable 'j' in SHA1Final
+3. Changed exit(0) to return(0) at end of main.
+
+ALL changes I made can be located by searching for comments containing 'JHB'
+-----------------
+Modified 8/98
+By Steve Reid <sreid@sea-to-sky.net>
+Still 100% public domain
+
+1- Removed #include <process.h> and used return() instead of exit()
+2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
+3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
+
+-----------------
+Modified 4/01
+By Saul Kravitz <Saul.Kravitz@celera.com>
+Still 100% PD
+Modified to run on Compaq Alpha hardware.  
+
+-----------------
+Modified 4/01
+By Jouni Malinen <j@w1.fi>
+Minor changes to match the coding style used in Dynamics.
+
+Modified September 24, 2004
+By Jouni Malinen <j@w1.fi>
+Fixed alignment issue in SHA1Transform when SHA1HANDSOFF is defined.
+
+*/
+
+/*
+Test Vectors (from FIPS PUB 180-1)
+"abc"
+  A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
+"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+  84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
+A million repetitions of "a"
+  34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
+*/
+
+#define SHA1HANDSOFF
+
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+
+/* blk0() and blk() perform the initial expand. */
+/* I got the idea of expanding during the round function from SSLeay */
+#ifndef WORDS_BIGENDIAN
+#define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \
+       (rol(block->l[i], 8) & 0x00FF00FF))
+#else
+#define blk0(i) block->l[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))
+
+/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
+#define R0(v,w,x,y,z,i) \
+       z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
+       w = rol(w, 30);
+#define R1(v,w,x,y,z,i) \
+       z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
+       w = rol(w, 30);
+#define R2(v,w,x,y,z,i) \
+       z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30);
+#define R3(v,w,x,y,z,i) \
+       z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
+       w = rol(w, 30);
+#define R4(v,w,x,y,z,i) \
+       z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
+       w=rol(w, 30);
+
+
+#ifdef VERBOSE  /* SAK */
+void SHAPrintContext(SHA1_CTX *context, char *msg)
+{
+       printf("%s (%d,%d) %x %x %x %x %x\n",
+              msg,
+              context->count[0], context->count[1], 
+              context->state[0],
+              context->state[1],
+              context->state[2],
+              context->state[3],
+              context->state[4]);
+}
+#endif
+
+/* Hash a single 512-bit block. This is the core of the algorithm. */
+
+static void SHA1Transform(u32 state[5], const unsigned char buffer[64])
+{
+       u32 a, b, c, d, e;
+       typedef union {
+               unsigned char c[64];
+               u32 l[16];
+       } CHAR64LONG16;
+       CHAR64LONG16* block;
+#ifdef SHA1HANDSOFF
+       u32 workspace[16];
+       block = (CHAR64LONG16 *) workspace;
+       os_memcpy(block, buffer, 64);
+#else
+       block = (CHAR64LONG16 *) buffer;
+#endif
+       /* Copy context->state[] to working vars */
+       a = state[0];
+       b = state[1];
+       c = state[2];
+       d = state[3];
+       e = state[4];
+       /* 4 rounds of 20 operations each. Loop unrolled. */
+       R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
+       R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
+       R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
+       R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
+       R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
+       R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
+       R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
+       R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
+       R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
+       R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
+       R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
+       R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
+       R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
+       R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
+       R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
+       R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
+       R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
+       R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
+       R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
+       R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
+       /* Add the working vars back into context.state[] */
+       state[0] += a;
+       state[1] += b;
+       state[2] += c;
+       state[3] += d;
+       state[4] += e;
+       /* Wipe variables */
+       a = b = c = d = e = 0;
+#ifdef SHA1HANDSOFF
+       os_memset(block, 0, 64);
+#endif
+}
+
+
+/* SHA1Init - Initialize new context */
+
+void SHA1Init(SHA1_CTX* context)
+{
+       /* SHA1 initialization constants */
+       context->state[0] = 0x67452301;
+       context->state[1] = 0xEFCDAB89;
+       context->state[2] = 0x98BADCFE;
+       context->state[3] = 0x10325476;
+       context->state[4] = 0xC3D2E1F0;
+       context->count[0] = context->count[1] = 0;
+}
+
+
+/* Run your data through this. */
+
+void SHA1Update(SHA1_CTX* context, const void *_data, u32 len)
+{
+       u32 i, j;
+       const unsigned char *data = _data;
+
+#ifdef VERBOSE
+       SHAPrintContext(context, "before");
+#endif
+       j = (context->count[0] >> 3) & 63;
+       if ((context->count[0] += len << 3) < (len << 3))
+               context->count[1]++;
+       context->count[1] += (len >> 29);
+       if ((j + len) > 63) {
+               os_memcpy(&context->buffer[j], data, (i = 64-j));
+               SHA1Transform(context->state, context->buffer);
+               for ( ; i + 63 < len; i += 64) {
+                       SHA1Transform(context->state, &data[i]);
+               }
+               j = 0;
+       }
+       else i = 0;
+       os_memcpy(&context->buffer[j], &data[i], len - i);
+#ifdef VERBOSE
+       SHAPrintContext(context, "after ");
+#endif
+}
+
+
+/* Add padding and return the message digest. */
+
+void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
+{
+       u32 i;
+       unsigned char finalcount[8];
+
+       for (i = 0; i < 8; i++) {
+               finalcount[i] = (unsigned char)
+                       ((context->count[(i >= 4 ? 0 : 1)] >>
+                         ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
+       }
+       SHA1Update(context, (unsigned char *) "\200", 1);
+       while ((context->count[0] & 504) != 448) {
+               SHA1Update(context, (unsigned char *) "\0", 1);
+       }
+       SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform()
+                                             */
+       for (i = 0; i < 20; i++) {
+               digest[i] = (unsigned char)
+                       ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) &
+                        255);
+       }
+       /* Wipe variables */
+       i = 0;
+       os_memset(context->buffer, 0, 64);
+       os_memset(context->state, 0, 20);
+       os_memset(context->count, 0, 8);
+       os_memset(finalcount, 0, 8);
+}
+
+/* ===== end - public domain SHA1 implementation ===== */
index ceec1a4..e05210f 100644 (file)
@@ -373,361 +373,3 @@ void pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len,
 }
 
 #endif /* CONFIG_NO_PBKDF2 */
-
-
-#ifdef INTERNAL_SHA1
-
-struct SHA1Context {
-       u32 state[5];
-       u32 count[2];
-       unsigned char buffer[64];
-};
-
-typedef struct SHA1Context SHA1_CTX;
-
-#ifndef CONFIG_CRYPTO_INTERNAL
-static void SHA1Init(struct SHA1Context *context);
-static void SHA1Update(struct SHA1Context *context, const void *data, u32 len);
-static void SHA1Final(unsigned char digest[20], struct SHA1Context *context);
-#endif /* CONFIG_CRYPTO_INTERNAL */
-static void SHA1Transform(u32 state[5], const unsigned char buffer[64]);
-
-
-/**
- * sha1_vector - SHA-1 hash for data vector
- * @num_elem: Number of elements in the data vector
- * @addr: Pointers to the data areas
- * @len: Lengths of the data blocks
- * @mac: Buffer for the hash
- */
-void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len,
-                u8 *mac)
-{
-       SHA1_CTX ctx;
-       size_t i;
-
-       SHA1Init(&ctx);
-       for (i = 0; i < num_elem; i++)
-               SHA1Update(&ctx, addr[i], len[i]);
-       SHA1Final(mac, &ctx);
-}
-
-
-#ifndef CONFIG_NO_FIPS186_2_PRF
-int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
-{
-       u8 xkey[64];
-       u32 t[5], _t[5];
-       int i, j, m, k;
-       u8 *xpos = x;
-       u32 carry;
-
-       if (seed_len > sizeof(xkey))
-               seed_len = sizeof(xkey);
-
-       /* FIPS 186-2 + change notice 1 */
-
-       os_memcpy(xkey, seed, seed_len);
-       os_memset(xkey + seed_len, 0, 64 - seed_len);
-       t[0] = 0x67452301;
-       t[1] = 0xEFCDAB89;
-       t[2] = 0x98BADCFE;
-       t[3] = 0x10325476;
-       t[4] = 0xC3D2E1F0;
-
-       m = xlen / 40;
-       for (j = 0; j < m; j++) {
-               /* XSEED_j = 0 */
-               for (i = 0; i < 2; i++) {
-                       /* XVAL = (XKEY + XSEED_j) mod 2^b */
-
-                       /* w_i = G(t, XVAL) */
-                       os_memcpy(_t, t, 20);
-                       SHA1Transform(_t, xkey);
-                       _t[0] = host_to_be32(_t[0]);
-                       _t[1] = host_to_be32(_t[1]);
-                       _t[2] = host_to_be32(_t[2]);
-                       _t[3] = host_to_be32(_t[3]);
-                       _t[4] = host_to_be32(_t[4]);
-                       os_memcpy(xpos, _t, 20);
-
-                       /* XKEY = (1 + XKEY + w_i) mod 2^b */
-                       carry = 1;
-                       for (k = 19; k >= 0; k--) {
-                               carry += xkey[k] + xpos[k];
-                               xkey[k] = carry & 0xff;
-                               carry >>= 8;
-                       }
-
-                       xpos += SHA1_MAC_LEN;
-               }
-               /* x_j = w_0|w_1 */
-       }
-
-       return 0;
-}
-#endif /* CONFIG_NO_FIPS186_2_PRF */
-
-
-/* ===== start - public domain SHA1 implementation ===== */
-
-/*
-SHA-1 in C
-By Steve Reid <sreid@sea-to-sky.net>
-100% Public Domain
-
------------------
-Modified 7/98 
-By James H. Brown <jbrown@burgoyne.com>
-Still 100% Public Domain
-
-Corrected a problem which generated improper hash values on 16 bit machines
-Routine SHA1Update changed from
-       void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int
-len)
-to
-       void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned
-long len)
-
-The 'len' parameter was declared an int which works fine on 32 bit machines.
-However, on 16 bit machines an int is too small for the shifts being done
-against
-it.  This caused the hash function to generate incorrect values if len was
-greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
-
-Since the file IO in main() reads 16K at a time, any file 8K or larger would
-be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
-"a"s).
-
-I also changed the declaration of variables i & j in SHA1Update to 
-unsigned long from unsigned int for the same reason.
-
-These changes should make no difference to any 32 bit implementations since
-an
-int and a long are the same size in those environments.
-
---
-I also corrected a few compiler warnings generated by Borland C.
-1. Added #include <process.h> for exit() prototype
-2. Removed unused variable 'j' in SHA1Final
-3. Changed exit(0) to return(0) at end of main.
-
-ALL changes I made can be located by searching for comments containing 'JHB'
------------------
-Modified 8/98
-By Steve Reid <sreid@sea-to-sky.net>
-Still 100% public domain
-
-1- Removed #include <process.h> and used return() instead of exit()
-2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
-3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
-
------------------
-Modified 4/01
-By Saul Kravitz <Saul.Kravitz@celera.com>
-Still 100% PD
-Modified to run on Compaq Alpha hardware.  
-
------------------
-Modified 4/01
-By Jouni Malinen <j@w1.fi>
-Minor changes to match the coding style used in Dynamics.
-
-Modified September 24, 2004
-By Jouni Malinen <j@w1.fi>
-Fixed alignment issue in SHA1Transform when SHA1HANDSOFF is defined.
-
-*/
-
-/*
-Test Vectors (from FIPS PUB 180-1)
-"abc"
-  A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
-"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
-  84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
-A million repetitions of "a"
-  34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
-*/
-
-#define SHA1HANDSOFF
-
-#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
-
-/* blk0() and blk() perform the initial expand. */
-/* I got the idea of expanding during the round function from SSLeay */
-#ifndef WORDS_BIGENDIAN
-#define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \
-       (rol(block->l[i], 8) & 0x00FF00FF))
-#else
-#define blk0(i) block->l[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))
-
-/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
-#define R0(v,w,x,y,z,i) \
-       z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
-       w = rol(w, 30);
-#define R1(v,w,x,y,z,i) \
-       z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
-       w = rol(w, 30);
-#define R2(v,w,x,y,z,i) \
-       z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30);
-#define R3(v,w,x,y,z,i) \
-       z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
-       w = rol(w, 30);
-#define R4(v,w,x,y,z,i) \
-       z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
-       w=rol(w, 30);
-
-
-#ifdef VERBOSE  /* SAK */
-void SHAPrintContext(SHA1_CTX *context, char *msg)
-{
-       printf("%s (%d,%d) %x %x %x %x %x\n",
-              msg,
-              context->count[0], context->count[1], 
-              context->state[0],
-              context->state[1],
-              context->state[2],
-              context->state[3],
-              context->state[4]);
-}
-#endif
-
-/* Hash a single 512-bit block. This is the core of the algorithm. */
-
-static void SHA1Transform(u32 state[5], const unsigned char buffer[64])
-{
-       u32 a, b, c, d, e;
-       typedef union {
-               unsigned char c[64];
-               u32 l[16];
-       } CHAR64LONG16;
-       CHAR64LONG16* block;
-#ifdef SHA1HANDSOFF
-       u32 workspace[16];
-       block = (CHAR64LONG16 *) workspace;
-       os_memcpy(block, buffer, 64);
-#else
-       block = (CHAR64LONG16 *) buffer;
-#endif
-       /* Copy context->state[] to working vars */
-       a = state[0];
-       b = state[1];
-       c = state[2];
-       d = state[3];
-       e = state[4];
-       /* 4 rounds of 20 operations each. Loop unrolled. */
-       R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
-       R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
-       R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
-       R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
-       R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
-       R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
-       R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
-       R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
-       R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
-       R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
-       R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
-       R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
-       R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
-       R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
-       R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
-       R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
-       R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
-       R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
-       R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
-       R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
-       /* Add the working vars back into context.state[] */
-       state[0] += a;
-       state[1] += b;
-       state[2] += c;
-       state[3] += d;
-       state[4] += e;
-       /* Wipe variables */
-       a = b = c = d = e = 0;
-#ifdef SHA1HANDSOFF
-       os_memset(block, 0, 64);
-#endif
-}
-
-
-/* SHA1Init - Initialize new context */
-
-void SHA1Init(SHA1_CTX* context)
-{
-       /* SHA1 initialization constants */
-       context->state[0] = 0x67452301;
-       context->state[1] = 0xEFCDAB89;
-       context->state[2] = 0x98BADCFE;
-       context->state[3] = 0x10325476;
-       context->state[4] = 0xC3D2E1F0;
-       context->count[0] = context->count[1] = 0;
-}
-
-
-/* Run your data through this. */
-
-void SHA1Update(SHA1_CTX* context, const void *_data, u32 len)
-{
-       u32 i, j;
-       const unsigned char *data = _data;
-
-#ifdef VERBOSE
-       SHAPrintContext(context, "before");
-#endif
-       j = (context->count[0] >> 3) & 63;
-       if ((context->count[0] += len << 3) < (len << 3))
-               context->count[1]++;
-       context->count[1] += (len >> 29);
-       if ((j + len) > 63) {
-               os_memcpy(&context->buffer[j], data, (i = 64-j));
-               SHA1Transform(context->state, context->buffer);
-               for ( ; i + 63 < len; i += 64) {
-                       SHA1Transform(context->state, &data[i]);
-               }
-               j = 0;
-       }
-       else i = 0;
-       os_memcpy(&context->buffer[j], &data[i], len - i);
-#ifdef VERBOSE
-       SHAPrintContext(context, "after ");
-#endif
-}
-
-
-/* Add padding and return the message digest. */
-
-void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
-{
-       u32 i;
-       unsigned char finalcount[8];
-
-       for (i = 0; i < 8; i++) {
-               finalcount[i] = (unsigned char)
-                       ((context->count[(i >= 4 ? 0 : 1)] >>
-                         ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
-       }
-       SHA1Update(context, (unsigned char *) "\200", 1);
-       while ((context->count[0] & 504) != 448) {
-               SHA1Update(context, (unsigned char *) "\0", 1);
-       }
-       SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform()
-                                             */
-       for (i = 0; i < 20; i++) {
-               digest[i] = (unsigned char)
-                       ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) &
-                        255);
-       }
-       /* Wipe variables */
-       i = 0;
-       os_memset(context->buffer, 0, 64);
-       os_memset(context->state, 0, 20);
-       os_memset(context->count, 0, 8);
-       os_memset(finalcount, 0, 8);
-}
-
-/* ===== end - public domain SHA1 implementation ===== */
-
-#endif /* INTERNAL_SHA1 */
index 1e147fe..4211c04 100644 (file)
@@ -52,7 +52,6 @@
 #define CONFIG_TLS_INTERNAL_CLIENT
 #define CONFIG_INTERNAL_LIBTOMMATH
 #define INTERNAL_AES
-#define INTERNAL_SHA1
 #define INTERNAL_SHA256
 #define INTERNAL_MD5
 #define INTERNAL_MD4
@@ -70,7 +69,6 @@
 #define CONFIG_NO_STDOUT_DEBUG
 #define CONFIG_BACKEND_FILE
 #define INTERNAL_AES
-#define INTERNAL_SHA1
 #define INTERNAL_MD5
 #define INTERNAL_MD4
 #define INTERNAL_DES
index e5b524b..1ae715d 100644 (file)
@@ -41,6 +41,9 @@ install: all
        for i in $(ALL); do cp $$i $(DESTDIR)$(BINDIR)/$$i; done
        $(MAKE) -C ../src install
 
+SHA1OBJS = ../src/crypto/sha1.o
+
+
 OBJS = config.o
 OBJS += ../src/utils/common.o
 OBJS += ../src/utils/wpa_debug.o
@@ -48,14 +51,12 @@ OBJS += ../src/utils/wpabuf.o
 OBJS += ../src/crypto/md5.o
 OBJS += ../src/crypto/rc4.o
 OBJS += ../src/crypto/md4.o
-OBJS += ../src/crypto/sha1.o
 OBJS += ../src/crypto/des.o
 OBJS_p = wpa_passphrase.o
 OBJS_p += ../src/utils/common.o
 OBJS_p += ../src/utils/wpa_debug.o
 OBJS_p += ../src/crypto/md5.o
 OBJS_p += ../src/crypto/md4.o
-OBJS_p += ../src/crypto/sha1.o
 OBJS_p += ../src/crypto/des.o
 OBJS_c = wpa_cli.o ../src/common/wpa_ctrl.o
 
@@ -906,7 +907,7 @@ ifdef CONFIG_INTERNAL_AES
 CFLAGS += -DINTERNAL_AES
 endif
 ifdef CONFIG_INTERNAL_SHA1
-CFLAGS += -DINTERNAL_SHA1
+SHA1OBJS += ../src/crypto/sha1-internal.o
 endif
 ifdef CONFIG_INTERNAL_SHA256
 CFLAGS += -DINTERNAL_SHA256
@@ -1116,6 +1117,9 @@ CFLAGS += -DCONFIG_DELAYED_MIC_ERROR_REPORT
 endif
 
 OBJS += ../src/drivers/scan_helpers.o
+OBJS += $(SHA1OBJS)
+
+OBJS_p += $(SHA1OBJS)
 
 OBJS_wpa_rm := ctrl_iface.o mlme.o ctrl_iface_unix.o
 OBJS_wpa := $(filter-out $(OBJS_wpa_rm),$(OBJS)) $(OBJS_h) tests/test_wpa.o
@@ -1142,7 +1146,7 @@ OBJS_priv += ../src/utils/wpa_debug.o
 OBJS_priv += ../src/utils/wpabuf.o
 OBJS_priv += wpa_priv.o
 ifdef CONFIG_DRIVER_TEST
-OBJS_priv += ../src/crypto/sha1.o
+OBJS_priv += $(SHA1OBJS)
 OBJS_priv += ../src/crypto/md5.o
 ifeq ($(CONFIG_TLS), openssl)
 OBJS_priv += ../src/crypto/crypto_openssl.o
@@ -1208,7 +1212,7 @@ test_wpa: $(OBJS_wpa) $(OBJS_h)
        $(LDO) $(LDFLAGS) -o test_wpa $(OBJS_wpa) $(LIBS)
 
 OBJSa=../src/tls/asn1_test.o ../src/tls/asn1.o ../src/tls/x509v3.o ../src/utils/common.o ../src/utils/wpa_debug.o ../src/utils/os_unix.o \
-       ../src/crypto/crypto_$(CONFIG_CRYPTO).o ../src/crypto/md5.o ../src/crypto/sha1.o \
+       ../src/crypto/crypto_$(CONFIG_CRYPTO).o ../src/crypto/md5.o $(SHA1OBSJ) \
        ../src/crypto/rc4.o ../src/crypto/des.o ../src/crypto/aes_wrap.o \
        ../src/crypto/aes.o ../src/tls/bignum.o ../src/tls/rsa.o
 asn1_test: $(OBJSa)
@@ -1217,7 +1221,7 @@ asn1_test: $(OBJSa)
 OBJSx=tests/test_x509v3.o ../src/tls/asn1.o ../src/tls/x509v3.o \
        ../src/utils/common.o ../src/utils/wpa_debug.o ../src/utils/os_unix.o \
        ../src/crypto/crypto_$(CONFIG_CRYPTO).o \
-       ../src/crypto/md5.o ../src/crypto/sha1.o ../src/crypto/aes.o \
+       ../src/crypto/md5.o $(SHA1OBJS) ../src/crypto/aes.o \
        ../src/crypto/rc4.o ../src/crypto/des.o ../src/crypto/aes_wrap.o \
        ../src/crypto/sha256.o \
        ../src/tls/bignum.o ../src/tls/rsa.o
@@ -1290,14 +1294,14 @@ wpa_gui-qt4/Makefile:
 wpa_gui-qt4: wpa_gui-qt4/Makefile
        $(MAKE) -C wpa_gui-qt4
 
-TEST_MS_FUNCS_OBJS = ../src/crypto/crypto_openssl.o ../src/crypto/sha1.o ../src/crypto/md5.o \
+TEST_MS_FUNCS_OBJS = ../src/crypto/crypto_openssl.o $(SHA1OBJS) ../src/crypto/md5.o \
        ../src/utils/os_unix.o ../src/crypto/rc4.o tests/test_ms_funcs.o
 test-ms_funcs: $(TEST_MS_FUNCS_OBJS)
        $(LDO) $(LDFLAGS) -o $@ $(TEST_MS_FUNCS_OBJS) $(LIBS) -lcrypto
        ./test-ms_funcs
        rm test-ms_funcs
 
-TEST_SHA1_OBJS = ../src/crypto/sha1.o ../src/crypto/md5.o tests/test_sha1.o #../src/crypto/crypto_openssl.o
+TEST_SHA1_OBJS = $(SHA1OBJS) ../src/crypto/md5.o tests/test_sha1.o #../src/crypto/crypto_openssl.o
 test-sha1: $(TEST_SHA1_OBJS)
        $(LDO) $(LDFLAGS) -o $@ $(TEST_SHA1_OBJS) $(LIBS)
        ./test-sha1
@@ -1315,7 +1319,7 @@ test-aes: $(TEST_AES_OBJS)
        ./test-aes
        rm test-aes
 
-TEST_EAP_SIM_COMMON_OBJS = ../src/crypto/sha1.o ../src/crypto/md5.o \
+TEST_EAP_SIM_COMMON_OBJS = $(SHA1OBJS) ../src/crypto/md5.o \
        ../src/crypto/aes_wrap.o ../src/utils/common.o ../src/utils/os_unix.o \
        ../src/utils/wpa_debug.o ../src/crypto/aes.o \
        tests/test_eap_sim_common.o
index 1a3618b..1b4b9e3 100755 (executable)
@@ -41,7 +41,7 @@
                                Name="VCCLCompilerTool"\r
                                Optimization="0"\r
                                AdditionalIncludeDirectories="..\..\..\src\utils;..\..\..\src\crypto;C:\dev\openssl\include"\r
-                               PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;CONFIG_WIN32_DEFAULTS;INTERNAL_SHA1;INTERNAL_MD5"\r
+                               PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;CONFIG_WIN32_DEFAULTS;INTERNAL_MD5"\r
                                MinimalRebuild="true"\r
                                BasicRuntimeChecks="3"\r
                                RuntimeLibrary="3"\r
                        <Tool\r
                                Name="VCCLCompilerTool"\r
                                AdditionalIncludeDirectories="..\..\..\src\utils;..\..\..\src\crypto;C:\dev\openssl\include"\r
-                               PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;CONFIG_WIN32_DEFAULTS;INTERNAL_SHA1;INTERNAL_MD5"\r
+                               PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;CONFIG_WIN32_DEFAULTS;INTERNAL_MD5"\r
                                RuntimeLibrary="2"\r
                                UsePrecompiledHeader="0"\r
                                WarningLevel="3"\r
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath="..\..\..\src\crypto\sha1-internal.c"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath="..\..\wpa_passphrase.c"\r
                                >\r
                        </File>\r