/src/cryptopp/blowfish.cpp
Line | Count | Source |
1 | | // blowfish.cpp - originally written and placed in the public domain by Wei Dai |
2 | | |
3 | | #include "pch.h" |
4 | | #include "blowfish.h" |
5 | | #include "misc.h" |
6 | | |
7 | | NAMESPACE_BEGIN(CryptoPP) |
8 | | |
9 | | void Blowfish::Base::UncheckedSetKey(const byte *key_string, unsigned int keylength, const NameValuePairs &) |
10 | 20 | { |
11 | 20 | AssertValidKeyLength(keylength); |
12 | | |
13 | 20 | unsigned i, j=0, k; |
14 | 20 | word32 data, dspace[2] = {0, 0}; |
15 | | |
16 | 20 | std::memcpy(pbox, p_init, sizeof(p_init)); |
17 | 20 | std::memcpy(sbox, s_init, sizeof(s_init)); |
18 | | |
19 | | // Xor key string into encryption key vector |
20 | 380 | for (i=0 ; i<ROUNDS+2 ; ++i) |
21 | 360 | { |
22 | 360 | data = 0 ; |
23 | 1.80k | for (k=0 ; k<4 ; ++k ) |
24 | 1.44k | data = (data << 8) | key_string[j++ % keylength]; |
25 | 360 | pbox[i] ^= data; |
26 | 360 | } |
27 | | |
28 | 20 | crypt_block(dspace, pbox); |
29 | | |
30 | 180 | for (i=0; i<ROUNDS; i+=2) |
31 | 160 | crypt_block(pbox+i, pbox+i+2); |
32 | | |
33 | 20 | crypt_block(pbox+ROUNDS, sbox); |
34 | | |
35 | 10.2k | for (i=0; i<4*256-2; i+=2) |
36 | 10.2k | crypt_block(sbox+i, sbox+i+2); |
37 | | |
38 | 20 | if (!IsForwardTransformation()) |
39 | 20 | for (i=0; i<(ROUNDS+2)/2; i++) |
40 | 18 | std::swap(pbox[i], pbox[ROUNDS+1-i]); |
41 | 20 | } |
42 | | |
43 | | // this version is only used to make pbox and sbox |
44 | | void Blowfish::Base::crypt_block(const word32 in[2], word32 out[2]) const |
45 | 10.4k | { |
46 | 10.4k | word32 left = in[0]; |
47 | 10.4k | word32 right = in[1]; |
48 | | |
49 | 10.4k | const word32 *const s=sbox; |
50 | 10.4k | const word32 *p=pbox; |
51 | | |
52 | 10.4k | left ^= p[0]; |
53 | | |
54 | 93.7k | for (unsigned i=0; i<ROUNDS/2; i++) |
55 | 83.3k | { |
56 | 83.3k | right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)]) |
57 | 83.3k | ^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)]) |
58 | 83.3k | ^ p[2*i+1]; |
59 | | |
60 | 83.3k | left ^= (((s[GETBYTE(right,3)] + s[256+GETBYTE(right,2)]) |
61 | 83.3k | ^ s[2*256+GETBYTE(right,1)]) + s[3*256+GETBYTE(right,0)]) |
62 | 83.3k | ^ p[2*i+2]; |
63 | 83.3k | } |
64 | | |
65 | 10.4k | right ^= p[ROUNDS+1]; |
66 | | |
67 | 10.4k | out[0] = right; |
68 | 10.4k | out[1] = left; |
69 | 10.4k | } |
70 | | |
71 | | void Blowfish::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const |
72 | 8.26k | { |
73 | 8.26k | typedef BlockGetAndPut<word32, BigEndian> Block; |
74 | | |
75 | 8.26k | word32 left, right; |
76 | 8.26k | Block::Get(inBlock)(left)(right); |
77 | | |
78 | 8.26k | const word32 *const s=sbox; |
79 | 8.26k | const word32 *p=pbox; |
80 | | |
81 | 8.26k | left ^= p[0]; |
82 | | |
83 | 74.3k | for (unsigned i=0; i<ROUNDS/2; i++) |
84 | 66.1k | { |
85 | 66.1k | right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)]) |
86 | 66.1k | ^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)]) |
87 | 66.1k | ^ p[2*i+1]; |
88 | | |
89 | 66.1k | left ^= (((s[GETBYTE(right,3)] + s[256+GETBYTE(right,2)]) |
90 | 66.1k | ^ s[2*256+GETBYTE(right,1)]) + s[3*256+GETBYTE(right,0)]) |
91 | 66.1k | ^ p[2*i+2]; |
92 | 66.1k | } |
93 | | |
94 | 8.26k | right ^= p[ROUNDS+1]; |
95 | | |
96 | 8.26k | Block::Put(xorBlock, outBlock)(right)(left); |
97 | 8.26k | } |
98 | | |
99 | | NAMESPACE_END |