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

dkcBlowfish.c

Blowfish encryption algorithm [詳細]

#include "dkcBlowfish.h"

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

ソースコードを見る。

マクロ定義

#define DKUTIL_C_BLOWFISH_C
#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()で確保したメモリ領域を開放
static DKC_INLINE void Blowfish_encipher (DKC_BLOWFISH *p, DWORD *xl, DWORD *xr)
static DKC_INLINE 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)

変数

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


説明

Blowfish encryption algorithm

作者:
converted by d金魚
参照:
dkcBlowfish.h

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


マクロ定義

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

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

#define DKUTIL_C_BLOWFISH_C
 

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

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

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

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

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

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


関数

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

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

参照先 dkc_Blowfish::PArray, ROUND, と dkc_Blowfish::SBoxes.

参照元 dkcBlowfishDecrypt().

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

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

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

参照先 dkc_Blowfish::PArray, ROUND, と dkc_Blowfish::SBoxes.

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

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

DKC_BLOWFISH* WINAPI dkcAllocBlowfish (  BYTE *  key,
int  keysize
) 
 

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

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

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

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

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.c の 516 行で定義されています。

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

00517 {
00518     DWORD   lCount ;
00519     BYTE    *pi, *po ;
00520     int     i ;
00521     int     SameDest = (pInput == pOutput ? 1 : 0) ;
00522 
00523     for (lCount = 0 ; lCount < lSize ; lCount += 8)
00524     {
00525         if (SameDest)   // if encoded data is being written into inputbuffer
00526         {
00527             Blowfish_decipher (p,(DWORD *) pInput,
00528                 (DWORD *) (pInput + 4)) ;
00529             pInput += 8 ;
00530         }
00531         else            // output buffer not equal to inputbuffer
00532         {               // so copy input to output before decoding
00533             pi = pInput ;
00534             po = pOutput ;
00535             for (i = 0 ; i < 8 ; i++)
00536                 *po++ = *pi++ ;
00537             Blowfish_decipher (p,(DWORD *) pOutput,
00538                 (DWORD *) (pOutput + 4)) ;
00539             pInput += 8 ;
00540             pOutput += 8 ;
00541         }
00542     }
00543 }

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.

戻り値:
0 == ERROR

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

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

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

DWORD WINAPI dkcBlowfishGetOutputLength (  DWORD  lInputLong  ) 
 

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

参照先 DWORD.

参照元 dkcBlowfishEncrypt().

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

int WINAPI dkcBlowfishInit (  DKC_BLOWFISH *  p,
BYTE *  key,
int  keybytes
) 
 

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

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

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

参照先 bf_P, bf_S, Blowfish_encipher(), dkcd_BLOWFISH_MAX_KEY_SIZE, DWORD, NPASS, dkc_Blowfish::PArray, と dkc_Blowfish::SBoxes.

参照元 dkcAllocBlowfish().

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

int WINAPI dkcFreeBlowfish (  DKC_BLOWFISH **  p  ) 
 

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

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

参照先 dkcFree(), と NULL.

参照元 dkcAllocBlowfish().

00316                                             {
00317     if(NULL==p || NULL==(*p)){
00318         return edk_FAILED;
00319     }
00320     if((*p)->SBoxes){
00321         free((*p)->SBoxes);
00322     }
00323     if((*p)->PArray){
00324         free((*p)->PArray);
00325     }
00326     return dkcFree((void **)p);
00327 }


変数

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.c の 22 行で定義されています。

参照元 dkcBlowfishInit().

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

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

参照元 dkcBlowfishInit().


dkutil_cに対してSat Sep 10 09:24:06 2005に生成されました。  doxygen 1.4.4