summaryrefslogtreecommitdiff
path: root/src/experimental
diff options
context:
space:
mode:
Diffstat (limited to 'src/experimental')
-rw-r--r--src/experimental/blowfish.cpp102
-rw-r--r--src/experimental/blowfish.h17
-rw-r--r--src/experimental/cipher.cpp65
-rw-r--r--src/experimental/cipher.h29
4 files changed, 127 insertions, 86 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
diff --git a/src/experimental/blowfish.h b/src/experimental/blowfish.h
index 054fc82..4dbd637 100644
--- a/src/experimental/blowfish.h
+++ b/src/experimental/blowfish.h
@@ -1,7 +1,7 @@
1#ifndef BU_BLOWFISH_H 1#ifndef BU_BLOWFISH_H
2#define BU_BLOWFISH_H 2#define BU_BLOWFISH_H
3 3
4#include "bu/filter.h" 4#include "bu/cipher.h"
5 5
6#define NUM_SUBKEYS 18 6#define NUM_SUBKEYS 18
7#define NUM_S_BOXES 4 7#define NUM_S_BOXES 4
@@ -12,7 +12,7 @@
12 12
13namespace Bu 13namespace Bu
14{ 14{
15 class Blowfish : public Bu::Filter 15 class Blowfish : public Bu::Cipher
16 { 16 {
17 public: 17 public:
18 Blowfish( Bu::Stream &rNext ); 18 Blowfish( Bu::Stream &rNext );
@@ -20,14 +20,6 @@ namespace Bu
20 20
21 void setPassword( const Bu::String &sPass ); 21 void setPassword( const Bu::String &sPass );
22 22
23 virtual void start();
24 virtual Bu::size stop();
25
26 virtual Bu::size read( void *pBuf, Bu::size iBytes );
27 virtual Bu::size write( const void *pBuf, Bu::size iBytes );
28 using Bu::Stream::read;
29 using Bu::Stream::write;
30
31 private: 23 private:
32 uint32_t PA[NUM_SUBKEYS]; 24 uint32_t PA[NUM_SUBKEYS];
33 uint32_t SB[NUM_S_BOXES][NUM_ENTRIES]; 25 uint32_t SB[NUM_S_BOXES][NUM_ENTRIES];
@@ -65,8 +57,9 @@ namespace Bu
65 }; 57 };
66 58
67 void reset(); 59 void reset();
68 inline void BF_En( Word *, Word * ); 60 virtual void encipher( void *pData );
69 inline void BF_De( Word *, Word * ); 61 virtual void decipher( void *pData );
62 inline void keyEncipher( Word &w1, Word &w2 );
70 }; 63 };
71}; 64};
72 65
diff --git a/src/experimental/cipher.cpp b/src/experimental/cipher.cpp
new file mode 100644
index 0000000..3430c08
--- /dev/null
+++ b/src/experimental/cipher.cpp
@@ -0,0 +1,65 @@
1#include "bu/cipher.h"
2
3Bu::Cipher::Cipher( Bu::Stream &rNext ) :
4 Bu::Filter( rNext )
5{
6}
7
8Bu::Cipher::~Cipher()
9{
10}
11
12void Bu::Cipher::start()
13{
14}
15
16Bu::size Bu::Cipher::stop()
17{
18 return 0;
19}
20
21Bu::size Bu::Cipher::read( void *pBuf, Bu::size iBytes )
22{
23 uint32_t i;
24
25 if (iBytes%8)
26 {
27 return 0;
28 }
29
30 iBytes /= 8;
31
32 for (i=0;i<iBytes;i++)
33 {
34 void *pSeg = ((char *)pBuf)+(i*8);
35 int iRead = rNext.read( pSeg, 8 );
36 decipher( pSeg );
37 }
38
39 return iBytes*8;
40}
41
42Bu::size Bu::Cipher::write( const void *pBuf, Bu::size iBytes )
43{
44 uint32_t i;
45
46 if (iBytes%8)
47 {
48 return 0;
49 }
50
51 iBytes /= 8;
52
53 char buf[8];
54
55 for (i=0;i<iBytes;i++)
56 {
57 memcpy( buf, ((const char *)pBuf)+(i*8), 8 );
58 encipher( buf );
59 rNext.write( buf, 8 );
60 }
61
62 memset( &buf, 0, 8 );
63 return iBytes*8;
64}
65
diff --git a/src/experimental/cipher.h b/src/experimental/cipher.h
new file mode 100644
index 0000000..2327aa6
--- /dev/null
+++ b/src/experimental/cipher.h
@@ -0,0 +1,29 @@
1#ifndef BU_CIPHER_H
2#define BU_CIPHER_H
3
4#include "bu/filter.h"
5
6namespace Bu
7{
8 class Cipher : Bu::Filter
9 {
10 public:
11 Cipher( Bu::Stream &rNext );
12 virtual ~Cipher();
13
14 virtual void start();
15 virtual Bu::size stop();
16
17 virtual Bu::size read( void *pBuf, Bu::size iBytes );
18 virtual Bu::size write( const void *pBuf, Bu::size iBytes );
19
20 using Bu::Stream::read;
21 using Bu::Stream::write;
22
23 protected:
24 virtual void encipher( void *pData )=0;
25 virtual void decipher( void *pData )=0;
26 };
27};
28
29#endif