From ef2e26c91b80556af033d3335e55f5dfa6fff31d Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 13 Aug 2003 01:53:07 +0000 Subject: first public release of samba4 code (This used to be commit b0510b5428b3461aeb9bbe3cc95f62fc73e2b97f) --- source4/lib/crypto/md4.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 source4/lib/crypto/md4.c (limited to 'source4/lib/crypto/md4.c') diff --git a/source4/lib/crypto/md4.c b/source4/lib/crypto/md4.c new file mode 100644 index 0000000000..417e87bd8e --- /dev/null +++ b/source4/lib/crypto/md4.c @@ -0,0 +1,175 @@ +/* + Unix SMB/CIFS implementation. + a implementation of MD4 designed for use in the SMB authentication protocol + Copyright (C) Andrew Tridgell 1997-1998. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +/* NOTE: This code makes no attempt to be fast! + + It assumes that a int is at least 32 bits long +*/ + +struct mdfour_state { + uint32 A, B, C, D; +}; + +static uint32 F(uint32 X, uint32 Y, uint32 Z) +{ + return (X&Y) | ((~X)&Z); +} + +static uint32 G(uint32 X, uint32 Y, uint32 Z) +{ + return (X&Y) | (X&Z) | (Y&Z); +} + +static uint32 H(uint32 X, uint32 Y, uint32 Z) +{ + return X^Y^Z; +} + +static uint32 lshift(uint32 x, int s) +{ + x &= 0xFFFFFFFF; + return ((x<>(32-s)); +} + +#define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s) +#define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32)0x5A827999,s) +#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32)0x6ED9EBA1,s) + +/* this applies md4 to 64 byte chunks */ +static void mdfour64(struct mdfour_state *s, uint32 *M) +{ + int j; + uint32 AA, BB, CC, DD; + uint32 X[16]; + + for (j=0;j<16;j++) + X[j] = M[j]; + + AA = s->A; BB = s->B; CC = s->C; DD = s->D; + + ROUND1(s->A,s->B,s->C,s->D, 0, 3); ROUND1(s->D,s->A,s->B,s->C, 1, 7); + ROUND1(s->C,s->D,s->A,s->B, 2, 11); ROUND1(s->B,s->C,s->D,s->A, 3, 19); + ROUND1(s->A,s->B,s->C,s->D, 4, 3); ROUND1(s->D,s->A,s->B,s->C, 5, 7); + ROUND1(s->C,s->D,s->A,s->B, 6, 11); ROUND1(s->B,s->C,s->D,s->A, 7, 19); + ROUND1(s->A,s->B,s->C,s->D, 8, 3); ROUND1(s->D,s->A,s->B,s->C, 9, 7); + ROUND1(s->C,s->D,s->A,s->B, 10, 11); ROUND1(s->B,s->C,s->D,s->A, 11, 19); + ROUND1(s->A,s->B,s->C,s->D, 12, 3); ROUND1(s->D,s->A,s->B,s->C, 13, 7); + ROUND1(s->C,s->D,s->A,s->B, 14, 11); ROUND1(s->B,s->C,s->D,s->A, 15, 19); + + ROUND2(s->A,s->B,s->C,s->D, 0, 3); ROUND2(s->D,s->A,s->B,s->C, 4, 5); + ROUND2(s->C,s->D,s->A,s->B, 8, 9); ROUND2(s->B,s->C,s->D,s->A, 12, 13); + ROUND2(s->A,s->B,s->C,s->D, 1, 3); ROUND2(s->D,s->A,s->B,s->C, 5, 5); + ROUND2(s->C,s->D,s->A,s->B, 9, 9); ROUND2(s->B,s->C,s->D,s->A, 13, 13); + ROUND2(s->A,s->B,s->C,s->D, 2, 3); ROUND2(s->D,s->A,s->B,s->C, 6, 5); + ROUND2(s->C,s->D,s->A,s->B, 10, 9); ROUND2(s->B,s->C,s->D,s->A, 14, 13); + ROUND2(s->A,s->B,s->C,s->D, 3, 3); ROUND2(s->D,s->A,s->B,s->C, 7, 5); + ROUND2(s->C,s->D,s->A,s->B, 11, 9); ROUND2(s->B,s->C,s->D,s->A, 15, 13); + + ROUND3(s->A,s->B,s->C,s->D, 0, 3); ROUND3(s->D,s->A,s->B,s->C, 8, 9); + ROUND3(s->C,s->D,s->A,s->B, 4, 11); ROUND3(s->B,s->C,s->D,s->A, 12, 15); + ROUND3(s->A,s->B,s->C,s->D, 2, 3); ROUND3(s->D,s->A,s->B,s->C, 10, 9); + ROUND3(s->C,s->D,s->A,s->B, 6, 11); ROUND3(s->B,s->C,s->D,s->A, 14, 15); + ROUND3(s->A,s->B,s->C,s->D, 1, 3); ROUND3(s->D,s->A,s->B,s->C, 9, 9); + ROUND3(s->C,s->D,s->A,s->B, 5, 11); ROUND3(s->B,s->C,s->D,s->A, 13, 15); + ROUND3(s->A,s->B,s->C,s->D, 3, 3); ROUND3(s->D,s->A,s->B,s->C, 11, 9); + ROUND3(s->C,s->D,s->A,s->B, 7, 11); ROUND3(s->B,s->C,s->D,s->A, 15, 15); + + s->A += AA; + s->B += BB; + s->C += CC; + s->D += DD; + + s->A &= 0xFFFFFFFF; + s->B &= 0xFFFFFFFF; + s->C &= 0xFFFFFFFF; + s->D &= 0xFFFFFFFF; + + for (j=0;j<16;j++) + X[j] = 0; +} + +static void copy64(uint32 *M, const unsigned char *in) +{ + int i; + + for (i=0;i<16;i++) + M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) | + (in[i*4+1]<<8) | (in[i*4+0]<<0); +} + +static void copy4(unsigned char *out, uint32 x) +{ + out[0] = x&0xFF; + out[1] = (x>>8)&0xFF; + out[2] = (x>>16)&0xFF; + out[3] = (x>>24)&0xFF; +} + +/* produce a md4 message digest from data of length n bytes */ +void mdfour(unsigned char *out, const unsigned char *in, int n) +{ + unsigned char buf[128]; + uint32 M[16]; + uint32 b = n * 8; + int i; + struct mdfour_state state; + + state.A = 0x67452301; + state.B = 0xefcdab89; + state.C = 0x98badcfe; + state.D = 0x10325476; + + while (n > 64) { + copy64(M, in); + mdfour64(&state, M); + in += 64; + n -= 64; + } + + for (i=0;i<128;i++) + buf[i] = 0; + memcpy(buf, in, n); + buf[n] = 0x80; + + if (n <= 55) { + copy4(buf+56, b); + copy64(M, buf); + mdfour64(&state, M); + } else { + copy4(buf+120, b); + copy64(M, buf); + mdfour64(&state, M); + copy64(M, buf+64); + mdfour64(&state, M); + } + + for (i=0;i<128;i++) + buf[i] = 0; + copy64(M, buf); + + copy4(out, state.A); + copy4(out+4, state.B); + copy4(out+8, state.C); + copy4(out+12, state.D); +} + + -- cgit From f9d8f8843dc0ab8c9d59abde7222e0f118b86b5d Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Tue, 25 May 2004 16:24:13 +0000 Subject: r884: convert samba4 to use [u]int32_t instead of [u]int32 metze (This used to be commit 0e5517d937a2eb7cf707991d1c7498c1ab456095) --- source4/lib/crypto/md4.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'source4/lib/crypto/md4.c') diff --git a/source4/lib/crypto/md4.c b/source4/lib/crypto/md4.c index 417e87bd8e..cc8b9b6841 100644 --- a/source4/lib/crypto/md4.c +++ b/source4/lib/crypto/md4.c @@ -26,40 +26,40 @@ */ struct mdfour_state { - uint32 A, B, C, D; + uint32_t A, B, C, D; }; -static uint32 F(uint32 X, uint32 Y, uint32 Z) +static uint32_t F(uint32_t X, uint32_t Y, uint32_t Z) { return (X&Y) | ((~X)&Z); } -static uint32 G(uint32 X, uint32 Y, uint32 Z) +static uint32_t G(uint32_t X, uint32_t Y, uint32_t Z) { return (X&Y) | (X&Z) | (Y&Z); } -static uint32 H(uint32 X, uint32 Y, uint32 Z) +static uint32_t H(uint32_t X, uint32_t Y, uint32_t Z) { return X^Y^Z; } -static uint32 lshift(uint32 x, int s) +static uint32_t lshift(uint32_t x, int s) { x &= 0xFFFFFFFF; return ((x<>(32-s)); } #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s) -#define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32)0x5A827999,s) -#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32)0x6ED9EBA1,s) +#define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32_t)0x5A827999,s) +#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32_t)0x6ED9EBA1,s) /* this applies md4 to 64 byte chunks */ -static void mdfour64(struct mdfour_state *s, uint32 *M) +static void mdfour64(struct mdfour_state *s, uint32_t *M) { int j; - uint32 AA, BB, CC, DD; - uint32 X[16]; + uint32_t AA, BB, CC, DD; + uint32_t X[16]; for (j=0;j<16;j++) X[j] = M[j]; @@ -107,7 +107,7 @@ static void mdfour64(struct mdfour_state *s, uint32 *M) X[j] = 0; } -static void copy64(uint32 *M, const unsigned char *in) +static void copy64(uint32_t *M, const unsigned char *in) { int i; @@ -116,7 +116,7 @@ static void copy64(uint32 *M, const unsigned char *in) (in[i*4+1]<<8) | (in[i*4+0]<<0); } -static void copy4(unsigned char *out, uint32 x) +static void copy4(unsigned char *out, uint32_t x) { out[0] = x&0xFF; out[1] = (x>>8)&0xFF; @@ -128,8 +128,8 @@ static void copy4(unsigned char *out, uint32 x) void mdfour(unsigned char *out, const unsigned char *in, int n) { unsigned char buf[128]; - uint32 M[16]; - uint32 b = n * 8; + uint32_t M[16]; + uint32_t b = n * 8; int i; struct mdfour_state state; -- cgit From 45e93c19ef95978f908f5b14962770510634cd3b Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Sat, 29 May 2004 08:11:46 +0000 Subject: r943: change samba4 to use 'uint8_t' instead of 'unsigned char' metze (This used to be commit b5378803fdcb3b3afe7c2932a38828e83470f61a) --- source4/lib/crypto/md4.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source4/lib/crypto/md4.c') diff --git a/source4/lib/crypto/md4.c b/source4/lib/crypto/md4.c index cc8b9b6841..98fcabf224 100644 --- a/source4/lib/crypto/md4.c +++ b/source4/lib/crypto/md4.c @@ -107,7 +107,7 @@ static void mdfour64(struct mdfour_state *s, uint32_t *M) X[j] = 0; } -static void copy64(uint32_t *M, const unsigned char *in) +static void copy64(uint32_t *M, const uint8_t *in) { int i; @@ -116,7 +116,7 @@ static void copy64(uint32_t *M, const unsigned char *in) (in[i*4+1]<<8) | (in[i*4+0]<<0); } -static void copy4(unsigned char *out, uint32_t x) +static void copy4(uint8_t *out, uint32_t x) { out[0] = x&0xFF; out[1] = (x>>8)&0xFF; @@ -125,9 +125,9 @@ static void copy4(unsigned char *out, uint32_t x) } /* produce a md4 message digest from data of length n bytes */ -void mdfour(unsigned char *out, const unsigned char *in, int n) +void mdfour(uint8_t *out, const uint8_t *in, int n) { - unsigned char buf[128]; + uint8_t buf[128]; uint32_t M[16]; uint32_t b = n * 8; int i; -- cgit From c71c86c52458eefae8a34774ec186c2837f473af Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 5 Mar 2006 17:44:16 +0000 Subject: r13842: Make some more functions public. (This used to be commit aac1b99b362993352d80692afa55c38fc851c016) --- source4/lib/crypto/md4.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'source4/lib/crypto/md4.c') diff --git a/source4/lib/crypto/md4.c b/source4/lib/crypto/md4.c index 98fcabf224..5b57c57dbb 100644 --- a/source4/lib/crypto/md4.c +++ b/source4/lib/crypto/md4.c @@ -124,8 +124,10 @@ static void copy4(uint8_t *out, uint32_t x) out[3] = (x>>24)&0xFF; } -/* produce a md4 message digest from data of length n bytes */ -void mdfour(uint8_t *out, const uint8_t *in, int n) +/** + * produce a md4 message digest from data of length n bytes + */ +_PUBLIC_ void mdfour(uint8_t *out, const uint8_t *in, int n) { uint8_t buf[128]; uint32_t M[16]; -- cgit From 0479a2f1cbae51fcd8dbdc3c148c808421fb4d25 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 02:07:03 +0000 Subject: r23792: convert Samba4 to GPLv3 There are still a few tidyups of old FSF addresses to come (in both s3 and s4). More commits soon. (This used to be commit fcf38a38ac691abd0fa51b89dc951a08e89fdafa) --- source4/lib/crypto/md4.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'source4/lib/crypto/md4.c') diff --git a/source4/lib/crypto/md4.c b/source4/lib/crypto/md4.c index 5b57c57dbb..7ad93ce786 100644 --- a/source4/lib/crypto/md4.c +++ b/source4/lib/crypto/md4.c @@ -5,7 +5,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -14,8 +14,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit