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

# dkcDCF01.c

説明を見る。

    00001
    00013 #define DKUTIL_C_DCF01_C
    00014 #include "dkcDCF01.h"
    00015 #include "dkcGenericFileSystem.h"
    00016 #include "dkcSJISFileSystem.h"
    00017 #include "dkcCryptograph.h"
    00018
    00019
    00020
    00021
    00022 DKC_INLINE DKC_DCF *WINAPI dkcAllocDCF_Version01_SNOW2(
    00023     DKC_SNOW2 *attach_cp,DKC_STREAM *attach_sp)
    00024 {
    00025     DKC_DCF *p;
    00026     if(NULL==attach_cp || NULL==attach_sp) return NULL;
    00027     if(!(attach_sp->mMode & edkcStreamInitFile64))
    00028     {
    00029         return NULL;
    00030     }
    00031     if(0!=dkcStreamTell(attach_sp)){
    00032         return NULL;
    00033     }
    00034     p = dkcAllocate(sizeof(DKC_DCF));
    00035     if(NULL==p) return NULL;
    00036
    00037     p->CryptObj = attach_cp;
    00038     p->StreamObj = attach_sp;
    00039     p->SHAObj = dkcAllocSHA512();
    00040     if(NULL==p->SHAObj){
    00041         goto Error;
    00042     }
    00043     p->BufferObj = dkcAllocBuffer(NULL,1024 * 64);
    00044     if(NULL==p->BufferObj){
    00045         goto Error;
    00046     }
    00047     dkcDCF01Init(p);
    00048     return p;
    00049 Error:
    00050     dkcFreeSHA512(&(p->SHAObj));
    00051     dkcFree(&p);
    00052     return NULL;
    00053 }
    00054
    00055
    00056
    00057 DKC_DCF *WINAPI dkcAllocDCF01(const char *filename,uint32 stream_flags,const void *key,size_t keysize)
    00058 {
    00059     DKC_DCF *p;
    00060     DKC_STREAM *ps;
    00061     DKC_SNOW2 *psnow2;
    00062
    00063     ps = dkcAllocStreamFile64Type(edkcStreamWin32PromoteFlag,filename,stream_flags);
    00064     if(NULL==ps) return NULL;
    00065     psnow2 = dkcAllocSNOW2NoLimitKeyLength(key,keysize);
    00066     if(NULL==psnow2) goto Error;
    00067
    00068     p = dkcAllocDCF_Version01_SNOW2(psnow2,ps);
    00069     if(NULL==p) goto Error;
    00070     return p;
    00071 Error:
    00072     //dkcFreeDCF01(&p);
    00073     dkcFreeSNOW2(&psnow2);
    00074     dkcFreeStream(&ps);
    00075     return NULL;
    00076 }
    00077
    00078
    00079 int WINAPI dkcFreeDCF01(DKC_DCF **pp)
    00080 {
    00081     DKC_DCF *p = *pp;
    00082     if(NULL==pp || NULL==*pp) return edk_FAILED;
    00083     dkcFreeBuffer(&(p->BufferObj));
    00084     dkcFreeSHA512(&(p->SHAObj));
    00085     return dkcFree(pp);
    00086 }
    00087
    00088 void WINAPI dkcDCF01Init(DKC_DCF *p)
    00089 {
    00090     dkcSHA512Init(p->SHAObj);
    00091     dkcStreamSeek(p->StreamObj,
    00092         SHA512_BIN_BUFFER_SIZE + sizeof(uint64),edkcStreamSeekSet
    00093     );
    00094     p->Count = 0;
    00095
    00096
    00097     p->fpInit = dkcDCF01Init;
    00098     p->fpWrite = dkcDCF01Write;
    00099     p->fpWriteFinal = dkcDCF01WriteFinal;
    00100     p->fpRead = dkcDCF01Read;
    00101     p->fpReadFinal = dkcDCF01ReadFinal;
    00102
    00103
    00104     p->fpFree = (DKC_DCF_FREE_F_TYPE)dkcFreeDCF01;
    00105
    00106     p->fpFileToDCF = dkcFileToDCF01;
    00107     p->fpDCFToFile = dkcDCF01ToFile;
    00108 }
    00109
    00110 DKC_INLINE int WINAPI dkcDCF01Write(DKC_DCF *p,const void *data,size_t size,size_t *write_size)
    00111 {
    00112     int r;
    00113     size_t offset,i;
    00114     size_t pSize = p->BufferObj->mSize;
    00115     size_t div =  size / pSize;
    00116     size_t rest = size % pSize;
    00117     uint8 *pBuff = p->BufferObj->mBuff;
    00118     const uint8 *pData = data;
    00119
    00120
    00121     dkcSHA512Load(p->SHAObj,data,size);
    00122
    00123     offset = 0;
    00124     for(i=0;i<div;i++){
    00125         memcpy(pBuff,pData + offset,pSize);
    00126         r = dkcSNOW2EncryptNoDestDOE(p->CryptObj,pBuff,pSize);
    00127         if(DKUTIL_FAILED(r)) return r;
    00128         r = dkcStreamWriteWithWriteSize(p->StreamObj,pBuff,pSize,write_size);
    00129         if(DKUTIL_FAILED(r)) return r;
    00130         if(pSize != *write_size) return edk_FAILED;
    00131         offset += pSize;
    00132         //p->Count += (*write_size);
    00133     }
    00134     memcpy(pBuff,pData + offset,rest);
    00135     r = dkcSNOW2EncryptNoDestDOE(p->CryptObj,pBuff,rest);
    00136     if(DKUTIL_FAILED(r)) return r;
    00137     r = dkcStreamWriteWithWriteSize(p->StreamObj,pBuff,rest,write_size);
    00138     if(DKUTIL_FAILED(r)) return r;
    00139     if(rest != *write_size) return edk_FAILED;
    00140
    00141     //p->Count += (*write_size);
    00142     p->Count += size;
    00143     return r;
    00144 }
    00145
    00146
    00150 DKC_INLINE int WINAPI dkcDCF01WriteFinal(DKC_DCF *p){
    00151     int r;
    00152     uint8 sha512[SHA512_BIN_BUFFER_SIZE];
    00153     r = dkcSHA512FinalDigest(p->SHAObj,sha512,sizeof(sha512));
    00154     if(DKUTIL_FAILED(r)) return r;
    00155     dkcStreamSeek(p->StreamObj,0,edkcStreamSeekSet);
    00156     r = dkcStreamWrite(p->StreamObj,sha512,sizeof(sha512));
    00157     if(DKUTIL_FAILED(r)) return r;
    00158
    00159     r = dkcStreamWrite(p->StreamObj,&(p->Count),sizeof(p->Count));
    00160     return r;
    00161 }
    00162
    00163 DKC_INLINE int WINAPI dkcDCF01Read(DKC_DCF *p,void *buff,size_t size,size_t *readsize)
    00164 {
    00165     int r;
    00166     *readsize = 0;
    00167     r = dkcStreamRead(p->StreamObj,buff,size,readsize);
    00168     if(DKUTIL_FAILED(r)) return r;
    00169     r = dkcSNOW2DecryptNoDestDOE(p->CryptObj,buff,*readsize);
    00170     if(DKUTIL_FAILED(r)) return r;
    00171     dkcSHA512Load(p->SHAObj,buff,*readsize);
    00172     p->Count += (*readsize);
    00173
    00174     if(dkcStreamEOF(p->StreamObj)/* || 0==*readsize*/){
    00175         return edk_EndProcess;
    00176     }
    00177     return r;
    00178
    00179 }
    00180
    00181
    00185 DKC_INLINE int WINAPI dkcDCF01ReadFinal(DKC_DCF *p)
    00186 {
    00187     uint64 datasize;
    00188     uint8 sha512[SHA512_BIN_BUFFER_SIZE],fsig[SHA512_BIN_BUFFER_SIZE];
    00189     int r;
    00190     size_t readsize;
    00191     r = dkcStreamSeek(p->StreamObj,0,edkcStreamSeekSet);
    00192     if(DKUTIL_FAILED(r)) return r;
    00193     r = dkcStreamRead(p->StreamObj,fsig,sizeof(fsig),&readsize);
    00194     if(DKUTIL_FAILED(r)) return r;
    00195     if(readsize != sizeof(fsig)) return edk_FAILED;
    00196     r = dkcStreamRead(p->StreamObj,&datasize,sizeof(datasize),&readsize);
    00197     if(DKUTIL_FAILED(r)) return r;
    00198     if(readsize != sizeof(datasize)) return edk_FAILED;
    00199     if(p->Count != datasize){
    00200         return edk_SignatureException;
    00201     }
    00202     r = dkcSHA512FinalDigest(p->SHAObj,sha512,sizeof(sha512));
    00203     if(DKUTIL_FAILED(r)) return r;
    00204     if(0 != memcmp(sha512,fsig,sizeof(sha512))) return edk_SignatureException;
    00205
    00206     return edk_SUCCEEDED;
    00207 }
    00208
    00209
    00211 int WINAPI dkcFileToDCF01(const char *save_filename,const char *target_filename,
    00212                      const void *key,size_t keysize)
    00213 {
    00214     DKC_STREAM *ps = NULL,*reader = NULL;
    00215     DKC_SNOW2 *psnow2 = NULL;
    00216     DKC_DCF *p = NULL;
    00217     //DWORD high,low;
    00218     size_t readsize,writesize;
    00219     uint8 tempbuff[1024];
    00220     int r = edk_FAILED;
    00221
    00222     ps = dkcAllocStreamFile64Type(edkcStreamWin32PromoteFlag,save_filename,edkcWriteMode);
    00223     if(NULL==ps) return edk_FAILED;
    00224     psnow2 = dkcAllocSNOW2NoLimitKeyLength(key,keysize);
    00225     if(NULL==psnow2) goto Error;
    00226
    00227     p = dkcAllocDCF_Version01_SNOW2(psnow2,ps);
    00228     if(NULL==p) goto Error;
    00229
    00230
    00231
    00232     //if(FALSE==dkcFileSize64(target_filename,&high,&low)) goto Error;
    00233     //printf("%d bytes\n",(high << 32) + low)   ;
    00234
    00235     reader = dkcAllocStreamFile64Type(edkcStreamWin32PromoteFlag,target_filename,edkcReadMode);
    00236     if(NULL==reader) goto Error;
    00237
    00238     while(FALSE==dkcStreamEOF(reader))
    00239     {
    00240         dkcStreamRead(reader,tempbuff,sizeof(tempbuff),&readsize);
    00241         r = dkcDCF01Write(p,tempbuff,readsize,&writesize);
    00242         if(DKUTIL_FAILED(r)) goto Error;
    00243     }
    00244     r = dkcDCF01WriteFinal(p);
    00245     if(DKUTIL_FAILED(r)) goto Error;
    00246
    00247     dkcDCF01Init(p);
    00248
    00249     r = edk_SUCCEEDED;
    00250 Error:
    00251     dkcFreeDCF01(&p);
    00252     dkcFreeSNOW2(&psnow2);
    00253     dkcFreeStream(&ps);
    00254     dkcFreeStream(&reader);
    00255     return r;
    00256 }
    00257
    00259 int WINAPI dkcDCF01ToFile(const char *dest_filename,const char *target_filename,
    00260                      const void *key,size_t keysize)
    00261 {
    00262     DKC_STREAM *ps = NULL,*writer = NULL;
    00263     DKC_SNOW2 *psnow2 = NULL;
    00264     DKC_DCF *p = NULL;
    00265     //DWORD high,low;
    00266     size_t readsize;
    00267     uint8 tempbuff[1024];
    00268     int r = edk_FAILED;
    00269
    00270     ps = dkcAllocStreamFile64Type(edkcStreamWin32PromoteFlag,target_filename,edkcReadMode);
    00271     if(NULL==ps) return edk_FAILED;
    00272     psnow2 = dkcAllocSNOW2NoLimitKeyLength(key,keysize);
    00273     if(NULL==psnow2) goto Error;
    00274
    00275     p = dkcAllocDCF_Version01_SNOW2(psnow2,ps);
    00276     if(NULL==p) goto Error;
    00277
    00278
    00279
    00280     //if(FALSE==dkcFileSize64(target_filename,&high,&low) ) goto Error;
    00281
    00282     writer = dkcAllocStreamFile64Type(edkcStreamWin32PromoteFlag,dest_filename,edkcWriteMode);
    00283     if(NULL==writer) goto Error;
    00284
    00285     do
    00286     {
    00287         r = dkcDCF01Read(p,tempbuff,sizeof(tempbuff),&readsize);
    00288
    00289         if(DKUTIL_FAILED(r) && (r != edk_EndProcess))
    00290         {
    00291             goto Error;
    00292         }
    00293         if(dkcStreamWrite(writer,tempbuff,readsize))
    00294             goto Error;
    00295
    00296     }while(edk_EndProcess!=r);
    00297
    00298     r = dkcDCF01ReadFinal(p);
    00299     if(DKUTIL_FAILED(r)) goto Error;
    00300
    00301     dkcDCF01Init(p);
    00302
    00303     r = edk_SUCCEEDED;
    00304 Error:
    00305     dkcFreeDCF01(&p);
    00306     dkcFreeSNOW2(&psnow2);
    00307     dkcFreeStream(&ps);
    00308     dkcFreeStream(&writer);
    00309     return r;
    00310 }
    00311
    00312
    00313 #if 0
    00314
    00315 BOOL WINAPI dkcDCF01PasswordCheck(const char *filename,const void *key,size_t keysize)
    00316 {
    00317     DKC_STREAM *ps = NULL;
    00318     DKC_SNOW2 *psnow2 = NULL;
    00319     DKC_DCF *p = NULL;
    00320     //DWORD high,low;
    00321     size_t readsize;
    00322     uint8 tempbuff[1024];
    00323     BOOL r = FALSE;
    00324
    00325
    00326     ps = dkcAllocStreamFile64Type(edkcStreamWin32PromoteFlag,filename,edkcReadMode);
    00327     if(NULL==ps) return FALSE;
    00328     psnow2 = dkcAllocSNOW2NoLimitKeyLength(key,keysize);
    00329     if(NULL==psnow2) goto Error;
    00330
    00331     p = dkcAllocDCF_Version01_SNOW2(psnow2,ps);
    00332     if(NULL==p) goto Error;
    00333
    00334     do
    00335     {
    00336         r = dkcDCFRead(p,tempbuff,sizeof(tempbuff),&readsize);
    00337
    00338         if(DKUTIL_FAILED(r) && (r != edk_EndProcess))
    00339         {
    00340             goto Error;
    00341         }
    00342
    00343     }while(edk_EndProcess!=r);
    00344
    00345     r = dkcDCFReadFinal(p);
    00346     if(DKUTIL_FAILED(r)) goto Error;
    00347
    00348     r = TRUE;
    00349 Error:
    00350     dkcFreeDCF(&p);
    00351     dkcFreeSNOW2(&psnow2);
    00352     dkcFreeStream(&ps);
    00353     return r;
    00354 }
    00355
    00356 #endif