md4.cpp

00001 // md4.cpp - modified by Wei Dai from Andrew M. Kuchling's md4.c
00002 // The original code and all modifications are in the public domain.
00003 
00004 // This is the original introductory comment:
00005 
00006 /*
00007  *  md4.c : MD4 hash algorithm.
00008  *
00009  * Part of the Python Cryptography Toolkit, version 1.1
00010  *
00011  * Distribute and use freely; there are no restrictions on further 
00012  * dissemination and usage except those imposed by the laws of your 
00013  * country of residence.
00014  *
00015  */
00016 
00017 #include "pch.h"
00018 #include "md4.h"
00019 #include "misc.h"
00020 
00021 NAMESPACE_BEGIN(CryptoPP)
00022 
00023 void MD4::InitState(HashWordType *state)
00024 {
00025         state[0] = 0x67452301L;
00026         state[1] = 0xefcdab89L;
00027         state[2] = 0x98badcfeL;
00028         state[3] = 0x10325476L;
00029 }
00030 
00031 void MD4::Transform (word32 *digest, const word32 *in)
00032 {
00033 // #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
00034 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
00035 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
00036 #define H(x, y, z) ((x) ^ (y) ^ (z))
00037 
00038     word32 A, B, C, D;
00039 
00040         A=digest[0];
00041         B=digest[1];
00042         C=digest[2];
00043         D=digest[3];
00044 
00045 #define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+in[k],s);   
00046           function(A,B,C,D, 0, 3);
00047           function(D,A,B,C, 1, 7);
00048           function(C,D,A,B, 2,11);
00049           function(B,C,D,A, 3,19);
00050           function(A,B,C,D, 4, 3);
00051           function(D,A,B,C, 5, 7);
00052           function(C,D,A,B, 6,11);
00053           function(B,C,D,A, 7,19);
00054           function(A,B,C,D, 8, 3);
00055           function(D,A,B,C, 9, 7);
00056           function(C,D,A,B,10,11);
00057           function(B,C,D,A,11,19);
00058           function(A,B,C,D,12, 3);
00059           function(D,A,B,C,13, 7);
00060           function(C,D,A,B,14,11);
00061           function(B,C,D,A,15,19);
00062 
00063 #undef function   
00064 #define function(a,b,c,d,k,s) a=rotlFixed(a+G(b,c,d)+in[k]+0x5a827999,s);        
00065           function(A,B,C,D, 0, 3);
00066           function(D,A,B,C, 4, 5);
00067           function(C,D,A,B, 8, 9);
00068           function(B,C,D,A,12,13);
00069           function(A,B,C,D, 1, 3);
00070           function(D,A,B,C, 5, 5);
00071           function(C,D,A,B, 9, 9);
00072           function(B,C,D,A,13,13);
00073           function(A,B,C,D, 2, 3);
00074           function(D,A,B,C, 6, 5);
00075           function(C,D,A,B,10, 9);
00076           function(B,C,D,A,14,13);
00077           function(A,B,C,D, 3, 3);
00078           function(D,A,B,C, 7, 5);
00079           function(C,D,A,B,11, 9);
00080           function(B,C,D,A,15,13);
00081 
00082 #undef function  
00083 #define function(a,b,c,d,k,s) a=rotlFixed(a+H(b,c,d)+in[k]+0x6ed9eba1,s);        
00084           function(A,B,C,D, 0, 3);
00085           function(D,A,B,C, 8, 9);
00086           function(C,D,A,B, 4,11);
00087           function(B,C,D,A,12,15);
00088           function(A,B,C,D, 2, 3);
00089           function(D,A,B,C,10, 9);
00090           function(C,D,A,B, 6,11);
00091           function(B,C,D,A,14,15);
00092           function(A,B,C,D, 1, 3);
00093           function(D,A,B,C, 9, 9);
00094           function(C,D,A,B, 5,11);
00095           function(B,C,D,A,13,15);
00096           function(A,B,C,D, 3, 3);
00097           function(D,A,B,C,11, 9);
00098           function(C,D,A,B, 7,11);
00099           function(B,C,D,A,15,15);
00100 
00101         digest[0]+=A;
00102         digest[1]+=B;
00103         digest[2]+=C;
00104         digest[3]+=D;
00105 }
00106 
00107 NAMESPACE_END

Generated on Sat Dec 23 02:07:08 2006 for Crypto++ by  doxygen 1.5.1-p1