aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/blowfish.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-04-09 16:29:18 +0000
committerMike Buland <eichlan@xagasoft.com>2012-04-09 16:29:18 +0000
commit228b885b41652a015a91770dfd993456d76ad102 (patch)
tree9a4b0756133fa1b7553f9aacf9ad50e7f26179d8 /src/experimental/blowfish.cpp
parentb93f18e1dd303fb648bc350416f7f5ace536fd1f (diff)
downloadlibbu++-228b885b41652a015a91770dfd993456d76ad102.tar.gz
libbu++-228b885b41652a015a91770dfd993456d76ad102.tar.bz2
libbu++-228b885b41652a015a91770dfd993456d76ad102.tar.xz
libbu++-228b885b41652a015a91770dfd993456d76ad102.zip
Blowfish works in it's new split form, which will make it much easier to add
other types of ciphers down the road, should we choose to.
Diffstat (limited to 'src/experimental/blowfish.cpp')
-rw-r--r--src/experimental/blowfish.cpp102
1 files changed, 28 insertions, 74 deletions
diff --git a/src/experimental/blowfish.cpp b/src/experimental/blowfish.cpp
index 3dda87a..797ec73 100644
--- a/src/experimental/blowfish.cpp
+++ b/src/experimental/blowfish.cpp
@@ -9,7 +9,7 @@ using Bu::sio;
9 SB[3][x.byte.three]) 9 SB[3][x.byte.three])
10 10
11Bu::Blowfish::Blowfish( Bu::Stream &rNext ) : 11Bu::Blowfish::Blowfish( Bu::Stream &rNext ) :
12 Bu::Filter( rNext ) 12 Bu::Cipher( rNext )
13{ 13{
14} 14}
15 15
@@ -43,7 +43,7 @@ void Bu::Blowfish::setPassword( const Bu::String &sPass )
43 43
44 for (i=0;i<NUM_SUBKEYS;i+=2) 44 for (i=0;i<NUM_SUBKEYS;i+=2)
45 { 45 {
46 BF_En(&null0,&null1); 46 keyEncipher( null0, null1 );
47 PA[i] = null0.word; 47 PA[i] = null0.word;
48 PA[i+1] = null1.word; 48 PA[i+1] = null1.word;
49 } 49 }
@@ -51,7 +51,7 @@ void Bu::Blowfish::setPassword( const Bu::String &sPass )
51 for (j=0;j<NUM_S_BOXES;j++) 51 for (j=0;j<NUM_S_BOXES;j++)
52 for (i=0;i<NUM_ENTRIES;i+=2) 52 for (i=0;i<NUM_ENTRIES;i+=2)
53 { 53 {
54 BF_En(&null0,&null1); 54 keyEncipher( null0, null1 );
55 SB[j][i] = null0.word; 55 SB[j][i] = null0.word;
56 SB[j][i+1] = null1.word; 56 SB[j][i+1] = null1.word;
57 } 57 }
@@ -61,69 +61,6 @@ void Bu::Blowfish::setPassword( const Bu::String &sPass )
61 len = 0; 61 len = 0;
62} 62}
63 63
64void Bu::Blowfish::start()
65{
66}
67
68Bu::size Bu::Blowfish::stop()
69{
70 return 0;
71}
72
73Bu::size Bu::Blowfish::read( void *pBuf, Bu::size iBytes )
74{
75 uint32_t i;
76 DWord dwWork;
77
78 if (iBytes%8)
79 {
80 return 0;
81 }
82
83 iBytes /= 8;
84
85 for (i=0;i<iBytes;i++)
86 {
87 int iRead = rNext.read( &dwWork, 8 );
88 revBytes( dwWork.word0.word );
89 revBytes( dwWork.word1.word );
90 BF_De(&dwWork.word0,&dwWork.word1);
91 dwWork.word0.word = htobe32( dwWork.word0.word );
92 dwWork.word1.word = htobe32( dwWork.word1.word );
93 memcpy( ((char *)pBuf)+(i*8), &dwWork, 8 );
94 }
95
96 memset( &dwWork, 0, 8 );
97 return iBytes*8;
98}
99
100Bu::size Bu::Blowfish::write( const void *pBuf, Bu::size iBytes )
101{
102 uint32_t i;
103 DWord dwWork;
104
105 if (iBytes%8)
106 {
107 return 0;
108 }
109
110 iBytes /= 8;
111
112 for (i=0;i<iBytes;i++)
113 {
114 memcpy( &dwWork, ((const char *)pBuf)+(i*8), 8 );
115 dwWork.word0.word = be32toh( dwWork.word0.word );
116 dwWork.word1.word = be32toh( dwWork.word1.word );
117 BF_En(&dwWork.word0,&dwWork.word1);
118 revBytes( dwWork.word0.word );
119 revBytes( dwWork.word1.word );
120 rNext.write( &dwWork, 8 );
121 }
122
123 memset( &dwWork, 0, 8 );
124 return iBytes*8;
125}
126
127void Bu::Blowfish::reset() 64void Bu::Blowfish::reset()
128{ 65{
129 uint32_t i,j; 66 uint32_t i,j;
@@ -413,10 +350,22 @@ void Bu::Blowfish::reset()
413 SB[j][i] = SB_Init[j][i]; 350 SB[j][i] = SB_Init[j][i];
414} 351}
415 352
416void Bu::Blowfish::BF_En( Word *x1, Word *x2 ) 353void Bu::Blowfish::encipher( void *pData )
417{ 354{
418 Word w1=*x1,w2=*x2; 355 DWord *dwWork = (DWord *)pData;
356 Word &w1 = dwWork->word0, &w2 = dwWork->word1;
357
358 w1.word = be32toh( w1.word );
359 w2.word = be32toh( w2.word );
419 360
361 keyEncipher( w1, w2 );
362
363 revBytes( w1.word );
364 revBytes( w2.word );
365}
366
367void Bu::Blowfish::keyEncipher( Word &w1, Word &w2 )
368{
420 w1.word ^= PA[0]; 369 w1.word ^= PA[0];
421 w2.word ^= F(w1)^PA[1]; w1.word ^= F(w2)^PA[2]; 370 w2.word ^= F(w1)^PA[1]; w1.word ^= F(w2)^PA[2];
422 w2.word ^= F(w1)^PA[3]; w1.word ^= F(w2)^PA[4]; 371 w2.word ^= F(w1)^PA[3]; w1.word ^= F(w2)^PA[4];
@@ -428,14 +377,17 @@ void Bu::Blowfish::BF_En( Word *x1, Word *x2 )
428 w2.word ^= F(w1)^PA[15]; w1.word ^= F(w2)^PA[16]; 377 w2.word ^= F(w1)^PA[15]; w1.word ^= F(w2)^PA[16];
429 w2.word ^= PA[17]; 378 w2.word ^= PA[17];
430 379
431 *x1 = w2; 380 Bu::swap( w1, w2 );
432 *x2 = w1;
433} 381}
434 382
435void Bu::Blowfish::BF_De( Word *x1, Word *x2 ) 383void Bu::Blowfish::decipher( void *pData )
436{ 384{
437 Word w1=*x1,w2=*x2; 385 DWord *dwWork = (DWord *)pData;
386 Word &w1 = dwWork->word0, &w2 = dwWork->word1;
438 387
388 revBytes( w1.word );
389 revBytes( w2.word );
390
439 w1.word ^= PA[17]; 391 w1.word ^= PA[17];
440 w2.word ^= F(w1)^PA[16]; w1.word ^= F(w2)^PA[15]; 392 w2.word ^= F(w1)^PA[16]; w1.word ^= F(w2)^PA[15];
441 w2.word ^= F(w1)^PA[14]; w1.word ^= F(w2)^PA[13]; 393 w2.word ^= F(w1)^PA[14]; w1.word ^= F(w2)^PA[13];
@@ -446,8 +398,10 @@ void Bu::Blowfish::BF_De( Word *x1, Word *x2 )
446 w2.word ^= F(w1)^PA[4]; w1.word ^= F(w2)^PA[3]; 398 w2.word ^= F(w1)^PA[4]; w1.word ^= F(w2)^PA[3];
447 w2.word ^= F(w1)^PA[2]; w1.word ^= F(w2)^PA[1]; 399 w2.word ^= F(w1)^PA[2]; w1.word ^= F(w2)^PA[1];
448 w2.word ^= PA[0]; 400 w2.word ^= PA[0];
401
402 Bu::swap( w1, w2 );
449 403
450 *x1 = w2; 404 w1.word = htobe32( w1.word );
451 *x2 = w1; 405 w2.word = htobe32( w2.word );
452} 406}
453 407