aboutsummaryrefslogtreecommitdiff
path: root/src
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
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 '')
-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
-rw-r--r--src/tests/blowfish.cpp93
-rw-r--r--src/tests/threadid.cpp72
6 files changed, 292 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
diff --git a/src/tests/blowfish.cpp b/src/tests/blowfish.cpp
new file mode 100644
index 0000000..c7486e1
--- /dev/null
+++ b/src/tests/blowfish.cpp
@@ -0,0 +1,93 @@
1#include <bu/blowfish.h>
2#include <bu/file.h>
3#include <bu/membuf.h>
4#include <bu/hex.h>
5#include <bu/strfilter.h>
6#include <bu/sio.h>
7
8using namespace Bu;
9
10static const char *testdat[34][3] ={
11{"0000000000000000", "0000000000000000", "4EF997456198DD78"},
12{"FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A"},
13{"3000000000000000", "1000000000000001", "7D856F9A613063F2"},
14{"1111111111111111", "1111111111111111", "2466DD878B963C9D"},
15{"0123456789ABCDEF", "1111111111111111", "61F9C3802281B096"},
16{"1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7"},
17{"0000000000000000", "0000000000000000", "4EF997456198DD78"},
18{"FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D"},
19{"7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B"},
20{"0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0"},
21{"07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4"},
22{"3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB"},
23{"04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A"},
24{"0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918"},
25{"0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98"},
26{"43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5"},
27{"07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79"},
28{"04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3"},
29{"37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969"},
30{"1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B"},
31{"584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E"},
32{"025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD"},
33{"49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019"},
34{"4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3"},
35{"49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5"},
36{"018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778"},
37{"1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201"},
38{"0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2"},
39{"1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE"},
40{"E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D"},
41{"0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4"},
42{"FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC"},
43{"0123456789ABCDEF", "0000000000000000", "245946885754369A"},
44{"FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A"}};
45
46
47int main( int argc, char *argv[] )
48{
49
50 for( int j = 0; j < 34; j++ )
51 {
52 MemBuf mb;
53 Blowfish bf( mb );
54 bf.setPassword( decodeStr<Hex>( testdat[j][0] ) );
55 bf.write( decodeStr<Hex>( testdat[j][1] ) );
56 sio << "Test " << j << ": " << (mb.getString() == decodeStr<Hex>( testdat[j][2] )) << " (" << encodeStr<Hex>( mb.getString(), true ) << " == " << testdat[j][2] << ")" << sio.nl;
57
58 mb.setPos( 0 );
59 Blowfish bf2( mb );
60 bf2.setPassword( decodeStr<Hex>( testdat[j][0] ) );
61 char buf[8];
62 bf2.read( buf, 8 );
63
64 sio << " - Back: " << (Bu::String(testdat[j][1]) == encodeStr<Hex>(String(buf,8),true)) << sio.nl;
65 }
66
67 /*
68 {
69 File fIn("data.plain", File::Read );
70 File fOut("data.crypt", File::WriteNew );
71
72 Blowfish bOut( fOut );
73 bOut.setPassword("abcdefghijklmnop");
74 bOut.write( fIn.readAll() );
75 }
76 */
77 /*
78 {
79 File fIn("data.java", File::Read );
80 File fOut("data.stuff", File::WriteNew );
81
82 Blowfish bIn( fIn );
83 bIn.setPassword("abcdefghijklmnop");
84 char buf[64];
85 bIn.read( buf, 64 );
86 fOut.write( buf, 64 );
87 sio << sio.nl << "All done." << sio.nl;
88 }
89 */
90
91 return 0;
92}
93
diff --git a/src/tests/threadid.cpp b/src/tests/threadid.cpp
new file mode 100644
index 0000000..9ff99df
--- /dev/null
+++ b/src/tests/threadid.cpp
@@ -0,0 +1,72 @@
1#include <bu/thread.h>
2#include <bu/sio.h>
3
4#define BU_TRACE
5#include <bu/trace.h>
6
7using namespace Bu;
8
9class CopyThing
10{
11public:
12 CopyThing()
13 {
14 TRACE();
15 tidHome = Thread::currentThread();
16 }
17
18 CopyThing( const CopyThing &rSrc )
19 {
20 TRACE();
21 tidHome = Thread::currentThread();
22 sio << "Same thread? " << (tidHome == rSrc.tidHome) << sio.nl;
23 }
24
25 void doThings()
26 {
27 TRACE();
28 if( tidHome != Thread::currentThread() )
29 sio << "Different threads, hard copy here." << sio.nl;
30 else
31 sio << "Same thread, everything is cool." << sio.nl;
32 }
33
34private:
35 ThreadId tidHome;
36};
37
38class SubThread : public Thread
39{
40public:
41 SubThread( CopyThing &src ) :
42 src( src )
43 {
44 src.doThings();
45 }
46
47protected:
48 void run()
49 {
50 src.doThings();
51 sio << "run-Child is me? " << (getId() == Thread::currentThread()) << sio.nl;
52 }
53
54private:
55 CopyThing src;
56};
57
58int main( int argc, char *argv[] )
59{
60 CopyThing a;
61
62 SubThread st( a );
63 st.start();
64
65 sio << "Child is me? " << (st.getId() == Thread::currentThread()) << sio.nl;
66
67 st.join();
68
69
70 return 0;
71}
72