メインページ | アルファベット順一覧 | 構成 | ファイル一覧 | 構成メンバ | ファイルメンバ | 関連ページ

dkcMD4.c

説明を見る。
00001 
00023 #define DKUTIL_C_MD4_C
00024 
00025 
00026 #include "md_misc.h"
00027 #include "dkcMD4.h"
00028 #include "dkcStdio.h"
00029 
00030 //**********************************************************
00031 //GNETのpublic domainなMD4の実装から引用
00032 
00033 DKC_MD4 *WINAPI dkcAllocMD4(){
00034     DKC_MD4 *p = dkcAllocate(sizeof(DKC_MD4));
00035     if(NULL==p) return NULL;
00036     dkcMD4Init(p);
00037     return p;
00038 }
00039 
00040 
00041 int WINAPI dkcFreeMD4(DKC_MD4 **pp){
00042     if(NULL==pp || NULL==*pp){
00043         return edk_FAILED;
00044     }
00045     return dkcFree((void **)pp);
00046 }
00047 
00048 
00049 
00050 void WINAPI dkcMD4Init(DKC_MD4 *p){
00051     p->count[0] = p->count[1] = 0;
00052 
00053     p->abcd[0] = 0x67452301;
00054   p->abcd[1] = 0xefcdab89;
00055   p->abcd[2] = 0x98badcfe;
00056   p->abcd[3] = 0x10325476;
00057     memset(p->a8,0,sizeof(p->a8));
00058     //p->mByteOrder = (uint8)dkcGetByteOrder();
00059 }
00060 
00061 
00062 
00063 
00064 
00065 /* #define F1(x, y, z) (x & y | ~x & z) */
00066 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00067 #define F2(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
00068 #define F3(x, y, z) (x ^ y ^ z)
00069 
00070 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
00071 
00073 #define MD4STEP(f,a,b,c,d,x,s,data)\
00074     (a) += f(b,c,d) + x + data;\
00075     (a) = ROTATE_LEFT(a,s);
00076 
00077 
00078 /*
00079  * The core of the MD4 algorithm, this alters an existing MD4 hash to
00080  * reflect the addition of 16 longwords of new data.  MD4Update blocks
00081  * the data and converts bytes into longwords for this routine.
00082  */
00083 static DKC_INLINE void 
00084 MD4Transform(uint32 buf[4], uint32 const in[16])
00085 {
00086   register uint32 a, b, c, d;
00087 
00088   a = buf[0];
00089   b = buf[1];
00090   c = buf[2];
00091   d = buf[3];
00092 
00093     MD4STEP(F1,a,b,c,d, in[0], 3,0);
00094     MD4STEP(F1,d,a,b,c, in[1], 7,0);
00095     MD4STEP(F1,c,d,a,b, in[2],11,0);
00096     MD4STEP(F1,b,c,d,a, in[3],19,0);
00097     MD4STEP(F1,a,b,c,d, in[4], 3,0);
00098     MD4STEP(F1,d,a,b,c, in[5], 7,0);
00099     MD4STEP(F1,c,d,a,b, in[6],11,0);
00100     MD4STEP(F1,b,c,d,a, in[7],19,0);
00101     MD4STEP(F1,a,b,c,d, in[8], 3,0);
00102     MD4STEP(F1,d,a,b,c, in[9], 7,0);
00103     MD4STEP(F1,c,d,a,b,in[10],11,0);
00104     MD4STEP(F1,b,c,d,a,in[11],19,0);
00105     MD4STEP(F1,a,b,c,d,in[12], 3,0);
00106     MD4STEP(F1,d,a,b,c,in[13], 7,0);
00107     MD4STEP(F1,c,d,a,b,in[14],11,0);
00108     MD4STEP(F1,b,c,d,a,in[15],19,0);
00109 
00110     MD4STEP(F2,a,b,c,d, in[0], 3,0x5a827999);
00111     MD4STEP(F2,d,a,b,c, in[4], 5,0x5a827999);
00112     MD4STEP(F2,c,d,a,b, in[8], 9,0x5a827999);
00113     MD4STEP(F2,b,c,d,a,in[12],13,0x5a827999);
00114     MD4STEP(F2,a,b,c,d, in[1], 3,0x5a827999);
00115     MD4STEP(F2,d,a,b,c, in[5], 5,0x5a827999);
00116     MD4STEP(F2,c,d,a,b, in[9], 9,0x5a827999);
00117     MD4STEP(F2,b,c,d,a,in[13],13,0x5a827999);
00118     MD4STEP(F2,a,b,c,d, in[2], 3,0x5a827999);
00119     MD4STEP(F2,d,a,b,c, in[6], 5,0x5a827999);
00120     MD4STEP(F2,c,d,a,b,in[10], 9,0x5a827999);
00121     MD4STEP(F2,b,c,d,a,in[14],13,0x5a827999);
00122     MD4STEP(F2,a,b,c,d, in[3], 3,0x5a827999);
00123     MD4STEP(F2,d,a,b,c, in[7], 5,0x5a827999);
00124     MD4STEP(F2,c,d,a,b,in[11], 9,0x5a827999);
00125     MD4STEP(F2,b,c,d,a,in[15],13,0x5a827999);
00126  
00127     MD4STEP(F3,a,b,c,d, in[0], 3,0x6ed9eba1);
00128     MD4STEP(F3,d,a,b,c, in[8], 9,0x6ed9eba1);
00129     MD4STEP(F3,c,d,a,b, in[4],11,0x6ed9eba1);
00130     MD4STEP(F3,b,c,d,a,in[12],15,0x6ed9eba1);
00131     MD4STEP(F3,a,b,c,d, in[2], 3,0x6ed9eba1);
00132     MD4STEP(F3,d,a,b,c,in[10], 9,0x6ed9eba1);
00133     MD4STEP(F3,c,d,a,b, in[6],11,0x6ed9eba1);
00134     MD4STEP(F3,b,c,d,a,in[14],15,0x6ed9eba1);
00135     MD4STEP(F3,a,b,c,d, in[1], 3,0x6ed9eba1);
00136     MD4STEP(F3,d,a,b,c, in[9], 9,0x6ed9eba1);
00137     MD4STEP(F3,c,d,a,b, in[5],11,0x6ed9eba1);
00138     MD4STEP(F3,b,c,d,a,in[13],15,0x6ed9eba1);
00139     MD4STEP(F3,a,b,c,d, in[3], 3,0x6ed9eba1);
00140     MD4STEP(F3,d,a,b,c,in[11], 9,0x6ed9eba1);
00141     MD4STEP(F3,c,d,a,b, in[7],11,0x6ed9eba1);
00142     MD4STEP(F3,b,c,d,a,in[15],15,0x6ed9eba1);
00143     
00144 
00145   buf[0] += a;
00146   buf[1] += b;
00147   buf[2] += c;
00148   buf[3] += d;
00149 }
00150 
00151 #if 0
00152 
00153 /*
00154  * Update context to reflect the concatenation of another buffer full
00155  * of bytes.
00156  */
00157 static DKC_INLINE void 
00158 MD4Update(DKC_MD4 *p, uint8 const *buf, uint32 len)
00159 {
00160   uint32 t;
00161 
00162   /* Update bitcount */
00163 
00164   t = p->count[0];
00165   if ((p->count[0] = t + ((uint32) len << 3)) < t)
00166     p->count[1]++;      /* Carry from low to high */
00167   p->count[1] += len >> 29;
00168 
00169   t = (t >> 3) & 0x3f;  /* Bytes already in shsInfo->data */
00170 
00171   /* Handle any leading odd-sized chunks */
00172 
00173   if (t) 
00174     {
00175       //uint8 *p = (uint8 *) p->a8 + t;
00176             uint8 *pc = (uint8 *)&(p->a8[t]);
00177       t = 64 - t;
00178       if (len < t) 
00179         {
00180           memmove(pc, buf, len);
00181           return;
00182     }
00183       memmove(pc, buf, t);
00184       if ((p->flags) & edkcMD_ByteReverse)
00185         byteReverse(p->a8, 16);
00186       MD4Transform(p->abcd, (uint32 *) p->a8);
00187       buf += t;
00188       len -= t;
00189     }
00190   /* Process data in 64-byte chunks */
00191 
00192   while (len >= 64) 
00193     {
00194       memmove(p->a8, buf, 64);
00195       if ((p->flags) & edkcMD_ByteReverse)
00196         byteReverse(p->a8, 16);
00197       MD4Transform(p->abcd, (uint32 *) p->a8);
00198       buf += 64;
00199       len -= 64;
00200     }
00201 
00202   /* Handle any remaining bytes of data. */
00203 
00204   memmove(p->a8, buf, len);
00205 }
00206 
00207 /*
00208  * Final wrapup - pad to 64-byte boundary with the bit pattern 
00209  * 1 0* (64-bit count of bits processed, MSB-first)
00210  */
00211 static DKC_INLINE void 
00212 MD4Final( DKC_MD4 *ctx)
00213 {
00214   uint32 count;
00215   uint8 *p;
00216 
00217   /* Compute number of bytes mod 64 */
00218   count = (ctx->count[0] >> 3) & 0x3F;
00219 
00220   /* Set the first char of padding to 0x80.  This is safe since there is
00221      always at least one byte free */
00222   p = ctx->a8 + count;
00223   *p++ = 0x80;
00224 
00225   /* Bytes of padding needed to make 64 bytes */
00226   count = 64 - 1 - count;
00227 
00228   /* Pad out to 56 mod 64 */
00229   if (count < 8) 
00230     {
00231     /* Two lots of padding:  Pad the first block to 64 bytes */
00232       memset(p, 0, count);
00233       if ((ctx->flags) & edkcMD_ByteReverse)
00234         byteReverse(ctx->a8, 16);
00235       MD4Transform(ctx->abcd, (uint32 *) ctx->a8);
00236 
00237       /* Now fill the next block with 56 bytes */
00238       memset(ctx->a8, 0, 56);
00239     } 
00240   else 
00241     {
00242       /* Pad block to 56 bytes */
00243       memset(p, 0, count - 8);
00244     }
00245   if ((ctx->flags) & edkcMD_ByteReverse)
00246     byteReverse(ctx->a8, 14);
00247 
00248   /* Append length in bits and transform */
00249   ((uint32 *) ctx->a8)[14] = ctx->count[0];
00250   ((uint32 *) ctx->a8)[15] = ctx->count[1];
00251 
00252   MD4Transform(ctx->abcd, (uint32 *) ctx->a8);
00253   if ((ctx->flags) & edkcMD_ByteReverse)
00254     byteReverse((uint8 *) ctx->abcd, 4);
00255   //memmove(digest, ctx->abcd, 16);
00256   //memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */
00257 }
00258 
00259 #endif
00260 
00261 void WINAPI dkcMD4Load(DKC_MD4 *p,const BYTE *pBuffer,DWORD dwSize){
00262     if(p->flags & edkcMD_Finalized){
00263         return;
00264     }
00265     dkcMD_Update(p,pBuffer,dwSize,MD4Transform);
00266     //MD4Update(p,pBuffer,dwSize);
00267 }
00268 
00269 
00270 
00271 void WINAPI dkcMD4Final(DKC_MD4 *p){
00272     //uint8 digest[MD4_BIN_BUFFER_SIZE];
00273     if(p->flags & edkcMD_Finalized){
00274         return;
00275     }
00276     //MD4Final(p);
00277     dkcMD_Final(p,MD4Transform);
00278     //memcpy(p->a8,digest,sizeof(digest));
00279     p->flags |= edkcMD_Finalized;
00280 }
00281 
00282 int WINAPI dkcMD4Digest(DKC_MD4 *p,BYTE *buff,size_t size){
00283     
00284     if(size < MD4_BIN_BUFFER_SIZE){
00285         return edk_BufferOverFlow;
00286     }
00287     return dkc_memcpy(buff,size,p->abcd,sizeof(p->abcd));
00288 }
00289 
00290 int WINAPI dkcMD4DigestStr(DKC_MD4 *p,char *buff,size_t size){
00291     register int i;
00292     uint8 temp[MD4_BIN_BUFFER_SIZE];
00293     if(size < MD4_STR_BUFFER_SIZE){
00294         return edk_BufferOverFlow;
00295     }
00296     i = dkcMD4Digest(p,temp,sizeof(temp));
00297     if(DKUTIL_FAILED(i)){
00298         return i;
00299     }
00300     for (i=0; i<16; i++){
00301         sprintf(buff+i*2,"%02x", temp[i]);
00302     }
00303   buff[32]='\0';
00304     return edk_SUCCEEDED;
00305 }
00306 
00307 int WINAPI dkcMD4FinalDigestStr(DKC_MD4 *p,char *buff,size_t size){
00308     dkcMD4Final(p);
00309     return dkcMD4DigestStr(p,buff,size);
00310 }
00311 
00312 int WINAPI dkcMD4FinalDigest(DKC_MD4 *p,BYTE *buff,size_t size){
00313     dkcMD4Final(p);
00314     return dkcMD4Digest(p,buff,size);
00315 
00316 }
00317 

dkutil_cに対してMon Jan 16 00:39:48 2006に生成されました。  doxygen 1.4.4