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

# dkcHMAC.c

説明を見る。

    00001
    00009 #include "dkcHMAC.h"
    00010 #include "dkcStdio.h"
    00011
    00012
    00013 #define dkcdHMAC_IMPL_STRING 0
    00014
    00015 /*
    00016 void WINAPI dkcHMAC_MD5Init(DKC_HMAC *p){
    00017
    00018
    00019     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Init;
    00020     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Load;
    00021     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Final;
    00022     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcMD5DigestStr;
    00023     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcMD5Digest;
    00024     p->ipad_init = 0x36;
    00025     p->opad_init = 0x5c;
    00026     p->digest_string_size = MD5_STR_BUFFER_SIZE;
    00027     p->digest_binary_size = MD5_BIN_BUFFER_SIZE;
    00028
    00029     p->mObj = dkcAllocMD5();
    00030 }
    00031
    00032
    00033 void WINAPI dkcHMAC_SHA1Init(DKC_HMAC *p){
    00034
    00035     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Init;
    00036     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Load;
    00037     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Final;
    00038     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1DigestStr;
    00039     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA1Digest;
    00040     p->ipad_init = 0x36;
    00041     p->opad_init = 0x5c;
    00042     p->digest_string_size = SHA1_STR_BUFFER_SIZE;
    00043     p->digest_binary_size = SHA1_BIN_BUFFER_SIZE;
    00044
    00045     p->mObj = dkcAllocSHA1();
    00046 }
    00047
    00048
    00049 void WINAPI dkcHMAC_SHA256Init(DKC_HMAC *p){
    00050
    00051     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Init;
    00052     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Load;
    00053     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Final;
    00054     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256DigestStr;
    00055     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA256Digest;
    00056     p->ipad_init = 0x36;
    00057     p->opad_init = 0x5c;
    00058     p->digest_string_size = SHA256_STR_BUFFER_SIZE;
    00059     p->digest_binary_size = SHA256_BIN_BUFFER_SIZE;
    00060
    00061     p->mObj = dkcAllocSHA256();
    00062 }
    00063
    00064
    00065 void WINAPI dkcHMAC_SHA384Init(DKC_HMAC *p){
    00066
    00067     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Init;
    00068     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Load;
    00069     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Final;
    00070     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384DigestStr;
    00071     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA384Digest;
    00072
    00073     p->digest_string_size = SHA384_STR_BUFFER_SIZE;
    00074     p->digest_binary_size = SHA384_BIN_BUFFER_SIZE;
    00075
    00076     p->mObj = dkcAllocSHA384();
    00077 }
    00078
    00079
    00081 void WINAPI dkcHMAC_SHA512Init(DKC_HMAC *p){
    00082
    00083     p->Init = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Init;
    00084     p->Load = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Load;
    00085     p->Final = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Final;
    00086     p->DigestStr = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512DigestStr;
    00087     p->Digest = (DKC_WINAPI_PROC_F_TYPE)dkcSHA512Digest;
    00088     p->ipad_init = 0x36;
    00089     p->opad_init = 0x5c;
    00090     p->digest_string_size = SHA512_STR_BUFFER_SIZE;
    00091     p->digest_binary_size = SHA512_BIN_BUFFER_SIZE;
    00092
    00093     p->mObj = dkcAllocSHA512();
    00094
    00095 }
    00096 */
    00097 DKC_HMAC *WINAPI dkcAllocHMAC(UINT option)
    00098 {
    00099     DKC_HMAC *p;
    00100
    00101     size_t padsize ;
    00102
    00103     //check_init();
    00104
    00105     p = dkcAllocate(sizeof(DKC_HMAC));
    00106     if(NULL==p){
    00107         return NULL;
    00108     }
    00109
    00110     p->sho = dkcAllocSHO(option);
    00111     if(NULL==p->sho){
    00112         goto Error;
    00113     }
    00114
    00115
    00116
    00117     //初期化した値から必要なパッドを確保
    00118     p->pad_size = p->sho->digest_binary_size * 4;
    00119
    00120     padsize = p->pad_size + 1;// + 1は文字列なので･･･
    00121
    00122     p->ipad_init = 0x36;
    00123     p->opad_init = 0x5c;
    00124
    00125     p->ipad = malloc(   padsize );
    00126     if(NULL==p->ipad){
    00127         goto Error;
    00128     }
    00129     p->opad = malloc( padsize );
    00130     if(NULL==p->opad){
    00131         goto Error;
    00132     }
    00133
    00134
    00135     return p;
    00136 Error:
    00137     dkcFreeHMAC(&p);
    00138     return NULL;
    00139 }
    00140
    00141 int WINAPI dkcFreeHMAC(DKC_HMAC **pp)
    00142 {
    00143     DKC_HMAC *p = *pp;
    00144
    00145     if(NULL==pp || NULL==p){
    00146         return edk_FAILED;
    00147     }
    00148
    00149     dkcFreeSHO(&(p->sho));
    00150
    00151     if(p->ipad){
    00152         free(p->ipad);
    00153     }
    00154     if(p->opad){
    00155         free(p->opad);
    00156     }
    00157     return dkcFree(pp);
    00158 }
    00159
    00160 void WINAPI dkcHMACInit(DKC_HMAC *p,const BYTE *key,size_t key_length)
    00161 {
    00162
    00163     size_t padsize = p->pad_size;
    00164
    00165     if (TRUE==p->mInnerHashKeyed)
    00166     {
    00167         //中身のハッシュ値を初期化
    00168         dkcSHOInit(p->sho);
    00169         //成功＾＾ 中身はないよフラグをする。
    00170         p->mInnerHashKeyed = FALSE;
    00171     }
    00172
    00173
    00174
    00175     //パッド作り
    00176     if(key_length <= padsize)
    00177     {
    00178         memcpy(p->ipad,key,key_length);
    00179     }
    00180     else
    00181     {
    00182
    00183 #if dkcdHMAC_IMPL_STRING //文字列版
    00184         dkcSecureHashCalculateStringDigest(
    00185             p->sho->mOption,
    00186             (char *)p->ipad,p->pad_size,
    00187             key,key_length
    00188         );
    00189         //update key_length
    00190         key_length = p->sho->digest_string_size;
    00191         dkcmASSERT(key_length <= p->sho->digest_string_size);
    00192
    00193 #else //binary版
    00194     dkcSecureHashCalculateBinaryDigest(
    00195             p->sho->mOption,
    00196             p->ipad,p->pad_size,
    00197             key,key_length
    00198         );
    00199         //update key_length
    00200         key_length = p->sho->digest_binary_size;
    00201         dkcmASSERT(key_length <= p->sho->digest_binary_size);
    00202 #endif
    00203     }
    00204
    00205
    00206     dkcmASSERT(key_length <= padsize);
    00207     //バッファの埋め合わせ
    00208     memset(p->ipad + key_length, 0, padsize - key_length);
    00209
    00210     //pad初期化
    00211     {
    00212         BYTE *ipad,*opad,iv,ov;
    00213         size_t i;
    00214
    00215         ipad = p->ipad;
    00216         opad = p->opad;
    00217         iv = p->ipad_init;
    00218         ov = p->opad_init;
    00219
    00220         for (i=0; i<padsize; i++)
    00221         {
    00222
    00223             opad[i] = (BYTE)(ipad[i] ^ ov);
    00224             ipad[i] ^= iv;
    00225         }
    00226
    00227     }
    00228
    00229     p->mInited = 1;
    00230
    00231 }
    00232
    00234 void DKC_INLINE WINAPI dkcHMACKeyInner(DKC_HMAC *p)
    00235 {
    00236     dkcmASSERT(!p->mInnerHashKeyed);
    00237     dkcSHOLoad(p->sho,p->ipad,p->pad_size);
    00238
    00239     p->mInnerHashKeyed = TRUE;
    00240 }
    00241
    00242
    00243 void WINAPI dkcHMACLoad(DKC_HMAC *p,const BYTE *pBuffer,DWORD dwSize)
    00244 {
    00245     dkcmASSERT(1==p->mInited);
    00246
    00247     if(FALSE == p->mInnerHashKeyed){
    00248         dkcHMACKeyInner(p);
    00249     }
    00250     dkcSHOLoad(p->sho,pBuffer,dwSize);
    00251 }
    00252
    00253
    00254
    00255 int WINAPI dkcHMACFinal(DKC_HMAC *p){
    00256     //ThrowIfInvalidTruncatedSize(size);
    00257
    00258 #if dkcdHMAC_IMPL_STRING
    00259
    00260     size_t tempsize = p->pad_size;
    00261     char *temp = malloc(tempsize);
    00262
    00263 #else
    00264     size_t tempsize = p->pad_size / 4;
    00265     char *temp = malloc(tempsize);
    00266
    00267 #endif
    00268     DKC_SECURE_HASH_OBJECT *pt = p->sho;
    00269     dkcmNOT_ASSERT(FALSE==p->mInited);
    00270
    00271     if(NULL==temp){
    00272         return edk_FAILED;
    00273     }
    00274     if(FALSE == p->mInnerHashKeyed){
    00275         dkcHMACKeyInner(p);
    00276     }
    00277
    00278     //End of inner process
    00279     dkcSHOFinal(pt);
    00280 #if dkcdHMAC_IMPL_STRING
    00281     dkcSHODigestStr(pt,temp,tempsize);
    00282 #else
    00283     dkcSHODigest(pt,(BYTE *)temp,tempsize);
    00284 #endif
    00285
    00286     //begin outer process
    00287     dkcSHOInit(pt);
    00288     dkcSHOLoad(pt,p->opad,p->pad_size);
    00289     dkcSHOLoad(pt,(const BYTE *)temp,tempsize);
    00290
    00291     dkcSHOFinal(pt);
    00292
    00293     //ココバグっているかも？
    00294     //p->mInnerHashKeyed = FALSE;
    00295     //state を 2にする
    00296     p->mInited = 2;
    00297
    00298     free(temp);
    00299     return edk_SUCCEEDED;
    00300 }
    00301
    00302 int WINAPI dkcHMACDigestStr(DKC_HMAC *p,char *buff,size_t size)
    00303 {
    00304     dkcmASSERT(2==p->mInited);
    00305     return dkcSHODigestStr(p->sho,buff,size);
    00306 }
    00307
    00308
    00309 int WINAPI dkcHMACDigest(DKC_HMAC *p,BYTE *buff,size_t size)
    00310 {
    00311     dkcmASSERT(2==p->mInited);
    00312     return dkcSHODigest(p->sho,buff,size);
    00313 }
    00314
    00315