pianod2
multisource multiuser scriptable networked music player
Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
BlowFish Class Reference

#include <blowfish.h>

Public Types

using size_type = uint32_t
 

Public Member Functions

 BlowFish (const uint8_t *key, size_type key_length)
 Initialize BlowFish cipher. More...
 
 BlowFish (const std::string &key)
 Initialize the BlowFish cipher. More...
 
void encrypt (const uint8_t *input, size_type input_length, uint8_t *output, size_type output_size) const
 Encipher a BlowFish ECD message. More...
 
void decrypt (const uint8_t *input, size_type input_length, uint8_t *output, size_type output_size) const
 Decipher a BlowFish ECD message. More...
 
std::string encrypt (const std::string &input) const
 Encipher a message with Blowfish ECD. More...
 
std::string decrypt (const std::string &input) const
 Decipher ciphertext encrypted with Blowfish ECD. More...
 
std::string encryptToHex (const std::string &input) const
 Encrypt a string and return it has hex digits. More...
 
std::string decryptFromHex (const std::string &input, bool trim_nulls=true) const
 Decrypt hex-encoded ciphertext and return it, optionally removing null padding that was added. More...
 

Static Public Member Functions

static constexpr size_type getOutputLength (size_type length)
 Get length of required output buffer. More...
 
static std::string to_hex (const uint8_t *bytes, size_type length)
 Translate some bytes into their hexadecimal representation. More...
 
static std::string to_hex (const std::string &input)
 Convert a string to its hexadecimal representation. More...
 
static std::string from_hex (const std::string &digits)
 Translate hexadecimal into a string. More...
 

Private Types

enum class  Action { Encrypt , Decrypt }
 
using PArray = uint32_t[NumberOfPasses+2]
 
using SBoxes = uint32_t[4][256]
 

Private Member Functions

uint32_t fFunction (uint32_t in) const
 The cipher's "F function", used by ciphering/deciphering. More...
 
void encipher (uint32_t *left_32, uint32_t *right_32) const
 Encipher a 64-bit block. More...
 
void decipher (uint32_t *left_32, uint32_t *right_32) const
 Decipher a 64-bit block. More...
 
void encrypt_or_decrypt (Action action, const uint8_t *input, size_type input_length, uint8_t *output, size_type output_size) const
 Encipher or decipher a series of bytes. More...
 

Private Attributes

PArray parray
 
SBoxes sboxes
 

Static Private Attributes

static constexpr int NumberOfPasses = 16
 
static constexpr int MaximumKeyBytes = 56
 
static const PArray InitialPArray
 
static const SBoxes InitialSBoxes
 

Member Typedef Documentation

◆ PArray

using BlowFish::PArray = uint32_t [NumberOfPasses + 2]
private

◆ SBoxes

using BlowFish::SBoxes = uint32_t [4][256]
private

◆ size_type

using BlowFish::size_type = uint32_t

Member Enumeration Documentation

◆ Action

enum BlowFish::Action
strongprivate
Enumerator
Encrypt 
Decrypt 

Constructor & Destructor Documentation

◆ BlowFish() [1/2]

BlowFish::BlowFish ( const uint8_t *  key,
size_type  key_length 
)

Initialize BlowFish cipher.

Parameters
keyThe cipher encryption/decryption key.
key_lengthThe length of the key, in bytes.
Here is the call graph for this function:

◆ BlowFish() [2/2]

BlowFish::BlowFish ( const std::string &  key)

Initialize the BlowFish cipher.

Parameters
keyThe key. The entire string is used, regardless of whitespace and nulls.

Member Function Documentation

◆ decipher()

void BlowFish::decipher ( uint32_t *  left_32,
uint32_t *  right_32 
) const
private

Decipher a 64-bit block.

Parameters
left_32First 32 bits, in host byte (mathy) order.
right_32Second 32 bits, in host byte (mathy) order.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ decrypt() [1/2]

std::string BlowFish::decrypt ( const std::string &  input) const

Decipher ciphertext encrypted with Blowfish ECD.

Parameters
inputThe ciphertext to decipher.
Returns
The deciphered message. Any nulls added during encryption to pad the message to the necessary 64-bit/8-byte boundary are included.
Warning
There is no error checking. If you have the wrong key, you'll get junk out. There is no inherent error detection or validation.
Here is the call graph for this function:

◆ decrypt() [2/2]

void BlowFish::decrypt ( const uint8_t *  input,
size_type  input_length,
uint8_t *  output,
size_type  output_size 
) const
inline

Decipher a BlowFish ECD message.

Parameters
inputThe ciphertext.
input_lengthThe length of the message. This should be a multiple of 8, as all enciphered messages end up a multiple of 8 bytes. Truncated messages will decipher incorrectly.
outputA buffer for the resulting message. This may be the same as the input to encrypt in place.
output_sizeThe size of the output. This must be large enough to accommodate any added padding.
Here is the call graph for this function:

◆ decryptFromHex()

std::string BlowFish::decryptFromHex ( const std::string &  input,
bool  trim_nulls = true 
) const

Decrypt hex-encoded ciphertext and return it, optionally removing null padding that was added.

Whitespace or punctuation in the input are skipped.

Parameters
inputThe ciphertext.
trim_nullsIf true, any trailing nulls are removed.
Returns
If the key is correct, the deciphered message. If not, junk.
Exceptions
invalid_argumentIf the ciphertext has non-hex letters in it.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encipher()

void BlowFish::encipher ( uint32_t *  left_32,
uint32_t *  right_32 
) const
private

Encipher a 64-bit block.

Parameters
left_32First 32 bits, in host byte (mathy) order.
right_32Second 32 bits, in host byte (mathy) order.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encrypt() [1/2]

std::string BlowFish::encrypt ( const std::string &  input) const

Encipher a message with Blowfish ECD.

Warning
Don't use this cipher for new stuff. And don't use ECD.
Parameters
inputThe message to encrypt.
Returns
The encrypted message. This will not be ASCII.
Here is the call graph for this function:

◆ encrypt() [2/2]

void BlowFish::encrypt ( const uint8_t *  input,
size_type  input_length,
uint8_t *  output,
size_type  output_size 
) const
inline

Encipher a BlowFish ECD message.

Warning
Don't use this cipher for new stuff. And don't use ECD.
Parameters
inputThe message to encrypt.
input_lengthThe length of the message.
outputA buffer for the resulting message. This may be the same as the input to encrypt in place.
output_sizeThe size of the output. This must be large enough to accommodate any added padding.
Here is the call graph for this function:

◆ encrypt_or_decrypt()

void BlowFish::encrypt_or_decrypt ( Action  action,
const uint8_t *  input,
size_type  input_length,
uint8_t *  output,
size_type  output_size 
) const
private

Encipher or decipher a series of bytes.

Result is produced in 64-bit (8-byte) chunks; if the input is not an even length, it is padded on the tail end with nulls. Once encrypted, these are part of the ciphertext; if the message is truncated to its original length, it won't decrypt.

Parameters
actionSelects encryption or decryption.
inputThe input message.
input_lengthThe length of the input message.
outputBuffer for the encrypted message.
output_sizeThe size of the buffer. Must be the required size (input length ceilinged to %8==0).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encryptToHex()

std::string BlowFish::encryptToHex ( const std::string &  input) const

Encrypt a string and return it has hex digits.

Parameters
inputThe message to encipher.
Returns
A hex representation of the ciphertext.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fFunction()

uint32_t BlowFish::fFunction ( uint32_t  in) const
private

The cipher's "F function", used by ciphering/deciphering.

Here is the caller graph for this function:

◆ from_hex()

std::string BlowFish::from_hex ( const std::string &  digits)
static

Translate hexadecimal into a string.

Whitespace, punctuation and other junk is ignored.

Parameters
digitsThe digits to translate.
Returns
The unhexed string.
Exceptions
invalid_argumentupon bad input:
  • An odd number of digits.
  • Letters that are not hex digits.
Here is the caller graph for this function:

◆ getOutputLength()

static constexpr size_type BlowFish::getOutputLength ( size_type  length)
inlinestaticconstexpr

Get length of required output buffer.

Parameters
lengthThe length of the input message.
Returns
The length the output buffer must be to accommodate the message.
Here is the caller graph for this function:

◆ to_hex() [1/2]

static std::string BlowFish::to_hex ( const std::string &  input)
inlinestatic

Convert a string to its hexadecimal representation.

Here is the call graph for this function:

◆ to_hex() [2/2]

std::string BlowFish::to_hex ( const uint8_t *  bytes,
size_type  length 
)
static

Translate some bytes into their hexadecimal representation.

Parameters
bytesThe beginning of the byte stream.
lengthThe number of bytes to hex encode.
Returns
A string containing each byte encoded as 2 hex digits.
Here is the caller graph for this function:

Member Data Documentation

◆ InitialPArray

const BlowFish::PArray BlowFish::InitialPArray
staticprivate
Initial value:
= {
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, 0x452821e6,
0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b,
}

◆ InitialSBoxes

const BlowFish::SBoxes BlowFish::InitialSBoxes
staticprivate

◆ MaximumKeyBytes

constexpr int BlowFish::MaximumKeyBytes = 56
staticconstexprprivate

◆ NumberOfPasses

constexpr int BlowFish::NumberOfPasses = 16
staticconstexprprivate

◆ parray

PArray BlowFish::parray
private

◆ sboxes

SBoxes BlowFish::sboxes
private

The documentation for this class was generated from the following files: