aboutsummaryrefslogtreecommitdiff
path: root/src/experimental
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-04-07 21:46:17 +0000
committerMike Buland <eichlan@xagasoft.com>2012-04-07 21:46:17 +0000
commitb93f18e1dd303fb648bc350416f7f5ace536fd1f (patch)
tree636bf4dfafcf2e9aef30db19a4e2f9cd12cb36d4 /src/experimental
parent2c6cbad869b0e60b37859c3c4f0c850721d057ce (diff)
downloadlibbu++-b93f18e1dd303fb648bc350416f7f5ace536fd1f.tar.gz
libbu++-b93f18e1dd303fb648bc350416f7f5ace536fd1f.tar.bz2
libbu++-b93f18e1dd303fb648bc350416f7f5ace536fd1f.tar.xz
libbu++-b93f18e1dd303fb648bc350416f7f5ace536fd1f.zip
Blowfish now passes all standard test vectors on little endian. I don't know
about big endian...we can hope.
Diffstat (limited to '')
-rw-r--r--src/experimental/blowfish.cpp23
-rw-r--r--src/experimental/blowfish.h22
2 files changed, 27 insertions, 18 deletions
diff --git a/src/experimental/blowfish.cpp b/src/experimental/blowfish.cpp
index 401291c..3dda87a 100644
--- a/src/experimental/blowfish.cpp
+++ b/src/experimental/blowfish.cpp
@@ -18,11 +18,13 @@ Bu::Blowfish::~Blowfish()
18 reset(); 18 reset();
19} 19}
20 20
21#define revBytes( x ) x = (((x&0xff)<<24)|((x&0xff00)<<8)|((x&0xff0000)>>8)|((x&0xff000000)>>24))
22
21void Bu::Blowfish::setPassword( const Bu::String &sPass ) 23void Bu::Blowfish::setPassword( const Bu::String &sPass )
22{ 24{
23 reset(); 25 reset();
24 26
25 unsigned int i,j,len=sPass.getSize(); 27 uint32_t i,j,len=sPass.getSize();
26 Word Work,null0,null1; 28 Word Work,null0,null1;
27 29
28 if (len > 0) 30 if (len > 0)
@@ -70,7 +72,7 @@ Bu::size Bu::Blowfish::stop()
70 72
71Bu::size Bu::Blowfish::read( void *pBuf, Bu::size iBytes ) 73Bu::size Bu::Blowfish::read( void *pBuf, Bu::size iBytes )
72{ 74{
73 unsigned int i; 75 uint32_t i;
74 DWord dwWork; 76 DWord dwWork;
75 77
76 if (iBytes%8) 78 if (iBytes%8)
@@ -83,8 +85,11 @@ Bu::size Bu::Blowfish::read( void *pBuf, Bu::size iBytes )
83 for (i=0;i<iBytes;i++) 85 for (i=0;i<iBytes;i++)
84 { 86 {
85 int iRead = rNext.read( &dwWork, 8 ); 87 int iRead = rNext.read( &dwWork, 8 );
86 sio << "Read: " << iRead << sio.nl; 88 revBytes( dwWork.word0.word );
89 revBytes( dwWork.word1.word );
87 BF_De(&dwWork.word0,&dwWork.word1); 90 BF_De(&dwWork.word0,&dwWork.word1);
91 dwWork.word0.word = htobe32( dwWork.word0.word );
92 dwWork.word1.word = htobe32( dwWork.word1.word );
88 memcpy( ((char *)pBuf)+(i*8), &dwWork, 8 ); 93 memcpy( ((char *)pBuf)+(i*8), &dwWork, 8 );
89 } 94 }
90 95
@@ -94,7 +99,7 @@ Bu::size Bu::Blowfish::read( void *pBuf, Bu::size iBytes )
94 99
95Bu::size Bu::Blowfish::write( const void *pBuf, Bu::size iBytes ) 100Bu::size Bu::Blowfish::write( const void *pBuf, Bu::size iBytes )
96{ 101{
97 unsigned int i; 102 uint32_t i;
98 DWord dwWork; 103 DWord dwWork;
99 104
100 if (iBytes%8) 105 if (iBytes%8)
@@ -107,7 +112,11 @@ Bu::size Bu::Blowfish::write( const void *pBuf, Bu::size iBytes )
107 for (i=0;i<iBytes;i++) 112 for (i=0;i<iBytes;i++)
108 { 113 {
109 memcpy( &dwWork, ((const char *)pBuf)+(i*8), 8 ); 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 );
110 BF_En(&dwWork.word0,&dwWork.word1); 117 BF_En(&dwWork.word0,&dwWork.word1);
118 revBytes( dwWork.word0.word );
119 revBytes( dwWork.word1.word );
111 rNext.write( &dwWork, 8 ); 120 rNext.write( &dwWork, 8 );
112 } 121 }
113 122
@@ -117,9 +126,9 @@ Bu::size Bu::Blowfish::write( const void *pBuf, Bu::size iBytes )
117 126
118void Bu::Blowfish::reset() 127void Bu::Blowfish::reset()
119{ 128{
120 unsigned int i,j; 129 uint32_t i,j;
121 130
122 static unsigned int PA_Init[NUM_SUBKEYS] = 131 static uint32_t PA_Init[NUM_SUBKEYS] =
123 { 132 {
124 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 133 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
125 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, 134 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
@@ -128,7 +137,7 @@ void Bu::Blowfish::reset()
128 0x9216d5d9, 0x8979fb1b 137 0x9216d5d9, 0x8979fb1b
129 }; 138 };
130 139
131 static unsigned int SB_Init[NUM_S_BOXES][NUM_ENTRIES] = { 140 static uint32_t SB_Init[NUM_S_BOXES][NUM_ENTRIES] = {
132 { 141 {
133 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 142 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
134 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, 143 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
diff --git a/src/experimental/blowfish.h b/src/experimental/blowfish.h
index fb46dfd..054fc82 100644
--- a/src/experimental/blowfish.h
+++ b/src/experimental/blowfish.h
@@ -29,24 +29,24 @@ namespace Bu
29 using Bu::Stream::write; 29 using Bu::Stream::write;
30 30
31 private: 31 private:
32 unsigned int PA[NUM_SUBKEYS]; 32 uint32_t PA[NUM_SUBKEYS];
33 unsigned int SB[NUM_S_BOXES][NUM_ENTRIES]; 33 uint32_t SB[NUM_S_BOXES][NUM_ENTRIES];
34 34
35#if __BYTE_ORDER == __BIG_ENDIAN 35#if __BYTE_ORDER == __BIG_ENDIAN
36 struct WordByte 36 struct WordByte
37 { 37 {
38 unsigned int zero:8; 38 uint32_t zero:8;
39 unsigned int one:8; 39 uint32_t one:8;
40 unsigned int two:8; 40 uint32_t two:8;
41 unsigned int three:8; 41 uint32_t three:8;
42 }; 42 };
43#elif __BYTE_ORDER == __LITTLE_ENDIAN 43#elif __BYTE_ORDER == __LITTLE_ENDIAN
44 struct WordByte 44 struct WordByte
45 { 45 {
46 unsigned int three:8; 46 uint32_t three:8;
47 unsigned int two:8; 47 uint32_t two:8;
48 unsigned int one:8; 48 uint32_t one:8;
49 unsigned int zero:8; 49 uint32_t zero:8;
50 }; 50 };
51#else 51#else
52#error No endianness defined 52#error No endianness defined
@@ -54,7 +54,7 @@ namespace Bu
54 54
55 union Word 55 union Word
56 { 56 {
57 unsigned int word; 57 uint32_t word;
58 WordByte byte; 58 WordByte byte;
59 }; 59 };
60 60