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

# dkcArcfour.c

RC4互換アルゴリズム arcfour [詳細]

`#include "dkcArcfour.h"`
`#include "dkcStdio.h"`

dkcArcfour.cのインクルード依存関係図

ソースコードを見る。

## マクロ定義
#define | DKUTIL_C_ARCFOUR_C
## 関数
DKC_ARCFOUR_STATE *WINAPI | dkcAllocArcfour (const unsigned char *key, size_t keylen)
DKC_INLINE unsigned char WINAPI | dkcArcfourByte (DKC_ARCFOUR_STATE *p)
static DKC_INLINE void | dkcArcfourEncrypt_Base (DKC_ARCFOUR_STATE *p, unsigned char *dest, unsigned const char *src, size_t srcsize)
int WINAPI | dkcArcfourEncrypt (DKC_ARCFOUR_STATE *p, unsigned char *dest, size_t destsize, const unsigned char *src, size_t srcsize)
void WINAPI | dkcArcfourEncryptNoDest (DKC_ARCFOUR_STATE *p, unsigned char *dest_and_src, size_t dest_and_srcsize)
int WINAPI | dkcFreeArcfour (DKC_ARCFOUR_STATE **p)
DKC_ARCFOUR2BYTE_STATE *WINAPI | dkcAllocArcfour2Byte (const unsigned char *key, size_t keylen)
DKC_INLINE unsigned short WINAPI | dkcArcfour2ByteProcess (DKC_ARCFOUR2BYTE_STATE *p)
static DKC_INLINE void | dkcArcfour2ByteEncrypt_Base (DKC_ARCFOUR2BYTE_STATE *p, unsigned short *dest, unsigned const short *src, size_t srcsize)
int WINAPI | dkcArcfour2ByteEncrypt (DKC_ARCFOUR2BYTE_STATE *p, unsigned char *dest, size_t destsize, const unsigned char *src, size_t srcsize)
int WINAPI | dkcArcfour2ByteEncryptNoDest (DKC_ARCFOUR2BYTE_STATE *p, unsigned char *dest_and_src, size_t dest_and_srcsize)
int WINAPI | dkcFreeArcfour2Byte (DKC_ARCFOUR2BYTE_STATE **p)

## 説明

RC4互換アルゴリズム arcfour

作者:
  d金魚

dkcArcfour.c で定義されています。

## マクロ定義

#define DKUTIL_C_ARCFOUR_C

| dkcArcfour.c の 7 行で定義されています。

## 関数

DKC_ARCFOUR_STATE* WINAPI dkcAllocArcfour | ( | const unsigned char * | key,
|  | size_t | keylen
| ) |

  | 引数:
  | key[in] | keyへのポインタ
  | keylen[in] | keyのサイズ
戻り値: DKC_ARCFOUR_STATE 構造体へのポインタ 覚え書き: 使い終わったらdkcFreeArcfourを使いましょう。 dkcArcfour.c の 13 行で定義されています。 参照先 dkcAllocate(), dkc_Arcfour_State::msbox, と NULL.
    00015 {
    00016
    00017     int i,j;
    00018     unsigned char temp;
    00019     unsigned char *sb1,sb2[256];
    00020     DKC_ARCFOUR_STATE *p;
    00021     if(NULL==key || 0==keylen){
    00022         return NULL;
    00023     }
    00024
    00025     memset(sb2,0,sizeof(sb2));
    00026     //allocate state struct
    00027     p = (DKC_ARCFOUR_STATE *)dkcAllocate(sizeof(DKC_ARCFOUR_STATE));
    00028     if(NULL==p){
    00029         return NULL;
    00030     }
    00031     sb1 = p->msbox;
    00032
    00033     //initialize
    00034     for(i=0;i<256;i++){
    00035         sb1[i] = (unsigned char)i;
    00036         //嗅ぎ回られないように(なんか、意味あるのか？ないナァ？
    00037         //p->msbox2[i] = key [i % keylen];
    00038     }
    00039     j=i=0;
    00040
    00041     for(;j<256;j++){
    00042         sb2[j] = key [j % keylen];
    00043     }
    00044
    00045
    00046     for(i=0;i<256;i++){
    00047         //j = (j + sb1 [i] + sb2 [i]) % 256;
    00048         j = (j + sb1[i] + sb2[i]) & 0xff;
    00049         temp = sb1 [i];
    00050         sb1 [i] = sb1 [j];
    00051         sb1 [j] = temp;
    00052         /*sb1[i] = sb1[j] - sb1[i] ;
    00053     sb1[j] -= sb1[i] ;
    00054     sb1[i] += sb1[j] ;*/
    00055     //SWAP_NUM(sb1[i],sb1[j]);
    00056     }
    00057     return p;
    00058 }

DKC_ARCFOUR2BYTE_STATE* WINAPI dkcAllocArcfour2Byte | ( | const unsigned char * | key,
|  | size_t | keylen
| ) |

| dkcArcfour.c の 160 行で定義されています。 参照先 dkcAllocate(), dkc_Arcfour2byte_State::msbox, と NULL.
    00162 {
    00163
    00164     int i,j;
    00165     unsigned short temp;
    00166     unsigned short *sb1,sb2[USHRT_MAX];
    00167     DKC_ARCFOUR2BYTE_STATE *p;
    00168     if(NULL==key || 0==keylen){
    00169         return NULL;
    00170     }
    00171
    00172     memset(sb2,0,sizeof(sb2));
    00173     //allocate state struct
    00174     p = (DKC_ARCFOUR2BYTE_STATE *)dkcAllocate(sizeof(DKC_ARCFOUR2BYTE_STATE));
    00175     if(NULL==p){
    00176         return NULL;
    00177     }
    00178     sb1 = p->msbox;
    00179
    00180     //initialize
    00181     for(i=0;i<USHRT_MAX;i++){
    00182         sb1[i] = (unsigned short)i;
    00183         //嗅ぎ回られないように(なんか、意味あるのか？ないナァ？
    00184         //p->msbox2[i] = key [i % keylen];
    00185     }
    00186     j=i=0;
    00187
    00188     for(;j<USHRT_MAX;j++){
    00189         sb2[j] = key [j % keylen];
    00190     }
    00191
    00192
    00193     for(i=0;i<USHRT_MAX;i++){
    00194         //j = (j + sb1 [i] + sb2 [i]) % 256;
    00195         j = (j + sb1[i] + sb2[i]) & 0xff;
    00196         temp = sb1 [i];
    00197         sb1 [i] = sb1 [j];
    00198         sb1 [j] = temp;
    00199         /*sb1[i] = sb1[j] - sb1[i] ;
    00200     sb1[j] -= sb1[i] ;
    00201     sb1[i] += sb1[j] ;*/
    00202     //SWAP_NUM(sb1[i],sb1[j]);
    00203     }
    00204     return p;
    00205 }

int WINAPI dkcArcfour2ByteEncrypt | ( | DKC_ARCFOUR2BYTE_STATE * | p,
|  | unsigned char * | dest,
|  | size_t | destsize,
|  | const unsigned char * | src,
|  | size_t | srcsize
| ) |

| dkcArcfour.c の 273 行で定義されています。 参照先 dkcArcfour2ByteEncrypt_Base().
    00276 {
    00277     //size_t i=0;
    00278     if(destsize < srcsize){
    00279         return edk_BufferOverFlow;
    00280     }
    00281     if(srcsize % 2 != 0){//2の倍数じゃないとねぇ
    00282         return edk_ArgumentException;
    00283     }
    00284
    00285     dkcArcfour2ByteEncrypt_Base(p,
    00286         (unsigned short *)dest,(unsigned short *)src,
    00287         srcsize / 2
    00288     );
    00289
    00290     /*for(i = 0;i < srcsize;i++){
    00291         dest[i] = (unsigned char )(src[i] ^ dkcArcfourByte(p) );
    00292     }*/
    00293
    00294     return edk_SUCCEEDED;
    00295 }

static DKC_INLINE void dkcArcfour2ByteEncrypt_Base | ( | DKC_ARCFOUR2BYTE_STATE * | p,
|  | unsigned short * | dest,
|  | unsigned const short * | src,
|  | size_t | srcsize
| ) | `[static]`

  | 引数:
  | srcsize[in] | バイト単位のサイズではなく、unsigned short単位の数
dkcArcfour.c の 233 行で定義されています。 参照先 dkc_Arcfour2byte_State::mi, dkc_Arcfour2byte_State::mj, と dkc_Arcfour2byte_State::msbox. 参照元 dkcArcfour2ByteEncrypt(), と dkcArcfour2ByteEncryptNoDest().
    00235 {
    00236
    00237     size_t cc;
    00238   unsigned short i,j;
    00239   unsigned short *sb1 = p->msbox;
    00240     unsigned short temp;
    00241
    00242      //calc
    00243     i = (unsigned short )(p->mi);
    00244     j = (unsigned short )(p->mj);
    00245
    00246     for(cc = 0;cc < srcsize;cc++){
    00247              //calc
    00248         i = (unsigned short )(i+1);
    00249         j = (unsigned short )(j + sb1[i]);
    00250
    00251          //swap
    00252
    00253         temp = sb1 [i];
    00254         sb1 [i] = sb1 [j];
    00255         sb1 [j] = temp;
    00256
    00257
    00258         //SWAP_NUM_EX(sb1[i],sb1[j],unsigned char);
    00259
    00260         //calc
    00261         dest[cc] = (unsigned short )
    00262             (src[cc] ^ (unsigned short )sb1[
    00263                 (unsigned short )(sb1 [i] + sb1 [j])
    00264             ]
    00265         );
    00266     }
    00267     //refresh
    00268     p->mi = i;
    00269     p->mj = j;
    00270
    00271 }

int WINAPI dkcArcfour2ByteEncryptNoDest | ( | DKC_ARCFOUR2BYTE_STATE * | p,
|  | unsigned char * | dest_and_src,
|  | size_t | dest_and_srcsize
| ) |

| dkcArcfour.c の 299 行で定義されています。 参照先 dkcArcfour2ByteEncrypt_Base().
    00301 {
    00302     if(dest_and_srcsize % 2 != 0){//2の倍数じゃないとねぇ
    00303         return edk_ArgumentException;
    00304     }
    00305     dkcArcfour2ByteEncrypt_Base(p,
    00306         (unsigned short *)dest_and_src,(unsigned short *)dest_and_src,
    00307         dest_and_srcsize / 2
    00308     );
    00309     return edk_SUCCEEDED;
    00310 }

DKC_INLINE unsigned short WINAPI dkcArcfour2ByteProcess | ( | DKC_ARCFOUR2BYTE_STATE * | p | ) |

| dkcArcfour.c の 207 行で定義されています。 参照先 dkc_Arcfour2byte_State::mi, dkc_Arcfour2byte_State::mj, と dkc_Arcfour2byte_State::msbox.
    00207                                                                                   {
    00208   unsigned short i,j,temp;
    00209   unsigned short *sb1 = p->msbox;
    00210
    00211   //calc
    00212   i = (unsigned short )(p->mi+1);// % 256;
    00213   j = (unsigned short )(p->mj + sb1[i]);// % 256;
    00214
    00215      //swap
    00216   temp = sb1 [i];
    00217   sb1 [i] = sb1 [j];
    00218   sb1 [j] = temp;
    00219
    00220   //SWAP_NUM(sb1[i],sb1[j]);
    00221   //refresh
    00222   p->mi = i;
    00223   p->mj = j;
    00224   //calc
    00225   i = (unsigned short )(sb1 [i] + sb1 [j]);// % 256;
    00226   j = (unsigned short )sb1[i];
    00227   return j;
    00228 }

DKC_INLINE unsigned char WINAPI dkcArcfourByte | ( | DKC_ARCFOUR_STATE * | p | ) |

  | 引数:
  | p[in][out] | dkcAllocArcfour()で確保した領域へのポインタ
戻り値: XORすべき値 dkcArcfour.c の 60 行で定義されています。 参照先 dkc_Arcfour_State::mi, dkc_Arcfour_State::mj, と dkc_Arcfour_State::msbox.
    00060                                                                     {
    00061   unsigned char i,j,temp;
    00062   unsigned char *sb1 = p->msbox;
    00063
    00064   //calc
    00065   i = (unsigned char )(p->mi+1);// % 256;
    00066   j = (unsigned char )(p->mj + sb1[i]);// % 256;
    00067
    00068      //swap
    00069   temp = sb1 [i];
    00070   sb1 [i] = sb1 [j];
    00071   sb1 [j] = temp;
    00072
    00073   //SWAP_NUM(sb1[i],sb1[j]);
    00074   //refresh
    00075   p->mi = i;
    00076   p->mj = j;
    00077   //calc
    00078   i = (unsigned char )(sb1 [i] + sb1 [j]);// % 256;
    00079   j = (unsigned char )sb1[i];
    00080   return j;
    00081 }

int WINAPI dkcArcfourEncrypt | ( | DKC_ARCFOUR_STATE * | p,
|  | unsigned char * | dest,
|  | size_t | destsize,
|  | const unsigned char * | src,
|  | size_t | srcsize
| ) |

  | 引数:
  | p[in] | dkcAllocArcfour()で確保した領域へのポインタ
  | dest[in] | 出力バッファへのポインタ
  | destsize[in] | destのサイズ
  | src[in] | 入力バッファへのポインタ
  | srcsize[in] | srcのサイズ
戻り値: edk_SUCCEEDED で成功 dkcArcfour.c の 123 行で定義されています。 参照先 dkcArcfourEncrypt_Base().
    00126 {
    00127     //size_t i=0;
    00128     if(destsize < srcsize){
    00129         return edk_BufferOverFlow;
    00130     }
    00131     dkcArcfourEncrypt_Base(p,dest,src,srcsize);
    00132
    00133     /*for(i = 0;i < srcsize;i++){
    00134         dest[i] = (unsigned char )(src[i] ^ dkcArcfourByte(p) );
    00135     }*/
    00136
    00137     return edk_SUCCEEDED;
    00138 }

static DKC_INLINE void dkcArcfourEncrypt_Base | ( | DKC_ARCFOUR_STATE * | p,
|  | unsigned char * | dest,
|  | unsigned const char * | src,
|  | size_t | srcsize
| ) | `[static]`

| dkcArcfour.c の 83 行で定義されています。 参照先 dkc_Arcfour_State::mi, dkc_Arcfour_State::mj, と dkc_Arcfour_State::msbox. 参照元 dkcArcfourEncrypt(), と dkcArcfourEncryptNoDest().
    00085 {
    00086
    00087     size_t cc;
    00088   unsigned char i,j;
    00089   unsigned char *sb1 = p->msbox;
    00090     unsigned char temp;
    00091
    00092      //calc
    00093     i = (unsigned char )(p->mi);
    00094     j = (unsigned char )(p->mj);
    00095
    00096     for(cc = 0;cc < srcsize;cc++){
    00097              //calc
    00098         i = (unsigned char )(i+1);
    00099         j = (unsigned char )(j + sb1[i]);
    00100
    00101          //swap
    00102
    00103         temp = sb1 [i];
    00104         sb1 [i] = sb1 [j];
    00105         sb1 [j] = temp;
    00106
    00107
    00108         //SWAP_NUM_EX(sb1[i],sb1[j],unsigned char);
    00109
    00110         //calc
    00111         dest[cc] = (unsigned char )
    00112             (src[cc] ^ (unsigned char )sb1[
    00113                 (unsigned char )(sb1 [i] + sb1 [j])
    00114             ]
    00115         );
    00116     }
    00117     //refresh
    00118     p->mi = i;
    00119     p->mj = j;
    00120
    00121 }

void WINAPI dkcArcfourEncryptNoDest | ( | DKC_ARCFOUR_STATE * | p,
|  | unsigned char * | dest_and_src,
|  | size_t | dest_and_srcsize
| ) |

| dkcArcfour.c の 142 行で定義されています。 参照先 dkcArcfourEncrypt_Base().
    00144 {
    00145     dkcArcfourEncrypt_Base(p,dest_and_src,dest_and_src,dest_and_srcsize);
    00146 }

int WINAPI dkcFreeArcfour | ( | DKC_ARCFOUR_STATE ** | p | ) |

  | 引数:
  | dkcAllocArcfourで確保した領域へのポインタへのポインタ |
戻り値: edk_SUCCEEDEDで成功 dkcArcfour.c の 148 行で定義されています。 参照先 dkcFree(), と NULL.
    00148                                                 {
    00149     if(NULL==p){
    00150         return edk_FAILED;
    00151     }
    00152     return dkcFree((void **)p);
    00153 }

int WINAPI dkcFreeArcfour2Byte | ( | DKC_ARCFOUR2BYTE_STATE ** | p | ) |

| dkcArcfour.c の 312 行で定義されています。 参照先 dkcFree(), と NULL.
    00312                                                           {
    00313     if(NULL==p){
    00314         return edk_FAILED;
    00315     }
    00316     return dkcFree((void **)p);
    00317 }