lib/md4.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    a implementation of MD4 designed for use in the SMB authentication protocol
00004    Copyright (C) Andrew Tridgell 1997-1998.
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #include "includes.h"
00022 
00023 /* NOTE: This code makes no attempt to be fast! 
00024 
00025    It assumes that a int is at least 32 bits long
00026 */
00027 
00028 static uint32 A, B, C, D;
00029 
00030 static uint32 F(uint32 X, uint32 Y, uint32 Z)
00031 {
00032         return (X&Y) | ((~X)&Z);
00033 }
00034 
00035 static uint32 G(uint32 X, uint32 Y, uint32 Z)
00036 {
00037         return (X&Y) | (X&Z) | (Y&Z); 
00038 }
00039 
00040 static uint32 H(uint32 X, uint32 Y, uint32 Z)
00041 {
00042         return X^Y^Z;
00043 }
00044 
00045 static uint32 lshift(uint32 x, int s)
00046 {
00047         x &= 0xFFFFFFFF;
00048         return ((x<<s)&0xFFFFFFFF) | (x>>(32-s));
00049 }
00050 
00051 #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
00052 #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32)0x5A827999,s)
00053 #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32)0x6ED9EBA1,s)
00054 
00055 /* this applies md4 to 64 byte chunks */
00056 static void mdfour64(uint32 *M)
00057 {
00058         int j;
00059         uint32 AA, BB, CC, DD;
00060         uint32 X[16];
00061 
00062         for (j=0;j<16;j++)
00063                 X[j] = M[j];
00064 
00065         AA = A; BB = B; CC = C; DD = D;
00066 
00067         ROUND1(A,B,C,D,  0,  3);  ROUND1(D,A,B,C,  1,  7);  
00068         ROUND1(C,D,A,B,  2, 11);  ROUND1(B,C,D,A,  3, 19);
00069         ROUND1(A,B,C,D,  4,  3);  ROUND1(D,A,B,C,  5,  7);  
00070         ROUND1(C,D,A,B,  6, 11);  ROUND1(B,C,D,A,  7, 19);
00071         ROUND1(A,B,C,D,  8,  3);  ROUND1(D,A,B,C,  9,  7);  
00072         ROUND1(C,D,A,B, 10, 11);  ROUND1(B,C,D,A, 11, 19);
00073         ROUND1(A,B,C,D, 12,  3);  ROUND1(D,A,B,C, 13,  7);  
00074         ROUND1(C,D,A,B, 14, 11);  ROUND1(B,C,D,A, 15, 19);      
00075 
00076         ROUND2(A,B,C,D,  0,  3);  ROUND2(D,A,B,C,  4,  5);  
00077         ROUND2(C,D,A,B,  8,  9);  ROUND2(B,C,D,A, 12, 13);
00078         ROUND2(A,B,C,D,  1,  3);  ROUND2(D,A,B,C,  5,  5);  
00079         ROUND2(C,D,A,B,  9,  9);  ROUND2(B,C,D,A, 13, 13);
00080         ROUND2(A,B,C,D,  2,  3);  ROUND2(D,A,B,C,  6,  5);  
00081         ROUND2(C,D,A,B, 10,  9);  ROUND2(B,C,D,A, 14, 13);
00082         ROUND2(A,B,C,D,  3,  3);  ROUND2(D,A,B,C,  7,  5);  
00083         ROUND2(C,D,A,B, 11,  9);  ROUND2(B,C,D,A, 15, 13);
00084 
00085         ROUND3(A,B,C,D,  0,  3);  ROUND3(D,A,B,C,  8,  9);  
00086         ROUND3(C,D,A,B,  4, 11);  ROUND3(B,C,D,A, 12, 15);
00087         ROUND3(A,B,C,D,  2,  3);  ROUND3(D,A,B,C, 10,  9);  
00088         ROUND3(C,D,A,B,  6, 11);  ROUND3(B,C,D,A, 14, 15);
00089         ROUND3(A,B,C,D,  1,  3);  ROUND3(D,A,B,C,  9,  9);  
00090         ROUND3(C,D,A,B,  5, 11);  ROUND3(B,C,D,A, 13, 15);
00091         ROUND3(A,B,C,D,  3,  3);  ROUND3(D,A,B,C, 11,  9);  
00092         ROUND3(C,D,A,B,  7, 11);  ROUND3(B,C,D,A, 15, 15);
00093 
00094         A += AA; B += BB; C += CC; D += DD;
00095         
00096         A &= 0xFFFFFFFF; B &= 0xFFFFFFFF;
00097         C &= 0xFFFFFFFF; D &= 0xFFFFFFFF;
00098 
00099         for (j=0;j<16;j++)
00100                 X[j] = 0;
00101 }
00102 
00103 static void copy64(uint32 *M, const unsigned char *in)
00104 {
00105         int i;
00106 
00107         for (i=0;i<16;i++)
00108                 M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
00109                         (in[i*4+1]<<8) | (in[i*4+0]<<0);
00110 }
00111 
00112 static void copy4(unsigned char *out, uint32 x)
00113 {
00114         out[0] = x&0xFF;
00115         out[1] = (x>>8)&0xFF;
00116         out[2] = (x>>16)&0xFF;
00117         out[3] = (x>>24)&0xFF;
00118 }
00119 
00120 /* produce a md4 message digest from data of length n bytes */
00121 void mdfour(unsigned char *out, const unsigned char *in, int n)
00122 {
00123         unsigned char buf[128];
00124         uint32 M[16];
00125         uint32 b = n * 8;
00126         int i;
00127 
00128         A = 0x67452301;
00129         B = 0xefcdab89;
00130         C = 0x98badcfe;
00131         D = 0x10325476;
00132 
00133         while (n > 64) {
00134                 copy64(M, in);
00135                 mdfour64(M);
00136                 in += 64;
00137                 n -= 64;
00138         }
00139 
00140         for (i=0;i<128;i++)
00141                 buf[i] = 0;
00142         memcpy(buf, in, n);
00143         buf[n] = 0x80;
00144         
00145         if (n <= 55) {
00146                 copy4(buf+56, b);
00147                 copy64(M, buf);
00148                 mdfour64(M);
00149         } else {
00150                 copy4(buf+120, b); 
00151                 copy64(M, buf);
00152                 mdfour64(M);
00153                 copy64(M, buf+64);
00154                 mdfour64(M);
00155         }
00156 
00157         for (i=0;i<128;i++)
00158                 buf[i] = 0;
00159         copy64(M, buf);
00160 
00161         copy4(out, A);
00162         copy4(out+4, B);
00163         copy4(out+8, C);
00164         copy4(out+12, D);
00165 
00166         A = B = C = D = 0;
00167 }
00168 
00169 

Sambaに対してSat Aug 29 21:22:58 2009に生成されました。  doxygen 1.4.7