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

# dkcHash.c

説明を見る。

    00001
    00008 #include "dkcHash.h"
    00009 #include "dkcStdio.h"
    00010
    00011
    00012 void WINAPI dkcSHO_MD5Init(DKC_SECURE_HASH_OBJECT *p){
    00013
    00014
    00015     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Init;
    00016     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Load;
    00017     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Final;
    00018     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcMD5DigestStr;
    00019     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Digest;
    00020
    00021     p->digest_string_size = MD5_STR_BUFFER_SIZE;
    00022     p->digest_binary_size = MD5_BIN_BUFFER_SIZE;
    00023
    00024     p->mObj = dkcAllocMD5();
    00025 }
    00026
    00027
    00028 void WINAPI dkcSHO_SHA1Init(DKC_SECURE_HASH_OBJECT *p){
    00029
    00030     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Init;
    00031     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Load;
    00032     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Final;
    00033     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1DigestStr;
    00034     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Digest;
    00035
    00036     p->digest_string_size = SHA1_STR_BUFFER_SIZE;
    00037     p->digest_binary_size = SHA1_BIN_BUFFER_SIZE;
    00038
    00039     p->mObj = dkcAllocSHA1();
    00040 }
    00041
    00042
    00043 void WINAPI dkcSHO_SHA256Init(DKC_SECURE_HASH_OBJECT *p){
    00044
    00045     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Init;
    00046     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Load;
    00047     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Final;
    00048     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256DigestStr;
    00049     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Digest;
    00050
    00051     p->digest_string_size = SHA256_STR_BUFFER_SIZE;
    00052     p->digest_binary_size = SHA256_BIN_BUFFER_SIZE;
    00053
    00054     p->mObj = dkcAllocSHA256();
    00055 }
    00056
    00057
    00058 void WINAPI dkcSHO_SHA384Init(DKC_SECURE_HASH_OBJECT *p){
    00059
    00060     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Init;
    00061     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Load;
    00062     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Final;
    00063     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384DigestStr;
    00064     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Digest;
    00065
    00066     p->digest_string_size = SHA384_STR_BUFFER_SIZE;
    00067     p->digest_binary_size = SHA384_BIN_BUFFER_SIZE;
    00068
    00069     p->mObj = dkcAllocSHA384();
    00070 }
    00071
    00072
    00074 void WINAPI dkcSHO_SHA512Init(DKC_SECURE_HASH_OBJECT *p){
    00075
    00076     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Init;
    00077     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Load;
    00078     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Final;
    00079     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512DigestStr;
    00080     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Digest;
    00081
    00082     p->digest_string_size = SHA512_STR_BUFFER_SIZE;
    00083     p->digest_binary_size = SHA512_BIN_BUFFER_SIZE;
    00084
    00085     p->mObj = dkcAllocSHA512();
    00086
    00087 }
    00088 static DKC_INLINE void check_init(){
    00089     dkcmFORCE_NOT_ASSERT(sizeof(void *) != sizeof(DKC_SHA1 *));
    00090 }
    00091
    00092 #define check_() dkcmNOT_ASSERT(sizeof(void *) != sizeof(DKC_SHA1 *));
    00093
    00094 typedef void (WINAPI *DKC_SHO_HASH_INIT_F_TYPE)(DKC_SECURE_HASH_OBJECT *);
    00095
    00096
    00097 DKC_SECURE_HASH_OBJECT *WINAPI dkcAllocSHO(UINT option)
    00098 {
    00099     DKC_SECURE_HASH_OBJECT *p;
    00100     DKC_SHO_HASH_INIT_F_TYPE init_func;
    00101
    00102     check_init();
    00103     p = dkcAllocate(sizeof(DKC_SECURE_HASH_OBJECT));
    00104     if(NULL==p){
    00105         return NULL;
    00106     }
    00107
    00108     {
    00109         switch(option)
    00110         {
    00111         case edkcSH_MD5:
    00112             init_func = dkcSHO_MD5Init;
    00113             break;
    00114         case edkcSH_SHA1:
    00115             init_func = dkcSHO_SHA1Init;
    00116             break;
    00117         case edkcSH_SHA256:
    00118             init_func = dkcSHO_SHA256Init;
    00119             break;
    00120         case edkcSH_SHA384:
    00121             init_func = dkcSHO_SHA384Init;
    00122             break;
    00123         case edkcSH_SHA512:
    00124             init_func = dkcSHO_SHA512Init;
    00125             break;
    00126         default:
    00127             goto Error;
    00128         }
    00129     }
    00130
    00131     //各ハッシュに応じた初期化
    00132     init_func(p);
    00133     if(NULL==p->mObj){
    00134         goto Error;
    00135     }
    00136     p->mOption = option;
    00137     //dkcAllocate()はmemset(0)
    00138     //p->mInited = 0;
    00139
    00140
    00141     return p;
    00142 Error:
    00143     dkcFreeSHO(&p);
    00144     return NULL;
    00145 }
    00146
    00147 int WINAPI dkcFreeSHO(DKC_SECURE_HASH_OBJECT **pp)
    00148 {
    00149     DKC_SECURE_HASH_OBJECT *p = *pp;
    00150     int option;
    00151     void **obj;
    00152
    00153     if(NULL==pp || NULL==p){
    00154         return edk_FAILED;
    00155     }
    00156     option = p->mOption;
    00157     obj = &(p->mObj);
    00158
    00159     switch(option)
    00160     {
    00161     case edkcSH_MD5:
    00162         dkcFreeMD5((DKC_MD5 **)obj);
    00163         break;
    00164     case edkcSH_SHA1:
    00165         dkcFreeSHA1((DKC_SHA1 **)obj);
    00166         break;
    00167     case edkcSH_SHA256:
    00168         dkcFreeSHA256((DKC_SHA256 **)obj);
    00169         break;
    00170     case edkcSH_SHA384:
    00171         dkcFreeSHA384((DKC_SHA384 **)obj);
    00172         break;
    00173     case edkcSH_SHA512:
    00174         dkcFreeSHA512((DKC_SHA512 **)obj);
    00175         break;
    00176     //default:
    00177
    00178
    00179     }//eos
    00180
    00181     return dkcFree(pp);
    00182 }
    00183
    00184 void WINAPI dkcSHOInit(DKC_SECURE_HASH_OBJECT *p)
    00185 {
    00186     DKC_SHO_INIT_F_TYPE init = (DKC_SHO_INIT_F_TYPE)p->Init;
    00187     init(p->mObj);
    00188
    00189     p->mInited = TRUE;
    00190 }
    00191
    00192 void WINAPI dkcSHOLoad(DKC_SECURE_HASH_OBJECT *p,const BYTE *pBuffer,DWORD dwSize)
    00193 {
    00194     DKC_SHO_LOAD_F_TYPE load = (DKC_SHO_LOAD_F_TYPE)p->Load;
    00195     check_();
    00196     if(FALSE == p->mInited){
    00197         dkcSHOInit(p);
    00198     }
    00199     load(p->mObj,pBuffer,dwSize);
    00200 }
    00201
    00202
    00203
    00204 void WINAPI dkcSHOFinal(DKC_SECURE_HASH_OBJECT *p){
    00205     DKC_SHO_FINAL_F_TYPE final = (DKC_SHO_FINAL_F_TYPE)p->Final;
    00206     check_();
    00207     final(p->mObj);
    00208 }
    00209
    00210 int WINAPI dkcSHODigestStr(DKC_SECURE_HASH_OBJECT *p,char *buff,size_t size)
    00211 {
    00212     DKC_SHO_DIGESTSTR_F_TYPE digeststr = (DKC_SHO_DIGESTSTR_F_TYPE)p->DigestStr;
    00213     check_();
    00214     return digeststr(p->mObj,buff,size);
    00215 }
    00216
    00217
    00218 int WINAPI dkcSHODigest(DKC_SECURE_HASH_OBJECT *p,BYTE *buff,size_t size)
    00219 {
    00220     DKC_SHO_DIGEST_F_TYPE digest = (DKC_SHO_DIGEST_F_TYPE)p->Digest;
    00221     check_();
    00222     return digest(p->mObj,buff,size);
    00223 }
    00224
    00225
    00226
    00227
    00228 static DKC_INLINE int shc(
    00229     UINT hash_option,void *digest_dest,size_t destsize,
    00230     const BYTE *data,size_t data_size,dkctBOOL isBinary
    00231     )
    00232 {
    00233     int r = edk_FAILED;
    00234     DKC_SECURE_HASH_OBJECT *p = dkcAllocSHO(hash_option);
    00235     if(NULL==p){
    00236         goto Error;
    00237     }
    00238
    00239     dkcSHOLoad(p,data,data_size);
    00240
    00241     dkcSHOFinal(p);
    00242
    00243     if(isBinary){
    00244         r = dkcSHODigest(p,digest_dest,destsize);
    00245     }else{//false
    00246         r = dkcSHODigestStr(p,digest_dest,destsize);
    00247     }
    00248     if(DKUTIL_FAILED(r)){
    00249         goto Error;
    00250     }
    00251     r = edk_SUCCEEDED;
    00252
    00253 Error:
    00254     dkcFreeSHO(&p);
    00255     return r;
    00256 }
    00257
    00258 int WINAPI dkcSecureHashCalculateBinaryDigest(
    00259     UINT hash_option,BYTE *digest_dest,size_t destsize,
    00260     const BYTE *data,size_t data_size
    00261     ){
    00262
    00263     return shc(hash_option,digest_dest,destsize,data,data_size,TRUE);
    00264 }
    00265
    00267 int WINAPI dkcSecureHashCalculateStringDigest(
    00268     UINT hash_option,char *digest_dest,size_t destsize,
    00269     const BYTE *data,size_t data_size
    00270 ){
    00271
    00272     return shc(hash_option,digest_dest,destsize,data,data_size,FALSE);
    00273 }
    00274
    00275 //**********************************************************
    00276
    00277
    00278 uint8 dkcHash8(uint8 *pd,size_t cycle){
    00279     uint32 v = 0;
    00280     size_t i;
    00281     for(i=0;i<cycle;i++){
    00282         v ^= (pd[i] << 3) + v;
    00283     }
    00284     return (uint8)((v % dkcdUNSIGNED_8BIT_PRIME_NUM) & 0xFF);
    00285 }
    00286
    00287 uint16 dkcHash16(uint8 *pd,size_t cycle){
    00288     uint32 v = 0;
    00289     size_t i;
    00290     for(i=0;i<cycle;i++){
    00291         v = (pd[i] << 3) + v;
    00292     }
    00293     return (uint16)((v % dkcdUNSIGNED_16BIT_PRIME_NUM) & 0xFFFF);
    00294 }
    00295
    00296 uint32 dkcHash24(uint8 *pd,size_t cycle){
    00297     uint32 v = 0;
    00298     size_t i;
    00299     for(i=0;i<cycle;i++){
    00300         v ^= (pd[i] << 3) + v;
    00301     }
    00302     return (v % dkcdUNSIGNED_16BIT_PRIME_NUM) & 0xFFFFFF;
    00303 }