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

# dkcCryptograph.c

説明を見る。

    00001
    00008 #define DKUTIL_C_CRYPTOGRAPH_C
    00009 #include "dkcCryptograph.h"
    00010
    00011
    00012
    00013
    00014
    00015
    00016 BYTE WINAPI dkcEncryptByte(BYTE a,BYTE key){
    00017     BYTE x = 0,y = 0;
    00018
    00019     x |= (BYTE)((a & 0x0f) << 4);
    00020     x |= (BYTE)((a & 0xf0) >> 4);
    00021
    00022     y |= (BYTE)((x & 0x33) << 2);
    00023     y |= (BYTE)((x & 0xCC) >> 2);
    00024
    00025     y ^= key;//0xaa;
    00026
    00027     return y;
    00028 }
    00029
    00030 BYTE WINAPI dkcDecryptByte(BYTE a,BYTE key){
    00031     BYTE x = 0,y = 0;
    00032     a ^= key;//0xaa;
    00033
    00034     x |= (BYTE)((a & 0x33) << 2);
    00035     x |= (BYTE)((a & 0xCC) >> 2);
    00036
    00037     y |= (BYTE)((x & 0x0f) << 4);
    00038     y |= (BYTE)((x & 0xf0) >> 4);
    00039
    00040     return y;
    00041 }
    00042
    00043 DKC_INLINE int WINAPI dkcSNOW2GetInitializeVector(const void *key,size_t keysize,
    00044                                                                                                         uint32 *iv3,uint32 *iv2,uint32 *iv1,uint32 *iv0)
    00045
    00046 {   int r ;//= edk_FAILED;
    00047     uint8 key160[SHA160_BIN_BUFFER_SIZE];
    00048     r = dkcSecureHashCalculateBinaryDigest(edkcSH_SHA160,key160,sizeof(key160),key,keysize);
    00049     if(DKUTIL_FAILED(r)) return r;
    00050     //todo endian check
    00056     memcpy(iv0,key160,sizeof(iv0));
    00057     memcpy(iv1,&(key160[4]),sizeof(iv1));
    00058     memcpy(iv2,&(key160[8]),sizeof(iv2));
    00059     memcpy(iv3,&(key160[12]),sizeof(iv3));
    00060
    00061     if(dkcIsBigEndian()){
    00062         *iv0 = dkcReverseEndian32(*iv0);
    00063         *iv1 = dkcReverseEndian32(*iv1);
    00064         *iv2 = dkcReverseEndian32(*iv2);
    00065         *iv3 = dkcReverseEndian32(*iv3);
    00066     }//else if(is
    00067     return r;
    00068 }
    00069
    00070 DKC_INLINE DKC_SNOW2 *WINAPI dkcAllocSNOW2NoLimitKeyLength(const void *key,size_t keysize)
    00071 {
    00072     int r ;//= edk_FAILED;
    00073
    00074     uint8 key256[SHA256_BIN_BUFFER_SIZE];
    00075
    00076     uint32 iv0=0,iv1=0,iv2=0,iv3=0;
    00077
    00078     r = dkcSNOW2GetInitializeVector(key,keysize,&iv3,&iv2,&iv1,&iv0);
    00079     if(DKUTIL_FAILED(r)) return NULL;
    00080     r = dkcSecureHashCalculateBinaryDigest(edkcSH_SHA256,key256,sizeof(key256),key,keysize);
    00081     if(DKUTIL_FAILED(r)) return NULL;
    00082
    00083
    00084     return dkcAllocSNOW2(key256,sizeof(key256),iv3,iv2,iv1,iv0);
    00085
    00086
    00087 }
    00088
    00089
    00090 DKC_INLINE DKC_HC256 *WINAPI dkcAllocHC256NoLimitKeyLength(const void *key,size_t keysize)
    00091 {
    00092     int r ;//= edk_FAILED;
    00093
    00094     uint8 key256[SHA256_BIN_BUFFER_SIZE];
    00095     uint8 iv[dkcdHC256_IV_SIZE];
    00096     r = dkcSecureHashCalculateBinaryDigest(edkcSH_SHA256,key256,sizeof(key256),key,keysize);
    00097     if(DKUTIL_FAILED(r)) return NULL;
    00098     r = dkcCalculateInitializeVector(iv,sizeof(iv),key,keysize);
    00099     if(DKUTIL_FAILED(r)) return NULL;
    00100     return dkcAllocHC256(key256,sizeof(key256),(uint32 *)iv,sizeof(iv));
    00101 }
    00102
    00103 DKC_INLINE int WINAPI dkcCalculateInitializeVector(
    00104     void *dest,size_t destsize_and_generate_iv_size,const void *key,size_t keysize)
    00105 {
    00106     uint8 *a = dest;
    00107     int r;
    00108     size_t count,rest,i,j;
    00109     DKC_SHA512 *p;
    00110     uint8 key512[SHA512_BIN_BUFFER_SIZE];
    00111     r = dkcSecureHashCalculateBinaryDigest(
    00112         edkcSH_SHA512,key512,sizeof(key512),key,keysize);
    00113     if(DKUTIL_FAILED(r)) return edk_FAILED;
    00114
    00115     p = dkcAllocSHA512();
    00116     if(NULL==p) return edk_OutOfMemory;
    00117
    00118     //初期ペンティアムは勘弁TT
    00119     count = destsize_and_generate_iv_size / SHA512_BIN_BUFFER_SIZE;
    00120     rest = destsize_and_generate_iv_size % SHA512_BIN_BUFFER_SIZE;
    00121     for(i=0;i<count;i++){
    00122         dkcSHA512Init(p);
    00123         dkcSHA512Load(p,key512,sizeof(key512));
    00124         dkcSHA512Load(p,key,keysize);
    00125         dkcSHA512Load(p,key512,sizeof(key512));
    00126         dkcSHA512FinalDigest(p,key512,sizeof(key512));
    00127         for(j=0;j<SHA512_BIN_BUFFER_SIZE;j++){
    00128             a[i] = key512[i];
    00129         }
    00130         a+=SHA512_BIN_BUFFER_SIZE;
    00131     }
    00132     if(rest){
    00133         dkcSHA512Init(p);
    00134         dkcSHA512Load(p,key512,sizeof(key512));
    00135         dkcSHA512Load(p,key,keysize);
    00136         dkcSHA512Load(p,key512,sizeof(key512));
    00137         dkcSHA512FinalDigest(p,key512,sizeof(key512));
    00138         for(j=0;j<rest;j++){
    00139             a[i] = key512[i];
    00140         }
    00141     }
    00142     dkcFreeSHA512(&p);
    00143     return edk_SUCCEEDED;
    00144 }
    00145
    00146 //**********************************************************