All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cypher.h
Go to the documentation of this file.
/*
* cypher.h
*
* Encryption support classes.
*
* Portable Windows Library
*
* Copyright (c) 1993-2002 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Contributor(s): ______________________________________.
*
* $Revision: 24983 $
* $Author: rjongbloed $
* $Date: 2010-12-22 23:23:23 -0600 (Wed, 22 Dec 2010) $
*/
#ifndef PTLIB_CYPHER_H
#define PTLIB_CYPHER_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
class PBase64 : public PObject
{
PCLASSINFO(PBase64, PObject);
public:
PBase64();
void StartEncoding(
bool useCRLFs = true
);
void StartEncoding(
const char * endOfLine
);
// Begin a base 64 encoding operation, initialising the object instance.
void ProcessEncoding(
const PString & str // String to be encoded
);
void ProcessEncoding(
const char * cstr // C String to be encoded
);
void ProcessEncoding(
const PBYTEArray & data // Data block to be encoded
);
void ProcessEncoding(
const void * dataBlock, // Pointer to data to be encoded
PINDEX length // Length of the data block.
);
// Incorporate the specified data into the base 64 encoding.
PString GetEncodedString();
PString CompleteEncoding();
static PString Encode(
const PString & str,
const char * endOfLine = "\n"
);
static PString Encode(
const char * cstr,
const char * endOfLine = "\n"
);
static PString Encode(
const PBYTEArray & data,
const char * endOfLine = "\n"
);
static PString Encode(
const void * dataBlock,
PINDEX length,
const char * endOfLine = "\n"
);
// Encode the data in memory to Base 64 data returnin the string.
void StartDecoding();
// Begin a base 64 decoding operation, initialising the object instance.
PBoolean ProcessDecoding(
const PString & str // String to be encoded
);
PBoolean ProcessDecoding(
const char * cstr // C String to be encoded
);
PBoolean GetDecodedData(
void * dataBlock, // Pointer to data to be decoded from base64
PINDEX length // Length of the data block.
);
PBYTEArray GetDecodedData();
PBoolean IsDecodeOK() { return perfectDecode; }
static PString Decode(
const PString & str // Encoded base64 string to be decoded.
);
static PBoolean Decode(
const PString & str, // Encoded base64 string to be decoded.
PBYTEArray & data // Converted binary data from base64.
);
static PBoolean Decode(
const PString & str, // Encoded base64 string to be decoded.
void * dataBlock, // Pointer to data to be decoded from base64
PINDEX length // Length of the data block.
);
private:
void OutputBase64(const BYTE * data);
PString encodedString;
PINDEX encodeLength;
BYTE saveTriple[3];
PINDEX saveCount;
PINDEX nextLine;
PString endOfLine;
PBoolean perfectDecode;
PINDEX quadPosition;
PBYTEArray decodedData;
PINDEX decodeSize;
};
class PMessageDigest : public PObject
{
PCLASSINFO(PMessageDigest, PObject)
public:
PMessageDigest();
class Result {
public:
PINDEX GetSize() const { return value.GetSize(); }
const BYTE * GetPointer() const { return (const BYTE *)value; }
private:
PBYTEArray value;
friend class PMessageDigest5;
friend class PMessageDigestSHA1;
};
virtual void Start() = 0;
virtual void Process(
const void * dataBlock,
PINDEX length
);
virtual void Process(
const PString & str
);
virtual void Process(
const char * cstr
);
virtual void Process(
const PBYTEArray & data
);
virtual PString CompleteDigest();
virtual void CompleteDigest(
Result & result
);
protected:
virtual void InternalProcess(
const void * dataBlock,
PINDEX length
) = 0;
virtual void InternalCompleteDigest(
Result & result
) = 0;
};
class PMessageDigest5 : public PMessageDigest
{
PCLASSINFO(PMessageDigest5, PMessageDigest)
public:
PMessageDigest5();
void Start();
static PString Encode(
const PString & str
);
static void Encode(
const PString & str,
Result & result
);
static PString Encode(
const char * cstr
);
static void Encode(
const char * cstr,
Result & result
);
static PString Encode(
const PBYTEArray & data
);
static void Encode(
const PBYTEArray & data,
Result & result
);
static PString Encode(
const void * dataBlock,
PINDEX length
);
static void Encode(
const void * dataBlock,
PINDEX length,
Result & result
);
// backwards compatibility functions
class Code {
private:
PUInt32l value[4];
friend class PMessageDigest5;
};
static void Encode(
const PString & str,
Code & result
);
static void Encode(
const char * cstr,
Code & result
);
static void Encode(
const PBYTEArray & data,
Code & result
);
static void Encode(
const void * dataBlock,
PINDEX length,
Code & result
);
virtual void Complete(
Code & result
);
virtual PString Complete();
protected:
virtual void InternalProcess(
const void * dataBlock,
PINDEX length
);
virtual void InternalCompleteDigest(
Result & result
);
private:
void Transform(const BYTE * block);
BYTE buffer[64];
DWORD state[4];
PUInt64 count;
};
#if P_SSL
class PMessageDigestSHA1 : public PMessageDigest
{
PCLASSINFO(PMessageDigestSHA1, PMessageDigest)
public:
PMessageDigestSHA1();
~PMessageDigestSHA1();
void Start();
static PString Encode(
const PString & str
);
static void Encode(
const PString & str,
Result & result
);
static PString Encode(
const char * cstr
);
static void Encode(
const char * cstr,
Result & result
);
static PString Encode(
const PBYTEArray & data
);
static void Encode(
const PBYTEArray & data,
Result & result
);
static PString Encode(
const void * dataBlock,
PINDEX length
);
static void Encode(
const void * dataBlock,
PINDEX length,
Result & result
);
protected:
virtual void InternalProcess(
const void * dataBlock,
PINDEX length
);
void InternalCompleteDigest(
Result & result
);
private:
void * shaContext;
};
#endif
class PCypher : public PObject
{
PCLASSINFO(PCypher, PObject)
public:
enum BlockChainMode {
ElectronicCodebook,
ECB = ElectronicCodebook,
CypherBlockChaining,
CBC = CypherBlockChaining,
OutputFeedback,
OFB = OutputFeedback,
CypherFeedback,
CFB = CypherFeedback,
NumBlockChainModes
};
// New functions for class
PString Encode(
const PString & str
);
PString Encode(
const PBYTEArray & clear
);
PString Encode(
const void * data,
PINDEX length
);
void Encode(
const PBYTEArray & clear,
PBYTEArray & coded
);
void Encode(
const void * data, // Clear text binary data to be encoded.
PINDEX length, // Number of bytes of data to be encoded.
PBYTEArray & coded // Encoded data.
);
PString Decode(
const PString & cypher
);
PBoolean Decode(
const PString & cypher,
PString & clear
);
PBoolean Decode(
const PString & cypher,
PBYTEArray & clear
);
PINDEX Decode(
const PString & cypher,
void * data,
PINDEX length
);
PINDEX Decode(
const PBYTEArray & coded,
void * data,
PINDEX length
);
PBoolean Decode(
const PBYTEArray & coded,
PBYTEArray & clear
);
protected:
PCypher(
PINDEX blockSize,
BlockChainMode chainMode
);
PCypher(
const void * keyData,
PINDEX keyLength,
PINDEX blockSize,
BlockChainMode chainMode
);
virtual void Initialise(
PBoolean encoding
) = 0;
virtual void EncodeBlock(
const void * in,
void * out
) = 0;
virtual void DecodeBlock(
const void * in,
void * out
) = 0;
PBYTEArray key;
PINDEX blockSize;
BlockChainMode chainMode;
};
class PTEACypher : public PCypher
{
PCLASSINFO(PTEACypher, PCypher)
public:
struct Key {
BYTE value[16];
};
PTEACypher(
BlockChainMode chainMode = ElectronicCodebook
);
PTEACypher(
const Key & keyData,
BlockChainMode chainMode = ElectronicCodebook
);
void SetKey(
const Key & newKey
);
void GetKey(
Key & newKey
) const;
static void GenerateKey(
Key & newKey
);
protected:
virtual void Initialise(
PBoolean encoding
);
virtual void EncodeBlock(
const void * in,
void * out
);
virtual void DecodeBlock(
const void * in,
void * out
);
private:
DWORD k0, k1, k2, k3;
};
#ifdef P_CONFIG_FILE
class PSecureConfig : public PConfig
{
PCLASSINFO(PSecureConfig, PConfig)
/* This class defines a set of configuration keys which may be secured by an
encrypted hash function. Thus values contained in keys specified by this
class cannot be changed without invalidating the hash function.
*/
public:
PSecureConfig(
const PTEACypher::Key & productKey, // Key to decrypt validation code.
const PStringArray & securedKeys, // List of secured keys.
Source src = Application // Standard source for the configuration.
);
PSecureConfig(
const PTEACypher::Key & productKey, // Key to decrypt validation code.
const char * const * securedKeyArray, // List of secured keys.
PINDEX count, // Number of secured keys in list.
Source src = Application // Standard source for the configuration.
);
/* Create a secured configuration. The default section for the
configuration keys is "Secured Options", the default security key is
"Validation" and the defualt prefix string is "Pending:".
The user can descend from this class and change any of the member
variable for the names of keys or the configuration file section.
*/
// New functions for class
const PStringArray & GetSecuredKeys() const { return securedKeys; }
/* Get the list of secured keys in the configuration file section.
@return
Array of strings for the secured keys.
*/
const PString & GetSecurityKey() const { return securityKey; }
/* Get the security keys name in the configuration file section.
@return
String for the security values key.
*/
const PString & GetExpiryDateKey() const { return expiryDateKey; }
/* Get the expiry date keys name in the configuration file section.
@return
String for the expiry date values key.
*/
const PString & GetOptionBitsKey() const { return optionBitsKey; }
/* Get the Option Bits keys name in the configuration file section.
@return
String for the Option Bits values key.
*/
const PString & GetPendingPrefix() const { return pendingPrefix; }
/* Get the pending prefix name in the configuration file section.
@return
String for the pending prefix.
*/
void GetProductKey(
PTEACypher::Key & productKey // Variable to receive the product key.
) const;
/* Get the pending prefix name in the configuration file section.
@return
String for the pending prefix.
*/
enum ValidationState {
Defaults,
Pending,
IsValid,
Expired,
Invalid
};
ValidationState GetValidation() const;
/* Check the current values attached to the keys specified in the
constructor against an encoded validation key.
@return
State of the validation keys.
*/
PBoolean ValidatePending();
/* Validate a pending secured option list for the product. All secured
keys with the <CODE>pendingPrefix</CODE> name will be checked against
the value of the field <CODE>securityKey</CODE>. If they match then
they are copied to the secured variables.
@return
true if secure key values are valid.
*/
void ResetPending();
/* "Unvalidate" a security configuration going back to a pending state,
usually used after an <CODE>Invalid</CODE> response was recieved from
the <A>GetValidation()</A> function.
*/
protected:
PTEACypher::Key productKey;
PStringArray securedKeys;
PString securityKey;
PString expiryDateKey;
PString optionBitsKey;
PString pendingPrefix;
};
#endif // P_CONFIG_FILE
#endif // PTLIB_CYPHER_H
// End Of File ///////////////////////////////////////////////////////////////
PMessageDigest::Start
virtual void Start()=0
Begin a Message Digest operation, initialising the object instance.
PSecureConfig::Defaults
Definition: cypher.h:774
PCypher::CypherBlockChaining
Definition: cypher.h:487
PSecureConfig::GetOptionBitsKey
const PString & GetOptionBitsKey() const
Definition: cypher.h:749
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PBase64
This class is used to encode/decode data using the MIME standard base64 encoding mechanism as defined...
Definition: cypher.h:70
PMessageDigest::Result::PMessageDigestSHA1
friend class PMessageDigestSHA1
Definition: cypher.h:233
PConfig
A class representing a configuration for the application.
Definition: config.h:67
PSecureConfig::optionBitsKey
PString optionBitsKey
Definition: cypher.h:810
PMessageDigest::Result
Definition: cypher.h:225
PBase64::StartDecoding
void StartDecoding()
PSecureConfig::GetSecuredKeys
const PStringArray & GetSecuredKeys() const
Definition: cypher.h:728
PSecureConfig::Pending
Definition: cypher.h:775
PBase64::Encode
static PString Encode(const PString &str, const char *endOfLine="\n")
PMessageDigest::InternalProcess
virtual void InternalProcess(const void *dataBlock, PINDEX length)=0
PBase64::StartEncoding
void StartEncoding(bool useCRLFs=true)
PSecureConfig::IsValid
Definition: cypher.h:776
PStringArray
This is an array collection class of PString objects.
Definition: pstring.h:2024
PSecureConfig::securedKeys
PStringArray securedKeys
Definition: cypher.h:807
PBase64::IsDecodeOK
PBoolean IsDecodeOK()
Return a flag to indicate that the input was decoded without any extraneous or illegal characters in ...
Definition: cypher.h:172
PBase64::ProcessDecoding
PBoolean ProcessDecoding(const PString &str)
Incorporate the specified data into the base 64 decoding.
PCypher::blockSize
PINDEX blockSize
Size of each encryption block in bytes.
Definition: cypher.h:621
PBoolean
BOOL PBoolean
Definition: object.h:102
PCypher::BlockChainMode
BlockChainMode
Mechanism by which sequential blocks are linked.
Definition: cypher.h:484
PBYTEArray
Array of unsigned characters.
Definition: array.h:670
PBase64::GetDecodedData
PBYTEArray GetDecodedData()
PSecureConfig::GetPendingPrefix
const PString & GetPendingPrefix() const
Definition: cypher.h:756
PMessageDigest5::Encode
static PString Encode(const PString &str)
Encode the data in memory to and MD5 hash value.
PConfig::Source
Source
Description of the standard source for configuration information.
Definition: config.h:76
PCypher::CypherFeedback
Definition: cypher.h:491
PMessageDigest
Definition: cypher.h:217
PMessageDigest5::InternalCompleteDigest
virtual void InternalCompleteDigest(Result &result)
PMessageDigest5::InternalProcess
virtual void InternalProcess(const void *dataBlock, PINDEX length)
PCypher
This abstract class defines an encryption/decryption algortihm.
Definition: cypher.h:478
PContainer::GetSize
virtual PINDEX GetSize() const
Get the current size of the container.
PSecureConfig
Definition: cypher.h:698
PMessageDigest5::Complete
virtual PString Complete()
PSecureConfig::securityKey
PString securityKey
Definition: cypher.h:808
PSecureConfig::expiryDateKey
PString expiryDateKey
Definition: cypher.h:809
PMessageDigest5::PMessageDigest5
PMessageDigest5()
Create a new message digestor.
PString
The character string class.
Definition: pstring.h:108
PTEACypher::Key
Definition: cypher.h:639
PMessageDigest::PMessageDigest
PMessageDigest()
Create a new message digestor.
PCypher::key
PBYTEArray key
Key for the encryption/decryption.
Definition: cypher.h:619
PMessageDigest::Result::GetPointer
const BYTE * GetPointer() const
Definition: cypher.h:228
PMessageDigest5
MD5 Message Digest.
Definition: cypher.h:286
PMessageDigest::CompleteDigest
virtual PString CompleteDigest()
Complete the message digest and return the magic number result.
PSecureConfig::GetSecurityKey
const PString & GetSecurityKey() const
Definition: cypher.h:735
PCypher::chainMode
BlockChainMode chainMode
Mode for sequential encryption each block.
Definition: cypher.h:623
PCypher::ElectronicCodebook
Definition: cypher.h:485
PBase64::PBase64
PBase64()
Construct a base 64 encoder/decoder and initialise both encode and decode members as in StartEncoding...
PMessageDigest::Result::GetSize
PINDEX GetSize() const
Definition: cypher.h:227
PMessageDigest::InternalCompleteDigest
virtual void InternalCompleteDigest(Result &result)=0
PSecureConfig::ValidationState
ValidationState
Definition: cypher.h:773
PMessageDigest5::Start
void Start()
Begin a Message Digest operation, initialising the object instance.
PSecureConfig::pendingPrefix
PString pendingPrefix
Definition: cypher.h:811
PSecureConfig::productKey
PTEACypher::Key productKey
Definition: cypher.h:806
PBase64::ProcessEncoding
void ProcessEncoding(const PString &str)
PCypher::OutputFeedback
Definition: cypher.h:489
PBase64::Decode
static PString Decode(const PString &str)
Convert a printable text string to binary data using the Internet MIME standard base 64 content trans...
PBase64::CompleteEncoding
PString CompleteEncoding()
Complete the base 64 encoding and return the remainder of the encoded Base64 string.
PBase64::GetEncodedString
PString GetEncodedString()
Get the partial Base64 string for the data encoded so far.
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PSecureConfig::GetExpiryDateKey
const PString & GetExpiryDateKey() const
Definition: cypher.h:742
PMessageDigest::Process
virtual void Process(const void *dataBlock, PINDEX length)
PSecureConfig::Expired
Definition: cypher.h:777
PTEACypher
Tiny Encryption Algorithm.
Definition: cypher.h:634
PMessageDigest5::Code
Definition: cypher.h:341