Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
blowfish.hpp
Go to the documentation of this file.
1
3// Blowfish.h Header File
4//
5// BLOWFISH ENCRYPTION ALGORITHM
6//
7// encryption and decryption of Byte Strings using the Blowfish encryption Algorithm.
8// Blowfish is a block cipher that encrypts data in 8-byte blocks. The algorithm consists
9// of two parts: a key-expansion part and a data-ancryption part. Key expansion converts a
10// variable key of at least 1 and at most 56 bytes into several subkey arrays totaling
11// 4168 bytes. Blowfish has 16 rounds. Each round consists of a key-dependent permutation,
12// and a key and data-dependent substitution. All operations are XORs and additions on 32-bit words.
13// The only additional operations are four indexed array data lookups per round.
14// Blowfish uses a large number of subkeys. These keys must be precomputed before any data
15// encryption or decryption. The P-array consists of 18 32-bit subkeys: P0, P1,...,P17.
16// There are also four 32-bit S-boxes with 256 entries each: S0,0, S0,1,...,S0,255;
17// S1,0, S1,1,...,S1,255; S2,0, S2,1,...,S2,255; S3,0, S3,1,...,S3,255;
18//
19// The Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback modes
20// are used:
21//
22// In ECB mode if the same block is encrypted twice with the same key, the resulting
23// ciphertext blocks are the same.
24//
25// In CBC Mode a ciphertext block is obtained by first xoring the
26// plaintext block with the previous ciphertext block, and encrypting the resulting value.
27//
28// In CFB mode a ciphertext block is obtained by encrypting the previous ciphertext block
29// and xoring the resulting value with the plaintext
30//
31// The previous ciphertext block is usually stored in an Initialization Vector (IV).
32// An Initialization Vector of zero is commonly used for the first block, though other
33// arrangements are also in use.
34
35/*
36http://www.counterpane.com/vectors.txt
37Test vectors by Eric Young. These tests all assume Blowfish with 16
38rounds.
39
40All data is shown as a hex string with 012345 loading as
41data[0]=0x01;
42data[1]=0x23;
43data[2]=0x45;
44ecb test data (taken from the DES validation tests)
45
46key bytes clear bytes cipher bytes
470000000000000000 0000000000000000 4EF997456198DD78
48FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 51866FD5B85ECB8A
493000000000000000 1000000000000001 7D856F9A613063F2 ???
501111111111111111 1111111111111111 2466DD878B963C9D
510123456789ABCDEF 1111111111111111 61F9C3802281B096
521111111111111111 0123456789ABCDEF 7D0CC630AFDA1EC7
530000000000000000 0000000000000000 4EF997456198DD78
54FEDCBA9876543210 0123456789ABCDEF 0ACEAB0FC6A0A28D
557CA110454A1A6E57 01A1D6D039776742 59C68245EB05282B
560131D9619DC1376E 5CD54CA83DEF57DA B1B8CC0B250F09A0
5707A1133E4A0B2686 0248D43806F67172 1730E5778BEA1DA4
583849674C2602319E 51454B582DDF440A A25E7856CF2651EB
5904B915BA43FEB5B6 42FD443059577FA2 353882B109CE8F1A
600113B970FD34F2CE 059B5E0851CF143A 48F4D0884C379918
610170F175468FB5E6 0756D8E0774761D2 432193B78951FC98
6243297FAD38E373FE 762514B829BF486A 13F04154D69D1AE5
6307A7137045DA2A16 3BDD119049372802 2EEDDA93FFD39C79
6404689104C2FD3B2F 26955F6835AF609A D887E0393C2DA6E3
6537D06BB516CB7546 164D5E404F275232 5F99D04F5B163969
661F08260D1AC2465E 6B056E18759F5CCA 4A057A3B24D3977B
67584023641ABA6176 004BD6EF09176062 452031C1E4FADA8E
68025816164629B007 480D39006EE762F2 7555AE39F59B87BD
6949793EBC79B3258F 437540C8698F3CFA 53C55F9CB49FC019
704FB05E1515AB73A7 072D43A077075292 7A8E7BFA937E89A3
7149E95D6D4CA229BF 02FE55778117F12A CF9C5D7A4986ADB5
72018310DC409B26D6 1D9D5C5018F728C2 D1ABB290658BC778
731C587F1C13924FEF 305532286D6F295A 55CB3774D13EF201
740101010101010101 0123456789ABCDEF FA34EC4847B268B2
751F1F1F1F0E0E0E0E 0123456789ABCDEF A790795108EA3CAE
76E0FEE0FEF1FEF1FE 0123456789ABCDEF C39E072D9FAC631D
770000000000000000 FFFFFFFFFFFFFFFF 014933E0CDAFF6E4
78FFFFFFFFFFFFFFFF 0000000000000000 F21E9A77B71C49BC
790123456789ABCDEF 0000000000000000 245946885754369A
80FEDCBA9876543210 FFFFFFFFFFFFFFFF 6B5C5A9C5D9E0A5A
81
82set_key test data
83data[8]= FEDCBA9876543210
84c=F9AD597C49DB005E k[ 1]=F0
85c=E91D21C1D961A6D6 k[ 2]=F0E1
86c=E9C2B70A1BC65CF3 k[ 3]=F0E1D2
87c=BE1E639408640F05 k[ 4]=F0E1D2C3
88c=B39E44481BDB1E6E k[ 5]=F0E1D2C3B4
89c=9457AA83B1928C0D k[ 6]=F0E1D2C3B4A5
90c=8BB77032F960629D k[ 7]=F0E1D2C3B4A596
91c=E87A244E2CC85E82 k[ 8]=F0E1D2C3B4A59687
92c=15750E7A4F4EC577 k[ 9]=F0E1D2C3B4A5968778
93c=122BA70B3AB64AE0 k[10]=F0E1D2C3B4A596877869
94c=3A833C9AFFC537F6 k[11]=F0E1D2C3B4A5968778695A
95c=9409DA87A90F6BF2 k[12]=F0E1D2C3B4A5968778695A4B
96c=884F80625060B8B4 k[13]=F0E1D2C3B4A5968778695A4B3C
97c=1F85031C19E11968 k[14]=F0E1D2C3B4A5968778695A4B3C2D
98c=79D9373A714CA34F k[15]=F0E1D2C3B4A5968778695A4B3C2D1E ???
99c=93142887EE3BE15C k[16]=F0E1D2C3B4A5968778695A4B3C2D1E0F
100c=03429E838CE2D14B k[17]=F0E1D2C3B4A5968778695A4B3C2D1E0F00
101c=A4299E27469FF67B k[18]=F0E1D2C3B4A5968778695A4B3C2D1E0F0011
102c=AFD5AED1C1BC96A8 k[19]=F0E1D2C3B4A5968778695A4B3C2D1E0F001122
103c=10851C0E3858DA9F k[20]=F0E1D2C3B4A5968778695A4B3C2D1E0F00112233
104c=E6F51ED79B9DB21F k[21]=F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344
105c=64A6E14AFD36B46F k[22]=F0E1D2C3B4A5968778695A4B3C2D1E0F001122334455
106c=80C7D7D45A5479AD k[23]=F0E1D2C3B4A5968778695A4B3C2D1E0F00112233445566
107c=05044B62FA52D080 k[24]=F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344556677
108
109chaining mode test data
110key[16] = 0123456789ABCDEFF0E1D2C3B4A59687
111iv[8] = FEDCBA9876543210
112data[29] = "7654321 Now is the time for " (includes trailing '\0')
113data[29] = 37363534333231204E6F77206973207468652074696D6520666F722000
114cbc cipher text
115cipher[32]= 6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC
116cfb64 cipher text cipher[29]=
117E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3
118ofb64 cipher text cipher[29]=
119E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA
120
121*/
122
123#pragma once
124#include <stdint.h>
125
126namespace fc {
127
128//Block Structure
129struct sblock
130{
131 //Constructors
132 sblock(unsigned int l=0, unsigned int r=0) : m_uil(l), m_uir(r) {}
133 //Copy Constructor
134 sblock(const sblock& roBlock) : m_uil(roBlock.m_uil), m_uir(roBlock.m_uir) {}
135 sblock& operator^=(sblock& b) { m_uil ^= b.m_uil; m_uir ^= b.m_uir; return *this; }
136 unsigned int m_uil, m_uir;
137};
138
140{
141public:
142 enum { ECB=0, CBC=1, CFB=2 };
143
144 //Constructor - Initialize the P and S boxes for a given Key
145 blowfish();
146 void start(unsigned char* ucKey, uint64_t n, const sblock& roChain = sblock(0UL,0UL));
147
148 //Resetting the chaining block
149 void reset_chain() { m_oChain = m_oChain0; }
150
151 // encrypt/decrypt Buffer in Place
152 void encrypt(unsigned char* buf, uint64_t n, int iMode=CFB);
153 void decrypt(unsigned char* buf, uint64_t n, int iMode=CFB);
154
155 // encrypt/decrypt from Input Buffer to Output Buffer
156 void encrypt(const unsigned char* in, unsigned char* out, uint64_t n, int iMode=CFB);
157 void decrypt(const unsigned char* in, unsigned char* out, uint64_t n, int iMode=CFB);
158
159//Private Functions
160private:
161 unsigned int F(unsigned int ui);
162 void encrypt(sblock&);
163 void decrypt(sblock&);
164
165private:
166 //The Initialization Vector, by default {0, 0}
167 sblock m_oChain0;
168 sblock m_oChain;
169 unsigned int m_auiP[18];
170 unsigned int m_auiS[4][256];
171 static const unsigned int scm_auiInitP[18];
172 static const unsigned int scm_auiInitS[4][256];
173};
174
175
176} // namespace fc
177
178
const mie::Vuint & r
Definition bn.cpp:28
void decrypt(unsigned char *buf, uint64_t n, int iMode=CFB)
Definition blowfish.cpp:493
void start(unsigned char *ucKey, uint64_t n, const sblock &roChain=sblock(0UL, 0UL))
Definition blowfish.cpp:306
void encrypt(unsigned char *buf, uint64_t n, int iMode=CFB)
Definition blowfish.cpp:450
void reset_chain()
Definition blowfish.hpp:149
namespace sysio::chain
Definition authority.cpp:3
unsigned __int64 uint64_t
Definition stdint.h:136
sblock(const sblock &roBlock)
Definition blowfish.hpp:134
unsigned int m_uil
Definition blowfish.hpp:136
sblock & operator^=(sblock &b)
Definition blowfish.hpp:135
unsigned int m_uir
Definition blowfish.hpp:136
sblock(unsigned int l=0, unsigned int r=0)
Definition blowfish.hpp:132
uint8_t buf[2048]
int l