Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

cypher.h

Go to the documentation of this file.
00001 /* 00002 * cypher.h 00003 * 00004 * Encryption support classes. 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-2002 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Contributor(s): ______________________________________. 00025 * 00026 * $Log: cypher.h,v $ 00027 * Revision 1.20 2004/03/23 05:59:17 csoutheren 00028 * Moved the Base64 routines into cypher.cxx, which is a more sensible 00029 * place and reduces the inclusion of unrelated code 00030 * 00031 * Revision 1.19 2004/02/04 02:31:34 csoutheren 00032 * Remove SHA-1 functions when OpenSSL is disabled 00033 * 00034 * Revision 1.18 2003/04/17 03:34:07 craigs 00035 * Fixed problem with delete'ing a void * 00036 * 00037 * Revision 1.17 2003/04/10 07:02:38 craigs 00038 * Fixed link problem in MD5 class 00039 * 00040 * Revision 1.16 2003/04/10 06:16:30 craigs 00041 * Added SHA-1 digest 00042 * 00043 * Revision 1.15 2002/11/06 22:47:23 robertj 00044 * Fixed header comment (copyright etc) 00045 * 00046 * Revision 1.14 2002/09/16 01:08:59 robertj 00047 * Added #define so can select if #pragma interface/implementation is used on 00048 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00049 * 00050 * Revision 1.13 2001/09/10 00:28:21 robertj 00051 * Fixed extra CR in comments. 00052 * 00053 * Revision 1.12 1999/03/09 08:01:46 robertj 00054 * Changed comments for doc++ support (more to come). 00055 * 00056 * Revision 1.11 1999/02/16 08:07:10 robertj 00057 * MSVC 6.0 compatibility changes. 00058 * 00059 * Revision 1.10 1998/09/23 06:19:24 robertj 00060 * Added open source copyright license. 00061 * 00062 * Revision 1.9 1997/10/10 10:44:01 robertj 00063 * Fixed bug in password encryption, missing string terminator. 00064 * 00065 * Revision 1.8 1996/11/16 10:50:24 robertj 00066 * Fixed bug in registration order form showing incorrect check code when have key. 00067 * 00068 * Revision 1.7 1996/07/15 10:29:38 robertj 00069 * Changed memory block cypher conversion functions to be void *. 00070 * Changed key types to be structures rather than arrays to avoid pinter/reference confusion by compilers. 00071 * 00072 * Revision 1.6 1996/03/17 05:47:00 robertj 00073 * Changed secured config to allow for expiry dates. 00074 * 00075 * Revision 1.5 1996/03/16 04:36:43 robertj 00076 * Redesign of secure config to accommodate expiry dates and option values passed in security key code. 00077 * 00078 * Revision 1.4 1996/02/25 02:52:46 robertj 00079 * Further secure config development. 00080 * 00081 * Revision 1.3 1996/01/28 14:16:11 robertj 00082 * Further implementation of secure config. 00083 * 00084 * Revision 1.2 1996/01/28 02:41:00 robertj 00085 * Removal of MemoryPointer classes as usage didn't work for GNU. 00086 * Added the secure configuration mechanism for protecting applications. 00087 * 00088 * Revision 1.1 1996/01/23 13:04:20 robertj 00089 * Initial revision 00090 * 00091 */ 00092 00093 00094 #ifndef _PCYPHER 00095 #define _PCYPHER 00096 00097 #ifdef P_USE_PRAGMA 00098 #pragma interface 00099 #endif 00100 00131 class PBase64 : public PObject 00132 { 00133 PCLASSINFO(PBase64, PObject); 00134 00135 public: 00139 PBase64(); 00140 00141 void StartEncoding( 00142 BOOL useCRLFs = TRUE // Use CR, LF pairs in end of line characters. 00143 ); 00144 // Begin a base 64 encoding operation, initialising the object instance. 00145 00146 void ProcessEncoding( 00147 const PString & str // String to be encoded 00148 ); 00149 void ProcessEncoding( 00150 const char * cstr // C String to be encoded 00151 ); 00152 void ProcessEncoding( 00153 const PBYTEArray & data // Data block to be encoded 00154 ); 00155 void ProcessEncoding( 00156 const void * dataBlock, // Pointer to data to be encoded 00157 PINDEX length // Length of the data block. 00158 ); 00159 // Incorporate the specified data into the base 64 encoding. 00160 00166 PString GetEncodedString(); 00167 00175 PString CompleteEncoding(); 00176 00177 00178 static PString Encode( 00179 const PString & str // String to be encoded to Base64 00180 ); 00181 static PString Encode( 00182 const char * cstr // C String to be encoded to Base64 00183 ); 00184 static PString Encode( 00185 const PBYTEArray & data // Data block to be encoded to Base64 00186 ); 00187 static PString Encode( 00188 const void * dataBlock, // Pointer to data to be encoded to Base64 00189 PINDEX length // Length of the data block. 00190 ); 00191 // Encode the data in memory to Base 64 data returnin the string. 00192 00193 00194 void StartDecoding(); 00195 // Begin a base 64 decoding operation, initialising the object instance. 00196 00202 BOOL ProcessDecoding( 00203 const PString & str // String to be encoded 00204 ); 00205 BOOL ProcessDecoding( 00206 const char * cstr // C String to be encoded 00207 ); 00208 00214 BOOL GetDecodedData( 00215 void * dataBlock, // Pointer to data to be decoded from base64 00216 PINDEX length // Length of the data block. 00217 ); 00218 PBYTEArray GetDecodedData(); 00219 00227 BOOL IsDecodeOK() { return perfectDecode; } 00228 00229 00241 static PString Decode( 00242 const PString & str // Encoded base64 string to be decoded. 00243 ); 00244 static BOOL Decode( 00245 const PString & str, // Encoded base64 string to be decoded. 00246 PBYTEArray & data // Converted binary data from base64. 00247 ); 00248 static BOOL Decode( 00249 const PString & str, // Encoded base64 string to be decoded. 00250 void * dataBlock, // Pointer to data to be decoded from base64 00251 PINDEX length // Length of the data block. 00252 ); 00253 00254 00255 00256 private: 00257 void OutputBase64(const BYTE * data); 00258 00259 PString encodedString; 00260 PINDEX encodeLength; 00261 BYTE saveTriple[3]; 00262 PINDEX saveCount; 00263 PINDEX nextLine; 00264 BOOL useCRLFs; 00265 00266 BOOL perfectDecode; 00267 PINDEX quadPosition; 00268 PBYTEArray decodedData; 00269 PINDEX decodeSize; 00270 }; 00271 00272 class PMessageDigest : public PObject 00273 { 00274 PCLASSINFO(PMessageDigest, PObject) 00275 00276 public: 00278 PMessageDigest(); 00279 00280 class Result { 00281 public: 00282 PINDEX GetSize() const { return value.GetSize(); } 00283 const BYTE * GetPointer() const { return (const BYTE *)value; } 00284 00285 private: 00286 PBYTEArray value; 00287 friend class PMessageDigest5; 00288 friend class PMessageDigestSHA1; 00289 }; 00290 00292 virtual void Start() = 0; 00293 00294 virtual void Process( 00295 const void * dataBlock, 00296 PINDEX length 00297 ); 00298 00300 virtual void Process( 00301 const PString & str 00302 ); 00304 virtual void Process( 00305 const char * cstr 00306 ); 00308 virtual void Process( 00309 const PBYTEArray & data 00310 ); 00311 00319 virtual PString CompleteDigest(); 00320 virtual void CompleteDigest( 00321 Result & result 00322 ); 00323 00324 protected: 00325 virtual void InternalProcess( 00326 const void * dataBlock, 00327 PINDEX length 00328 ) = 0; 00329 00330 virtual void InternalCompleteDigest( 00331 Result & result 00332 ) = 0; 00333 }; 00334 00335 00341 class PMessageDigest5 : public PMessageDigest 00342 { 00343 PCLASSINFO(PMessageDigest5, PMessageDigest) 00344 00345 public: 00347 PMessageDigest5(); 00348 00350 void Start(); 00351 00353 static PString Encode( 00354 const PString & str 00355 ); 00357 static void Encode( 00358 const PString & str, 00359 Result & result 00360 ); 00362 static PString Encode( 00363 const char * cstr 00364 ); 00366 static void Encode( 00367 const char * cstr, 00368 Result & result 00369 ); 00371 static PString Encode( 00372 const PBYTEArray & data 00373 ); 00375 static void Encode( 00376 const PBYTEArray & data, 00377 Result & result 00378 ); 00380 static PString Encode( 00381 const void * dataBlock, 00382 PINDEX length 00383 ); 00389 static void Encode( 00390 const void * dataBlock, 00391 PINDEX length, 00392 Result & result 00393 ); 00394 00395 // backwards compatibility functions 00396 class Code { 00397 private: 00398 PUInt32l value[4]; 00399 friend class PMessageDigest5; 00400 }; 00401 00403 static void Encode( 00404 const PString & str, 00405 Code & result 00406 ); 00408 static void Encode( 00409 const char * cstr, 00410 Code & result 00411 ); 00413 static void Encode( 00414 const PBYTEArray & data, 00415 Code & result 00416 ); 00422 static void Encode( 00423 const void * dataBlock, 00424 PINDEX length, 00425 Code & result 00426 ); 00427 virtual void Complete( 00428 Code & result 00429 ); 00430 virtual PString Complete(); 00431 00432 protected: 00433 virtual void InternalProcess( 00434 const void * dataBlock, 00435 PINDEX length 00436 ); 00437 00438 virtual void InternalCompleteDigest( 00439 Result & result 00440 ); 00441 00442 private: 00443 void Transform(const BYTE * block); 00444 00446 BYTE buffer[64]; 00448 DWORD state[4]; 00450 PUInt64 count; 00451 }; 00452 00453 #if P_SSL 00454 00459 class PMessageDigestSHA1 : public PMessageDigest 00460 { 00461 PCLASSINFO(PMessageDigestSHA1, PMessageDigest) 00462 00463 public: 00465 PMessageDigestSHA1(); 00466 ~PMessageDigestSHA1(); 00467 00469 void Start(); 00470 00472 static PString Encode( 00473 const PString & str 00474 ); 00476 static void Encode( 00477 const PString & str, 00478 Result & result 00479 ); 00481 static PString Encode( 00482 const char * cstr 00483 ); 00485 static void Encode( 00486 const char * cstr, 00487 Result & result 00488 ); 00490 static PString Encode( 00491 const PBYTEArray & data 00492 ); 00494 static void Encode( 00495 const PBYTEArray & data, 00496 Result & result 00497 ); 00499 static PString Encode( 00500 const void * dataBlock, 00501 PINDEX length 00502 ); 00508 static void Encode( 00509 const void * dataBlock, 00510 PINDEX length, 00511 Result & result 00512 ); 00513 00514 protected: 00515 virtual void InternalProcess( 00516 const void * dataBlock, 00517 PINDEX length 00518 ); 00519 00520 void InternalCompleteDigest( 00521 Result & result 00522 ); 00523 00524 private: 00525 void * shaContext; 00526 }; 00527 00528 #endif 00529 00533 class PCypher : public PObject 00534 { 00535 PCLASSINFO(PCypher, PObject) 00536 00537 public: 00539 enum BlockChainMode { 00540 ElectronicCodebook, 00541 ECB = ElectronicCodebook, 00542 CypherBlockChaining, 00543 CBC = CypherBlockChaining, 00544 OutputFeedback, 00545 OFB = OutputFeedback, 00546 CypherFeedback, 00547 CFB = CypherFeedback, 00548 NumBlockChainModes 00549 }; 00550 00551 // New functions for class 00553 PString Encode( 00554 const PString & str 00555 ); 00557 PString Encode( 00558 const PBYTEArray & clear 00559 ); 00561 PString Encode( 00562 const void * data, 00563 PINDEX length 00564 ); 00566 void Encode( 00567 const PBYTEArray & clear, 00568 PBYTEArray & coded 00569 ); 00585 void Encode( 00586 const void * data, // Clear text binary data to be encoded. 00587 PINDEX length, // Number of bytes of data to be encoded. 00588 PBYTEArray & coded // Encoded data. 00589 ); 00590 00592 PString Decode( 00593 const PString & cypher 00594 ); 00596 BOOL Decode( 00597 const PString & cypher, 00598 PString & clear 00599 ); 00601 BOOL Decode( 00602 const PString & cypher, 00603 PBYTEArray & clear 00604 ); 00606 PINDEX Decode( 00607 const PString & cypher, 00608 void * data, 00609 PINDEX length 00610 ); 00612 PINDEX Decode( 00613 const PBYTEArray & coded, 00614 void * data, 00615 PINDEX length 00616 ); 00632 BOOL Decode( 00633 const PBYTEArray & coded, 00634 PBYTEArray & clear 00635 ); 00636 00637 00638 protected: 00642 PCypher( 00643 PINDEX blockSize, 00644 BlockChainMode chainMode 00645 ); 00646 PCypher( 00647 const void * keyData, 00648 PINDEX keyLength, 00649 PINDEX blockSize, 00650 BlockChainMode chainMode 00651 ); 00652 00653 00655 virtual void Initialise( 00656 BOOL encoding 00657 ) = 0; 00658 00660 virtual void EncodeBlock( 00661 const void * in, 00662 void * out 00663 ) = 0; 00664 00665 00667 virtual void DecodeBlock( 00668 const void * in, 00669 void * out 00670 ) = 0; 00671 00672 00674 PBYTEArray key; 00676 PINDEX blockSize; 00678 BlockChainMode chainMode; 00679 }; 00680 00681 00689 class PTEACypher : public PCypher 00690 { 00691 PCLASSINFO(PTEACypher, PCypher) 00692 00693 public: 00694 struct Key { 00695 BYTE value[16]; 00696 }; 00697 00702 PTEACypher( 00703 BlockChainMode chainMode = ElectronicCodebook 00704 ); 00705 PTEACypher( 00706 const Key & keyData, 00707 BlockChainMode chainMode = ElectronicCodebook 00708 ); 00709 00710 00712 void SetKey( 00713 const Key & newKey 00714 ); 00715 00717 void GetKey( 00718 Key & newKey 00719 ) const; 00720 00721 00723 static void GenerateKey( 00724 Key & newKey 00725 ); 00726 00727 00728 protected: 00730 virtual void Initialise( 00731 BOOL encoding 00732 ); 00733 00735 virtual void EncodeBlock( 00736 const void * in, 00737 void * out 00738 ); 00739 00741 virtual void DecodeBlock( 00742 const void * in, 00743 void * out 00744 ); 00745 00746 private: 00747 DWORD k0, k1, k2, k3; 00748 }; 00749 00750 00751 00752 class PSecureConfig : public PConfig 00753 { 00754 PCLASSINFO(PSecureConfig, PConfig) 00755 /* This class defines a set of configuration keys which may be secured by an 00756 encrypted hash function. Thus values contained in keys specified by this 00757 class cannot be changed without invalidating the hash function. 00758 */ 00759 00760 public: 00761 PSecureConfig( 00762 const PTEACypher::Key & productKey, // Key to decrypt validation code. 00763 const PStringArray & securedKeys, // List of secured keys. 00764 Source src = Application // Standard source for the configuration. 00765 ); 00766 PSecureConfig( 00767 const PTEACypher::Key & productKey, // Key to decrypt validation code. 00768 const char * const * securedKeyArray, // List of secured keys. 00769 PINDEX count, // Number of secured keys in list. 00770 Source src = Application // Standard source for the configuration. 00771 ); 00772 /* Create a secured configuration. The default section for the 00773 configuration keys is "Secured Options", the default security key is 00774 "Validation" and the defualt prefix string is "Pending:". 00775 00776 The user can descend from this class and change any of the member 00777 variable for the names of keys or the configuration file section. 00778 */ 00779 00780 00781 // New functions for class 00782 const PStringArray & GetSecuredKeys() const { return securedKeys; } 00783 /* Get the list of secured keys in the configuration file section. 00784 00785 @return 00786 Array of strings for the secured keys. 00787 */ 00788 00789 const PString & GetSecurityKey() const { return securityKey; } 00790 /* Get the security keys name in the configuration file section. 00791 00792 @return 00793 String for the security values key. 00794 */ 00795 00796 const PString & GetExpiryDateKey() const { return expiryDateKey; } 00797 /* Get the expiry date keys name in the configuration file section. 00798 00799 @return 00800 String for the expiry date values key. 00801 */ 00802 00803 const PString & GetOptionBitsKey() const { return optionBitsKey; } 00804 /* Get the Option Bits keys name in the configuration file section. 00805 00806 @return 00807 String for the Option Bits values key. 00808 */ 00809 00810 const PString & GetPendingPrefix() const { return pendingPrefix; } 00811 /* Get the pending prefix name in the configuration file section. 00812 00813 @return 00814 String for the pending prefix. 00815 */ 00816 00817 void GetProductKey( 00818 PTEACypher::Key & productKey // Variable to receive the product key. 00819 ) const; 00820 /* Get the pending prefix name in the configuration file section. 00821 00822 @return 00823 String for the pending prefix. 00824 */ 00825 00826 00827 enum ValidationState { 00828 Defaults, 00829 Pending, 00830 IsValid, 00831 Expired, 00832 Invalid 00833 }; 00834 ValidationState GetValidation() const; 00835 /* Check the current values attached to the keys specified in the 00836 constructor against an encoded validation key. 00837 00838 @return 00839 State of the validation keys. 00840 */ 00841 00842 BOOL ValidatePending(); 00843 /* Validate a pending secured option list for the product. All secured 00844 keys with the <CODE>pendingPrefix</CODE> name will be checked against 00845 the value of the field <CODE>securityKey</CODE>. If they match then 00846 they are copied to the secured variables. 00847 00848 @return 00849 TRUE if secure key values are valid. 00850 */ 00851 00852 void ResetPending(); 00853 /* "Unvalidate" a security configuration going back to a pending state, 00854 usually used after an <CODE>Invalid</CODE> response was recieved from 00855 the <A>GetValidation()</A> function. 00856 */ 00857 00858 00859 protected: 00860 PTEACypher::Key productKey; 00861 PStringArray securedKeys; 00862 PString securityKey; 00863 PString expiryDateKey; 00864 PString optionBitsKey; 00865 PString pendingPrefix; 00866 }; 00867 00868 00869 #endif // _PCYPHER 00870 00871 00872 // End Of File ///////////////////////////////////////////////////////////////

Generated on Sat Jul 24 15:35:56 2004 for PWLib by doxygen 1.3.7