HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Blowfish.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Blowfish.h ( Library, C++)
7  *
8  * COMMENTS:
9  * Based on a free source code from:
10  * http://www.schneier.com/blowfish.html
11  * http://www.schneier.com/blowfish-download.html
12  *
13  * blowfish.h interface file for blowfish.cpp
14  * _THE BLOWFISH ENCRYPTION ALGORITHM_
15  * by Bruce Schneier
16  * Revised code--3/20/94
17  * Converted to C++ class 5/96, Jim Conger
18  */
19 
20 #ifndef __UT_Blowfish__
21 #define __UT_Blowfish__
22 
23 #include "UT_API.h"
24 #include <SYS/SYS_Types.h>
25 
26 
27 // the size of the s-box used by the blofish:
28 #define UT_BLOWFISH_SBOX_SIZE 256
29 
30 
32 {
33 public:
34  // Construct an encryptor/decryptor object with a given secret key.
35  UT_Blowfish( const uint8 key[], int key_size );
36  ~UT_Blowfish();
37 
38  // Get the amount of memory owned by this UT_Blowfish object
39  int64 getMemoryUsage(bool inclusive) const;
40 
41  // Get output cipher length given the input message length. The output
42  // cipher length must be divisible by 8 because it consists of 64-bit blocks
43  // (8 bytes x 8 bits = 64 bit block).
44  static int getEncryptionOutputSize( int input_size );
45 
46  // Gets the maximum data chunk size on which the algorithm can work and that
47  // is no larger than the specifies 'max_buffer_size'. This is a convenience
48  // method that returns the size that is the multiple of the block size.
49  static int getCryptBufferSize( int max_buffer_size );
50 
51  // Gets the block size. The alogrithm will work on sizes that are multiples
52  // of the block size.
53  static int getBlockSize();
54 
55  // Eencrypt input into output. Input length is input_size. Returned value
56  // is length of output (which will be divisible by 8 bytes).
57  // Input and output buffers can be the same, but be sure buffer length
58  // is divisible by 8, so that it can contain the output cipher.
59  int encrypt( const uint8 * input, uint8 * output,
60  int input_size );
61 
62  // Decrypt input into output. Input length in input_size (and is divisible
63  // by 8, since it contains a number of 64-bit (8-byte) blocks).
64  // Input and output buffer can be the same. The length of output buffer must
65  // be the same as (or larger than) the input buffer. The amount of data
66  // in the output buffer is the same as 'input_size'.
67  // Returns true if all is OK; false if errors occured
68  bool decrypt( const uint8 * input, uint8 * output,
69  int input_size );
70 
71 
72 private:
73  // Constructs the enctryption sieve using the secret key.
74  void initialize( const uint8 key[], int key_size );
75 
76  // Encrypts the 64-bit block that has been split into left and right 32-bit
77  // halves. The encrypted cipher replaces the original data in the block.
78  void encipherBlock( uint32 *xl, uint32 *xr );
79 
80  // Decrypts the 64-bit block that has been split into left and right 32-bit
81  // halves. The decrypted text replaces the original cipher in the block.
82  void decipherBlock( uint32 *xl, uint32 *xr );
83 
84  // Encrypts the 64-bit block from the input source into the destination
85  // output. The source and destination can be the same. The pointers
86  // point to an 8-byte (64-bit) memory chunk.
87  void encipherBlock( const uint8 *input, uint8 *output );
88 
89  // Decrypts the 64-bit block from the input source into the destination
90  // output. The source and destination can be the same. The pointers
91  // point to an 8-byte (64-bit) memory chunk.
92  void decipherBlock( const uint8 *input, uint8 *output );
93 
94  // Array (p-array) containing the subkeys used for xor'ing block's xL half.
95  uint32 *myPArray;
96 
97  // Lookup tables (S-boxes) that implement non-reversible function F.
98  uint32 (*mySBoxes)[ UT_BLOWFISH_SBOX_SIZE ];
99 };
100 
101 #endif // __UT_Blowfish__
102 
103 
104 
#define UT_API
Definition: UT_API.h:12
long long int64
Definition: SYS_Types.h:106
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:316
unsigned char uint8
Definition: SYS_Types.h:31
#define UT_BLOWFISH_SBOX_SIZE
Definition: UT_Blowfish.h:28
unsigned int uint32
Definition: SYS_Types.h:35