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

# dkcTelecomConfirmPacket.c

説明を見る。

    00001
    00008 #define  DKUTIL_C_TELECOM_CONFIRM_PACKET_C
    00009 #include "dkcTelecomConfirmPacket.h"
    00010
    00011 #if 0
    00012
    00013 #include "dkcMath.h"
    00014 #include "dkcLZW.h"
    00015
    00016 static DKC_INLINE ULONG all_add(uint8 *a,size_t size){
    00017     size_t i=0;
    00018     ULONG t=0;
    00019     for(;i<size;i++){
    00020         t += a[i];
    00021     }
    00022     return t;
    00023 }
    00024
    00025
    00026 DKC_TELECOM_CONFIRM_PACKET* WINAPI dkcAllocTelecomConfirmPacketAuto(const void *key,size_t keysize)
    00027 {
    00028     return dkcAllocTelecomConfirmPacket(dkcdTELECOM_CONFIRM_PACKET_DEFAULT_PAUSE_SIZE,
    00029         key,keysize,
    00030         edk_SNOW2_SIGNATURE,
    00031         FALSE
    00032     );
    00033
    00034 }
    00035
    00036
    00037 //DKC_TELECOM_CONFIRM_PACKET* WINAPI dkcAllocTelecomConfirmPacket(size_t pause_size,const void *key,size_t keysize,BOOL isNaturalKey)
    00038
    00039 //DKC_TELECOM_CONFIRM_PACKET* WINAPI dkcAllocTelecomConfirmPacket(size_t pause_size,UINT flag,const void *key,size_t keysize,BOOL isNaturalKey)
    00040 DKC_EXTERN DKC_TELECOM_CONFIRM_PACKET* WINAPI dkcAllocTelecomConfirmPacket(size_t pause_size,const void *key,size_t keysize,UINT flag,BOOL isNaturalKey)
    00041 {
    00042     DKC_TELECOM_CONFIRM_PACKET* p = dkcAllocate(sizeof(DKC_TELECOM_CONFIRM_PACKET));
    00043     int r;
    00044     if(NULL==p) return NULL;
    00045
    00046     p->pause_size = pause_size;
    00047     //p->pkey = NULL;
    00048     //p->keysize = keysize;
    00049     p->flag = flag;
    00050     p->psig = dkcAllocSHA512();
    00051     if(NULL==p->psig) goto Error;
    00052
    00053
    00054     dkcSHA512Init(p->psig);
    00055     dkcSHA512Load(p->psig,key,keysize);
    00056
    00057     r = dkcSHA512FinalDigest(p->psig,p->key512,sizeof(p->key512));
    00058     if(DKUTIL_FAILED(r)) goto Error;
    00059
    00060     if(edk_ARCFOUR_SIGNATURE & flag){
    00061         //todo endian check
    00062         if(isNaturalKey){
    00063             p->crypt_obj = dkcAllocArcfour(key,keysize);
    00064         }else{
    00065             p->crypt_obj = dkcAllocArcfour(p->key512,sizeof(p->key512));
    00066         }
    00067     }else if(edk_HC256_SIGNATURE & flag){
    00068         //todo endian check
    00069         if(isNaturalKey){
    00070             p->crypt_obj = dkcAllocHC256NoLimitKeyLength(key,keysize);
    00071         }else{
    00072             p->crypt_obj = dkcAllocHC256NoLimitKeyLength(p->key512,sizeof(p->key512));
    00073         }
    00074     }else if(edk_SNOW20_SIGNATURE & flag){
    00075         if(isNaturalKey){
    00076             p->crypt_obj = dkcAllocSNOW2NoLimitKeyLength(key,keysize);
    00077         }else{
    00078             p->crypt_obj = dkcAllocSNOW2NoLimitKeyLength(p->key512,sizeof(p->key512));
    00079         }
    00080     }else if(edk_VERNAM_SIGNATURE & flag){
    00081         dkcmNOT_ASSERT("NO IMPL");
    00082     }
    00083
    00084
    00085 /*
    00086
    00087
    00088     r = dkcSecureHashCalculateBinaryDigest(edkcSH_SHA160,p->key160,sizeof(p->key160),key,keysize);
    00089     if(DKUTIL_FAILED(r)) goto Error;
    00090     */
    00091
    00092
    00093     dkcSrand(&(p->seed),all_add(p->key512,sizeof(p->key512)));
    00094
    00095
    00096     p->isNaturalKey = (uint8)(isNaturalKey != 0);
    00097     return p;
    00098
    00099 Error:
    00100     dkcFreeSHA512(&(p->psig));//NULLでは動かない
    00101     dkcFree(&p);
    00102     return NULL;
    00103 }
    00104
    00105 int WINAPI dkcFreeTelecomConfirmPacket(DKC_TELECOM_CONFIRM_PACKET **p){
    00106     if(edk_ARCFOUR_SIGNATURE & (*p)->flag){
    00107         dkcFreeArcfour((DKC_ARCFOUR_STATE **)&((*p)->crypt_obj));
    00108     }else if(edk_HC256_SIGNATURE & (*p)->flag){
    00109         dkcFreeHC256((DKC_HC256**)&((*p)->crypt_obj));
    00110     }else if(edk_SNOW20_SIGNATURE & (*p)->flag){
    00111         dkcFreeSNOW2((DKC_SNOW2**)&((*p)->crypt_obj));
    00112     }else if(edk_VERNAM_SIGNATURE & (*p)->flag){
    00113         dkcmNOT_ASSERT("NO IMPL");
    00114     }
    00115
    00116     dkcFreeSHA512(&((*p)->psig));
    00117     return dkcFree(p);
    00118 }
    00119
    00120
    00122 static void copy_tcp_header(void *dest,DKC_TELECOM_CONFIRM_PACKET_HEADER *a){
    00123     DKC_TELECOM_CONFIRM_PACKET_HEADER *p = dest;
    00124     memcpy( p->sig,a->sig,sizeof(p->sig));
    00125     if(dkcIsBigEndian()){
    00126         p->length = dkcReverseEndian32(a->length);
    00127     }else{
    00128         p->length = a->length;
    00129     }
    00130 }
    00131
    00133 static void copy_lzw_header(void *dest,const DKC_LZW_HEADER *a){
    00134     DKC_LZW_HEADER *p = dest;
    00135
    00136     if(dkcIsBigEndian()){
    00137         p->mSignature = dkcReverseEndian32(a->mSignature);
    00138         p->mOriginSize = dkcReverseEndian32(a->mOriginSize);
    00139         p->mCompressedSize = dkcReverseEndian32(a->mCompressedSize);
    00140         p->option = dkcReverseEndian32(a->option);
    00141     }else{
    00142         memcpy(p,a,sizeof(*p));
    00143     }
    00144 }
    00145
    00146 static DKC_INLINE int tcp_encode_pause(DKC_TELECOM_CONFIRM_PACKET *p,
    00147                                                                                  uint8 *dest,size_t destsize,const uint8 *src,size_t srcsize)
    00148 {
    00149     int r;
    00150     DKC_TELECOM_CONFIRM_PACKET_HEADER header;
    00151     DKC_LZW_HEADER hlzw;    //save lzw state
    00152
    00153     size_t tt = sizeof(header) + sizeof(hlzw);
    00154
    00155     //errorcheck routine
    00156     if(dkcdTELECOM_CONFIRM_PACKET_MAX_LENGTH < srcsize + tt){
    00157         return edk_ArgumentException;
    00158     }
    00159     if(destsize < tt){
    00160         return edk_BufferOverFlow;
    00161     }
    00162
    00163     //header routine
    00164
    00165     //別に&する必要ないかも＾＾；
    00166     header.length = srcsize & dkcdTELECOM_CONFIRM_PACKET_MAX_LENGTH;
    00167     //もっといい方法があるはずだな。だけどisNaturalKeyは常に1とは限らないので･･･ todo debuggerでチェック
    00168     header.length |= (p->isNaturalKey) ? (1<<31) : 0;
    00169
    00170
    00171     dkcSHA512Init(p->psig);
    00172     dkcSHA512Load(p->psig,src,srcsize);
    00173     r = dkcSHA512FinalDigest(p->psig,header.sig,sizeof(header.sig));
    00174     if(DKUTIL_FAILED(r)) return r;
    00175
    00176     copy_tcp_header(dest,&header);
    00177     //compression process
    00178
    00179
    00180     {
    00181         DKC_LZW *plzw;
    00182
    00183         uint8 *dest_t;
    00184         size_t dest_size_t;
    00185
    00186
    00187
    00188
    00189         dest_t = dest + tt;
    00190         dest_size_t = destsize - tt;
    00191         plzw = dkcAllocLZW(p->pause_size);
    00192         if(NULL==plzw){
    00193             r = edk_FAILED;
    00194             goto Error;
    00195         }
    00196
    00197         r = dkcLZWEncode(plzw,&hlzw,dest_t,dest_size_t,src,srcsize,dest_size_t,
    00198             dkcRand(&(p->seed)),//notice
    00199             edkcLZW_Default);
    00200
    00201         dkcFreeLZW(&plzw);
    00202
    00203         if(DKUTIL_FAILED(r)) goto Error;
    00204
    00205         //headerを入れる。
    00206         dest_t = dest + sizeof(header);
    00207         copy_lzw_header(dest_t,&hlzw);
    00208
    00209
    00210
    00211     }//end of lzw
    00212
    00213
    00214     //crypt process
    00215     {
    00216         uint8 *dest_t = dest + tt;
    00217         size_t dest_size_t = hlzw.mCompressedSize ;
    00218         if(edk_ARCFOUR_SIGNATURE & p->flag){
    00219             dkcArcfourEncryptNoDest(p->crypt_obj,dest_t,dest_size_t);
    00220         }else if(edk_HC256_SIGNATURE & p->flag){
    00221             dkcHC256EncryptNoDestDOE(p->crypt_obj,dest_t,dest_size_t);
    00222         }else if(edk_SNOW20_SIGNATURE & p->flag){
    00223             dkcSNOW2EncryptNoDestDOE(p->crypt_obj,dest_t,dest_size_t);
    00224         }else if(edk_VERNAM_SIGNATURE & p->flag){
    00225             dkcmNOT_ASSERT("NO IMPL");
    00226         }
    00227
    00228
    00229     }//end of crypt process
    00230
    00231 Error:
    00232         return r;
    00233 }
    00234
    00235 int WINAPI dkcTelecomConfirmPacketEncode(DKC_TELECOM_CONFIRM_PACKET *p,
    00236                                                                                  uint8 *dest,size_t destsize,const uint8 *src,size_t srcsize)
    00237 {
    00238     size_t count = srcsize / p->pause_size;
    00239     size_t rest = srcsize % p->pause_size,i;
    00240     int r;size_t offset = 0;
    00241     for(i=0;i<count;i++){
    00242         offset = count * p->pause_size;
    00243         r = tcp_encode_pause(p,dest,destsize,src,srcsize);
    00244         if(DKUTIL_FAILED(r)) return r;
    00245     }
    00246
    00247 }
    00248
    00249 static DKC_INLINE void get_lzw_header(DKC_LZW_HEADER *p,const uint8 *src){
    00250     DKC_LZW_HEADER *a = (DKC_LZW_HEADER *)((uint8 *)src + sizeof(DKC_LZW_HEADER));
    00251     if(dkcIsBigEndian()){
    00252         p->mSignature = dkcReverseEndian32(a->mSignature);
    00253         p->mOriginSize = dkcReverseEndian32(a->mOriginSize);
    00254         p->mCompressedSize = dkcReverseEndian32(a->mCompressedSize);
    00255         p->option = dkcReverseEndian32(a->option);
    00256     }else{
    00257         memcpy(p,a,sizeof(*p));
    00258     }
    00259 }
    00260 size_t WINAPI dkcGetOutputSize(const uint8 *src){
    00261     DKC_LZW_HEADER h;
    00262     get_lzw_header(&h,src);
    00263     return h.mOriginSize;
    00264 }
    00265
    00266
    00267 int WINAPI dkcTelecomConfirmPacketDecode(DKC_TELECOM_CONFIRM_PACKET *p,uint8 *dest,size_t destsize,const uint8 *src,size_t srcsize)
    00268 {
    00269     int r;
    00270     DKC_TELECOM_CONFIRM_PACKET_HEADER header;
    00271     DKC_LZW_HEADER hlzw;    //save lzw state
    00272
    00273     size_t tt = sizeof(header) + sizeof(hlzw);
    00274
    00275     //errorcheck routine
    00276     if(dkcdTELECOM_CONFIRM_PACKET_MAX_LENGTH < srcsize + tt){
    00277         return edk_ArgumentException;
    00278     }
    00279
    00280
    00281     //header routine
    00282
    00283
    00284     copy_tcp_header((void *)&header,(DKC_TELECOM_CONFIRM_PACKET_HEADER *)src);
    00285     get_lzw_header(&hlzw,src);
    00286     if(hlzw.mOriginSize < destsize){
    00287         return edk_BufferOverFlow;
    00288     }
    00289     //compression process
    00290
    00291
    00292     {
    00293         DKC_LZW *plzw;
    00294
    00295         uint8 *src_t;
    00296         size_t src_size_t;
    00297
    00298
    00299
    00300
    00301
    00302         src_t = (uint8 *)src + tt;
    00303         src_size_t = srcsize - tt;
    00304         plzw = dkcAllocLZW(p->pause_size );
    00305         if(NULL==plzw){
    00306             r = edk_FAILED;
    00307             goto Error;
    00308         }
    00309
    00310         r = dkcLZWDecode(plzw,&hlzw,dest,destsize,src_t,src_size_t,
    00311             dkcRand(&(p->seed)));
    00312
    00313         dkcFreeLZW(&plzw);
    00314
    00315         if(DKUTIL_FAILED(r)) goto Error;
    00316
    00317
    00318
    00319     }//end of lzw
    00320
    00321
    00322     //crypt process
    00323     {
    00324         uint8 *dest_t = dest;
    00325         size_t dest_size_t = hlzw.mOriginSize ;
    00326
    00327         if(edk_ARCFOUR_SIGNATURE & p->flag){
    00328             dkcArcfourEncryptNoDest(p->crypt_obj,dest_t,dest_size_t);
    00329         }else if(edk_HC256_SIGNATURE & p->flag){
    00330             dkcHC256EncryptNoDestDOE(p->crypt_obj,dest_t,dest_size_t);
    00331         }else if(edk_SNOW20_SIGNATURE & p->flag){
    00332             dkcSNOW2EncryptNoDestDOE(p->crypt_obj,dest_t,dest_size_t);
    00333         }else if(edk_VERNAM_SIGNATURE & p->flag){
    00334             dkcmNOT_ASSERT("NO IMPL");
    00335         }
    00336
    00337
    00338     }//end of crypt process
    00339
    00340 Error:
    00341         return r;
    00342 }
    00343
    00344 #endif