メインページ | 構成 | ファイル一覧 | 構成メンバ | ファイルメンバ | 関連ページ

dkcBlowfish.c

Blowfish encryption algorithm converted by d金魚. [詳細]

#include "dkcBlowfish.h"

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

Include dependency graph

ソースコードを見る。

マクロ定義

#define S(SBox_, x, i)   (SBox_[i][x.w.byte##i])
#define bf_F(SBox_, x)   (((S(SBox_,x,0) + S(SBox_,x,1)) ^ S(SBox_,x,2)) + S(SBox_,x,3))
#define ROUND(PArray_, SBox_, a, b, n)   (a.dword ^= bf_F(SBox_,b) ^ PArray_[n])

関数

DKC_BLOWFISH *WINAPI dkcAllocBlowfish (BYTE *key, int keysize)
int WINAPI dkcFreeBlowfish (DKC_BLOWFISH **p)
 dkcAllocBlowfish()で確保したメモリ領域を開放

void Blowfish_encipher (DKC_BLOWFISH *p, DWORD *xl, DWORD *xr)
void Blowfish_decipher (DKC_BLOWFISH *p, DWORD *xl, DWORD *xr)
int WINAPI dkcBlowfishInit (DKC_BLOWFISH *p, BYTE *key, int keybytes)
 dkcAllocBlowfish()で初期化するけど、明示的に初期化したい場合これを呼び出す。

DWORD WINAPI dkcBlowfishGetOutputLength (DWORD lInputLong)
DWORD WINAPI dkcBlowfishEncrypt (DKC_BLOWFISH *p, BYTE *pInput, BYTE *pOutput, DWORD lSize)
void WINAPI dkcBlowfishDecrypt (DKC_BLOWFISH *p, BYTE *pInput, BYTE *pOutput, DWORD lSize)

変数

const DWORD bf_P [NPASS+2]
const DWORD bf_S [4][256]


説明

Blowfish encryption algorithm converted by d金魚.

参照:
dkcBlowfish.h

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


マクロ定義

#define bf_F SBox_,
 )     (((S(SBox_,x,0) + S(SBox_,x,1)) ^ S(SBox_,x,2)) + S(SBox_,x,3))
 

dkcBlowfish.c18 行で定義されています。

#define ROUND PArray_,
SBox_,
a,
b,
 )     (a.dword ^= bf_F(SBox_,b) ^ PArray_[n])
 

dkcBlowfish.c19 行で定義されています。

参照元 Blowfish_decipher(), と Blowfish_encipher().

#define S SBox_,
x,
 )     (SBox_[i][x.w.byte##i])
 

dkcBlowfish.c17 行で定義されています。


関数

void Blowfish_decipher DKC_BLOWFISH p,
DWORD *  xl,
DWORD *  xr
[static]
 

dkcBlowfish.c350 行で定義されています。

参照先 DKC_BLOWFISH, aword::dword, dkc_Blowfish::PArray, ROUND, と dkc_Blowfish::SBoxes.

参照元 dkcBlowfishDecrypt().

00351 {
00352    union aword  Xl ;
00353    union aword  Xr ;
00354 
00355    Xl.dword = *xl ;
00356    Xr.dword = *xr ;
00357 
00358    Xl.dword ^= p->PArray [17] ;
00359    ROUND (p->PArray,p->SBoxes,Xr, Xl, 16) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 15) ;
00360    ROUND (p->PArray,p->SBoxes,Xr, Xl, 14) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 13) ;
00361    ROUND (p->PArray,p->SBoxes,Xr, Xl, 12) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 11) ;
00362    ROUND (p->PArray,p->SBoxes,Xr, Xl, 10) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 9) ;
00363    ROUND (p->PArray,p->SBoxes,Xr, Xl, 8) ;   ROUND (p->PArray,p->SBoxes,Xl, Xr, 7) ;
00364    ROUND (p->PArray,p->SBoxes,Xr, Xl, 6) ;   ROUND (p->PArray,p->SBoxes,Xl, Xr, 5) ;
00365    ROUND (p->PArray,p->SBoxes,Xr, Xl, 4) ;   ROUND (p->PArray,p->SBoxes,Xl, Xr, 3) ;
00366    ROUND (p->PArray,p->SBoxes,Xr, Xl, 2) ;   ROUND (p->PArray,p->SBoxes,Xl, Xr, 1) ;
00367    Xr.dword ^= p->PArray[0];
00368 
00369    *xl = Xr.dword;
00370    *xr = Xl.dword;
00371 }

void Blowfish_encipher DKC_BLOWFISH p,
DWORD *  xl,
DWORD *  xr
[static]
 

dkcBlowfish.c327 行で定義されています。

参照先 DKC_BLOWFISH, aword::dword, dkc_Blowfish::PArray, ROUND, と dkc_Blowfish::SBoxes.

参照元 dkcBlowfishEncrypt(), と dkcBlowfishInit().

00328 {
00329     union aword  Xl, Xr ;
00330 
00331     Xl.dword = *xl ;
00332     Xr.dword = *xr ;
00333 
00334     Xl.dword ^= p->PArray [0];
00335     ROUND (p->PArray,p->SBoxes,Xr, Xl, 1) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 2) ;
00336     ROUND (p->PArray,p->SBoxes,Xr, Xl, 3) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 4) ;
00337     ROUND (p->PArray,p->SBoxes,Xr, Xl, 5) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 6) ;
00338     ROUND (p->PArray,p->SBoxes,Xr, Xl, 7) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 8) ;
00339     ROUND (p->PArray,p->SBoxes,Xr, Xl, 9) ;  ROUND (p->PArray,p->SBoxes,Xl, Xr, 10) ;
00340     ROUND (p->PArray,p->SBoxes,Xr, Xl, 11) ; ROUND (p->PArray,p->SBoxes,Xl, Xr, 12) ;
00341     ROUND (p->PArray,p->SBoxes,Xr, Xl, 13) ; ROUND (p->PArray,p->SBoxes,Xl, Xr, 14) ;
00342     ROUND (p->PArray,p->SBoxes,Xr, Xl, 15) ; ROUND (p->PArray,p->SBoxes,Xl, Xr, 16) ;
00343     Xr.dword ^= p->PArray [17] ;
00344 
00345     *xr = Xl.dword ;
00346     *xl = Xr.dword ;
00347 }

DKC_BLOWFISH* WINAPI dkcAllocBlowfish BYTE key,
int  keysize
 

引数:
key[in] キーへのポインタ
keysize[in] keyのサイズ
覚え書き:
dkcd_BLOWFISH_MAX_KEY_SIZEよりキーが大きい場合は失敗します。 内部でdkcBlowfishInit()を呼び出しています。

dkcBlowfish.c289 行で定義されています。

参照先 BYTE, DKC_BLOWFISH, DKC_BLOWFISH_SBOX_TYPE, dkcAllocate(), dkcBlowfishInit(), dkcFreeBlowfish(), DKUTIL_FAILED, DWORD, NULL, dkc_Blowfish::PArray, と dkc_Blowfish::SBoxes.

00289                                                             {
00290     DKC_BLOWFISH *p = (DKC_BLOWFISH *)dkcAllocate(sizeof(DKC_BLOWFISH));
00291     if(NULL==p){
00292         return NULL;
00293     }
00294     p->PArray = (DWORD *)malloc( sizeof(DWORD) * 18) ;
00295     if(NULL==p->PArray){
00296         goto Error;
00297     }
00298     p->SBoxes = (DKC_BLOWFISH_SBOX_TYPE)malloc( sizeof(DWORD) * 4 * 256) ;
00299     if(NULL==p->SBoxes){
00300         goto Error;
00301     }
00302     if(DKUTIL_FAILED(dkcBlowfishInit(p,key,keysize))){
00303         goto Error;
00304     }
00305     return p;
00306 Error:
00307     dkcFreeBlowfish(&p);
00308     return NULL;
00309 }

void WINAPI dkcBlowfishDecrypt DKC_BLOWFISH p,
BYTE pInput,
BYTE pOutput,
DWORD  lSize
 

Decode pIntput into pOutput. Input length in lSize. Inputbuffer and output buffer can be the same, but be sure buffer length is even MOD8.

dkcBlowfish.c504 行で定義されています。

参照先 Blowfish_decipher(), BYTE, DKC_BLOWFISH, と DWORD.

00505 {
00506     DWORD   lCount ;
00507     BYTE    *pi, *po ;
00508     int     i ;
00509     int     SameDest = (pInput == pOutput ? 1 : 0) ;
00510 
00511     for (lCount = 0 ; lCount < lSize ; lCount += 8)
00512     {
00513         if (SameDest)   // if encoded data is being written into inputbuffer
00514         {
00515             Blowfish_decipher (p,(DWORD *) pInput,
00516                 (DWORD *) (pInput + 4)) ;
00517             pInput += 8 ;
00518         }
00519         else            // output buffer not equal to inputbuffer
00520         {               // so copy input to output before decoding
00521             pi = pInput ;
00522             po = pOutput ;
00523             for (i = 0 ; i < 8 ; i++)
00524                 *po++ = *pi++ ;
00525             Blowfish_decipher (p,(DWORD *) pOutput,
00526                 (DWORD *) (pOutput + 4)) ;
00527             pInput += 8 ;
00528             pOutput += 8 ;
00529         }
00530     }
00531 }

DWORD WINAPI dkcBlowfishEncrypt DKC_BLOWFISH p,
BYTE pInput,
BYTE pOutput,
DWORD  lSize
 

Encode pIntput into pOutput. Input length in lSize. Returned value is length of output which will be even MOD 8 bytes. Inputbuffer and output buffer can be the same, but be sure buffer length is even MOD8.

dkcBlowfish.c445 行で定義されています。

参照先 Blowfish_encipher(), BYTE, DKC_BLOWFISH, dkcBlowfishGetOutputLength(), と DWORD.

00446 {
00447     DWORD   lCount, lOutSize, lGoodBytes ;
00448     BYTE    *pi, *po ;
00449     int     i, j ;
00450     int     SameDest = (pInput == pOutput ? 1 : 0) ;
00451 
00452     lOutSize = dkcBlowfishGetOutputLength (lSize) ;
00453     for (lCount = 0 ; lCount < lOutSize ; lCount += 8)
00454     {
00455         if (SameDest)   // if encoded data is being written into inputbuffer
00456         {
00457             if (lCount < lSize - 7) // if not dealing with unevenbytes at end
00458             {
00459                 Blowfish_encipher (p,(DWORD *) pInput,
00460                     (DWORD *) (pInput + 4)) ;
00461             }
00462             else        // pad end of data with null bytes tocomplete encryption
00463             {
00464                 po = pInput + lSize ;   // point at bytepast theend of actual data
00465                 j = (int) (lOutSize - lSize) ;  // number ofbytes to set to null
00466                 for (i = 0 ; i < j ; i++)
00467                     *po++ = 0 ;
00468                 Blowfish_encipher (p,(DWORD *) pInput,
00469                     (DWORD *) (pInput + 4)) ;
00470             }
00471             pInput += 8 ;
00472         }
00473         else            // output buffer not equal to inputbuffer, so must copy
00474         {               // input to output buffer prior to encrypting
00475             if (lCount < lSize - 7) // if not dealing with unevenbytes at end
00476             {
00477                 pi = pInput ;
00478                 po = pOutput ;
00479                 for (i = 0 ; i < 8 ; i++)
00480 // copy bytes to output
00481                     *po++ = *pi++ ;
00482                 Blowfish_encipher (p,(DWORD *) pOutput, // nowencrypt them
00483                     (DWORD *) (pOutput + 4)) ;
00484             }
00485             else        // pad end of data with null bytes tocomplete encryption
00486             {
00487                 lGoodBytes = lSize - lCount ;   // number ofremaining data bytes
00488                 po = pOutput ;
00489                 for (i = 0 ; i < (int) lGoodBytes ; i++)
00490                     *po++ = *pInput++ ;
00491                 for (j = i ; j < 8 ; j++)
00492                     *po++ = 0 ;
00493                 Blowfish_encipher (p,(DWORD *) pOutput,
00494                     (DWORD *) (pOutput + 4)) ;
00495             }
00496             pInput += 8 ;
00497             pOutput += 8 ;
00498         }
00499     }
00500     return lOutSize ;
00501  }

DWORD WINAPI dkcBlowfishGetOutputLength DWORD  lInputLong  ) 
 

dkcBlowfish.c432 行で定義されています。

参照先 DWORD.

参照元 dkcBlowfishEncrypt().

00433 {
00434     DWORD   lVal ;
00435 
00436     lVal = lInputLong % 8 ; // find out if uneven number of bytes at
00437 //the end
00438     if (lVal != 0)
00439         return lInputLong + 8 - lVal ;
00440     else
00441         return lInputLong ;
00442 }

int WINAPI dkcBlowfishInit DKC_BLOWFISH p,
BYTE key,
int  keybytes
 

dkcAllocBlowfish()で初期化するけど、明示的に初期化したい場合これを呼び出す。

覚え書き:
  • dkcd_BLOWFISH_MAX_KEY_SIZEよりキーが大きい場合は失敗します。
  • この関数の使用は奨励されていません。
  • この関数の名前は変更する場合があります。
戻り値:
edk_SUCCEEDEDなら成功

dkcBlowfish.c374 行で定義されています。

参照先 bf_P, bf_S, Blowfish_encipher(), BYTE, DKC_BLOWFISH, dkcd_BLOWFISH_MAX_KEY_SIZE, DWORD, aword::dword, edk_FAILED, edk_SUCCEEDED, NPASS, dkc_Blowfish::PArray, dkc_Blowfish::SBoxes, と aword::w.

参照元 dkcAllocBlowfish().

00375 {
00376     
00377     int         i, j ;
00378     DWORD       data, datal, datar ;
00379     union aword temp ;
00380 
00381     if(dkcd_BLOWFISH_MAX_KEY_SIZE < keybytes){
00382         return edk_FAILED;
00383     }
00384 
00385     // first fill arrays from data tables
00386     for (i = 0 ; i < 18 ; i++)
00387         p->PArray[i] = bf_P [i] ;
00388 
00389     for (i = 0 ; i < 4 ; i++)
00390     {
00391         for (j = 0 ; j < 256 ; j++)
00392             p->SBoxes [i][j] = bf_S [i][j] ;
00393     }
00394 
00395 
00396     j = 0 ;
00397     for (i = 0 ; i < NPASS + 2 ; ++i)
00398     {
00399         temp.dword = 0 ;
00400         temp.w.byte0 = key[j];
00401         temp.w.byte1 = key[(j+1) % keybytes] ;
00402         temp.w.byte2 = key[(j+2) % keybytes] ;
00403         temp.w.byte3 = key[(j+3) % keybytes] ;
00404         data = temp.dword ;
00405         p->PArray[i] ^= data ;
00406         j = (j + 4) % keybytes ;
00407     }
00408 
00409     datal = 0 ;
00410     datar = 0 ;
00411 
00412     for (i = 0 ; i < NPASS + 2 ; i += 2)
00413     {
00414         Blowfish_encipher (p,&datal, &datar) ;
00415         p->PArray[i] = datal ;
00416         p->PArray[i + 1] = datar ;
00417     }
00418 
00419     for (i = 0 ; i < 4 ; ++i)
00420     {
00421         for (j = 0 ; j < 256 ; j += 2)
00422         {
00423           Blowfish_encipher (p,&datal, &datar) ;
00424           p->SBoxes [i][j] = datal ;
00425           p->SBoxes [i][j + 1] = datar ;
00426         }
00427     }
00428 
00429     return edk_SUCCEEDED;
00430 }

int WINAPI dkcFreeBlowfish DKC_BLOWFISH **  p  ) 
 

dkcAllocBlowfish()で確保したメモリ領域を開放

dkcBlowfish.c312 行で定義されています。

参照先 DKC_BLOWFISH, dkcFree(), edk_FAILED, と NULL.

参照元 dkcAllocBlowfish().

00312                                             {
00313     if(NULL==p){
00314         return edk_FAILED;
00315     }
00316     if((*p)->SBoxes){
00317         free((*p)->SBoxes);
00318     }
00319     if((*p)->PArray){
00320         free((*p)->PArray);
00321     }
00322     return dkcFree(p);
00323 }


変数

const DWORD bf_P[NPASS + 2] [static]
 

初期値:

 {
  0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
  0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
  0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
  0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
  0x9216d5d9, 0x8979fb1b,
}

dkcBlowfish.c22 行で定義されています。

参照元 dkcBlowfishInit().

const DWORD bf_S[4][256] [static]
 

dkcBlowfish.c29 行で定義されています。

参照元 dkcBlowfishInit().


dkutil_cに対してSun Jul 18 22:45:29 2004に生成されました。 doxygen 1.3.6