aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/blowfish.h
blob: 054fc82d24bbacc7242f5bccb06b8018776d646e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef BU_BLOWFISH_H
#define BU_BLOWFISH_H

#include "bu/filter.h"

#define NUM_SUBKEYS  18
#define NUM_S_BOXES  4
#define NUM_ENTRIES  256

#define MAX_STRING   256
#define MAX_PASSWD   56  // 448bits

namespace Bu
{
	class Blowfish : public Bu::Filter
	{
	public:
		Blowfish( Bu::Stream &rNext );
		virtual ~Blowfish();

		void setPassword( const Bu::String &sPass );

		virtual void start();
		virtual Bu::size stop();

		virtual Bu::size read( void *pBuf, Bu::size iBytes );
		virtual Bu::size write( const void *pBuf, Bu::size iBytes );
		using Bu::Stream::read;
		using Bu::Stream::write;

	private:
		uint32_t PA[NUM_SUBKEYS];
		uint32_t SB[NUM_S_BOXES][NUM_ENTRIES];

#if __BYTE_ORDER == __BIG_ENDIAN
		struct WordByte
		{
			uint32_t zero:8;
			uint32_t one:8;
			uint32_t two:8;
			uint32_t three:8;
		};
#elif __BYTE_ORDER == __LITTLE_ENDIAN
		struct WordByte
		{
			uint32_t three:8;
			uint32_t two:8;
			uint32_t one:8;
			uint32_t zero:8;
		};
#else
#error No endianness defined
#endif

		union Word
		{
			uint32_t word;
			WordByte byte;
		};

		struct DWord
		{
			Word word0;
			Word word1;
		};

		void reset();
		inline void BF_En( Word *, Word * );
		inline void BF_De( Word *, Word * );
	};
};

#endif