PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cypher.h
Go to the documentation of this file.
1 /*
2  * cypher.h
3  *
4  * Encryption support classes.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 24983 $
27  * $Author: rjongbloed $
28  * $Date: 2010-12-22 23:23:23 -0600 (Wed, 22 Dec 2010) $
29  */
30 
31 
32 #ifndef PTLIB_CYPHER_H
33 #define PTLIB_CYPHER_H
34 
35 #ifdef P_USE_PRAGMA
36 #pragma interface
37 #endif
38 
39 
70 class PBase64 : public PObject
71 {
72  PCLASSINFO(PBase64, PObject);
73 
74  public:
78  PBase64();
79 
80  void StartEncoding(
81  bool useCRLFs = true
82  );
83  void StartEncoding(
84  const char * endOfLine
85  );
86  // Begin a base 64 encoding operation, initialising the object instance.
87 
88  void ProcessEncoding(
89  const PString & str // String to be encoded
90  );
91  void ProcessEncoding(
92  const char * cstr // C String to be encoded
93  );
94  void ProcessEncoding(
95  const PBYTEArray & data // Data block to be encoded
96  );
97  void ProcessEncoding(
98  const void * dataBlock, // Pointer to data to be encoded
99  PINDEX length // Length of the data block.
100  );
101  // Incorporate the specified data into the base 64 encoding.
102 
109 
117 
118 
119  static PString Encode(
120  const PString & str,
121  const char * endOfLine = "\n"
122  );
123  static PString Encode(
124  const char * cstr,
125  const char * endOfLine = "\n"
126  );
127  static PString Encode(
128  const PBYTEArray & data,
129  const char * endOfLine = "\n"
130  );
131  static PString Encode(
132  const void * dataBlock,
133  PINDEX length,
134  const char * endOfLine = "\n"
135  );
136  // Encode the data in memory to Base 64 data returnin the string.
137 
138 
139  void StartDecoding();
140  // Begin a base 64 decoding operation, initialising the object instance.
141 
148  const PString & str // String to be encoded
149  );
151  const char * cstr // C String to be encoded
152  );
153 
160  void * dataBlock, // Pointer to data to be decoded from base64
161  PINDEX length // Length of the data block.
162  );
164 
172  PBoolean IsDecodeOK() { return perfectDecode; }
173 
174 
186  static PString Decode(
187  const PString & str // Encoded base64 string to be decoded.
188  );
189  static PBoolean Decode(
190  const PString & str, // Encoded base64 string to be decoded.
191  PBYTEArray & data // Converted binary data from base64.
192  );
193  static PBoolean Decode(
194  const PString & str, // Encoded base64 string to be decoded.
195  void * dataBlock, // Pointer to data to be decoded from base64
196  PINDEX length // Length of the data block.
197  );
198 
199 
200 
201  private:
202  void OutputBase64(const BYTE * data);
203 
204  PString encodedString;
205  PINDEX encodeLength;
206  BYTE saveTriple[3];
207  PINDEX saveCount;
208  PINDEX nextLine;
209  PString endOfLine;
210 
211  PBoolean perfectDecode;
212  PINDEX quadPosition;
213  PBYTEArray decodedData;
214  PINDEX decodeSize;
215 };
216 
217 class PMessageDigest : public PObject
218 {
220 
221  public:
223  PMessageDigest();
224 
225  class Result {
226  public:
227  PINDEX GetSize() const { return value.GetSize(); }
228  const BYTE * GetPointer() const { return (const BYTE *)value; }
229 
230  private:
231  PBYTEArray value;
232  friend class PMessageDigest5;
233  friend class PMessageDigestSHA1;
234  };
235 
237  virtual void Start() = 0;
238 
239  virtual void Process(
240  const void * dataBlock,
241  PINDEX length
242  );
243 
245  virtual void Process(
246  const PString & str
247  );
249  virtual void Process(
250  const char * cstr
251  );
253  virtual void Process(
254  const PBYTEArray & data
255  );
256 
264  virtual PString CompleteDigest();
265  virtual void CompleteDigest(
266  Result & result
267  );
268 
269  protected:
270  virtual void InternalProcess(
271  const void * dataBlock,
272  PINDEX length
273  ) = 0;
274 
275  virtual void InternalCompleteDigest(
276  Result & result
277  ) = 0;
278 };
279 
280 
287 {
289 
290  public:
292  PMessageDigest5();
293 
295  void Start();
296 
298  static PString Encode(
299  const PString & str
300  );
302  static void Encode(
303  const PString & str,
304  Result & result
305  );
307  static PString Encode(
308  const char * cstr
309  );
311  static void Encode(
312  const char * cstr,
313  Result & result
314  );
316  static PString Encode(
317  const PBYTEArray & data
318  );
320  static void Encode(
321  const PBYTEArray & data,
322  Result & result
323  );
325  static PString Encode(
326  const void * dataBlock,
327  PINDEX length
328  );
334  static void Encode(
335  const void * dataBlock,
336  PINDEX length,
337  Result & result
338  );
339 
340  // backwards compatibility functions
341  class Code {
342  private:
343  PUInt32l value[4];
344  friend class PMessageDigest5;
345  };
346 
348  static void Encode(
349  const PString & str,
350  Code & result
351  );
353  static void Encode(
354  const char * cstr,
355  Code & result
356  );
358  static void Encode(
359  const PBYTEArray & data,
360  Code & result
361  );
367  static void Encode(
368  const void * dataBlock,
369  PINDEX length,
370  Code & result
371  );
372  virtual void Complete(
373  Code & result
374  );
375  virtual PString Complete();
376 
377  protected:
378  virtual void InternalProcess(
379  const void * dataBlock,
380  PINDEX length
381  );
382 
383  virtual void InternalCompleteDigest(
384  Result & result
385  );
386 
387  private:
388  void Transform(const BYTE * block);
389 
391  BYTE buffer[64];
393  DWORD state[4];
395  PUInt64 count;
396 };
397 
398 #if P_SSL
399 
404 class PMessageDigestSHA1 : public PMessageDigest
405 {
406  PCLASSINFO(PMessageDigestSHA1, PMessageDigest)
407 
408  public:
410  PMessageDigestSHA1();
411  ~PMessageDigestSHA1();
412 
414  void Start();
415 
417  static PString Encode(
418  const PString & str
419  );
421  static void Encode(
422  const PString & str,
423  Result & result
424  );
426  static PString Encode(
427  const char * cstr
428  );
430  static void Encode(
431  const char * cstr,
432  Result & result
433  );
435  static PString Encode(
436  const PBYTEArray & data
437  );
439  static void Encode(
440  const PBYTEArray & data,
441  Result & result
442  );
444  static PString Encode(
445  const void * dataBlock,
446  PINDEX length
447  );
453  static void Encode(
454  const void * dataBlock,
455  PINDEX length,
456  Result & result
457  );
458 
459  protected:
460  virtual void InternalProcess(
461  const void * dataBlock,
462  PINDEX length
463  );
464 
465  void InternalCompleteDigest(
466  Result & result
467  );
468 
469  private:
470  void * shaContext;
471 };
472 
473 #endif
474 
478 class PCypher : public PObject
479 {
481 
482  public:
486  ECB = ElectronicCodebook,
488  CBC = CypherBlockChaining,
490  OFB = OutputFeedback,
492  CFB = CypherFeedback,
493  NumBlockChainModes
494  };
495 
496  // New functions for class
498  PString Encode(
499  const PString & str
500  );
502  PString Encode(
503  const PBYTEArray & clear
504  );
506  PString Encode(
507  const void * data,
508  PINDEX length
509  );
511  void Encode(
512  const PBYTEArray & clear,
513  PBYTEArray & coded
514  );
530  void Encode(
531  const void * data, // Clear text binary data to be encoded.
532  PINDEX length, // Number of bytes of data to be encoded.
533  PBYTEArray & coded // Encoded data.
534  );
535 
537  PString Decode(
538  const PString & cypher
539  );
541  PBoolean Decode(
542  const PString & cypher,
543  PString & clear
544  );
546  PBoolean Decode(
547  const PString & cypher,
548  PBYTEArray & clear
549  );
551  PINDEX Decode(
552  const PString & cypher,
553  void * data,
554  PINDEX length
555  );
557  PINDEX Decode(
558  const PBYTEArray & coded,
559  void * data,
560  PINDEX length
561  );
577  PBoolean Decode(
578  const PBYTEArray & coded,
579  PBYTEArray & clear
580  );
581 
582 
583  protected:
587  PCypher(
588  PINDEX blockSize,
589  BlockChainMode chainMode
590  );
591  PCypher(
592  const void * keyData,
593  PINDEX keyLength,
594  PINDEX blockSize,
595  BlockChainMode chainMode
596  );
597 
598 
600  virtual void Initialise(
601  PBoolean encoding
602  ) = 0;
603 
605  virtual void EncodeBlock(
606  const void * in,
607  void * out
608  ) = 0;
609 
610 
612  virtual void DecodeBlock(
613  const void * in,
614  void * out
615  ) = 0;
616 
617 
621  PINDEX blockSize;
624 };
625 
626 
634 class PTEACypher : public PCypher
635 {
637 
638  public:
639  struct Key {
640  BYTE value[16];
641  };
642 
647  PTEACypher(
648  BlockChainMode chainMode = ElectronicCodebook
649  );
650  PTEACypher(
651  const Key & keyData,
652  BlockChainMode chainMode = ElectronicCodebook
653  );
654 
655 
657  void SetKey(
658  const Key & newKey
659  );
660 
662  void GetKey(
663  Key & newKey
664  ) const;
665 
666 
668  static void GenerateKey(
669  Key & newKey
670  );
671 
672 
673  protected:
675  virtual void Initialise(
676  PBoolean encoding
677  );
678 
680  virtual void EncodeBlock(
681  const void * in,
682  void * out
683  );
684 
686  virtual void DecodeBlock(
687  const void * in,
688  void * out
689  );
690 
691  private:
692  DWORD k0, k1, k2, k3;
693 };
694 
695 
696 #ifdef P_CONFIG_FILE
697 
698 class PSecureConfig : public PConfig
699 {
701 /* This class defines a set of configuration keys which may be secured by an
702  encrypted hash function. Thus values contained in keys specified by this
703  class cannot be changed without invalidating the hash function.
704  */
705 
706  public:
708  const PTEACypher::Key & productKey, // Key to decrypt validation code.
709  const PStringArray & securedKeys, // List of secured keys.
710  Source src = Application // Standard source for the configuration.
711  );
713  const PTEACypher::Key & productKey, // Key to decrypt validation code.
714  const char * const * securedKeyArray, // List of secured keys.
715  PINDEX count, // Number of secured keys in list.
716  Source src = Application // Standard source for the configuration.
717  );
718  /* Create a secured configuration. The default section for the
719  configuration keys is "Secured Options", the default security key is
720  "Validation" and the defualt prefix string is "Pending:".
721 
722  The user can descend from this class and change any of the member
723  variable for the names of keys or the configuration file section.
724  */
725 
726 
727  // New functions for class
728  const PStringArray & GetSecuredKeys() const { return securedKeys; }
729  /* Get the list of secured keys in the configuration file section.
730 
731  @return
732  Array of strings for the secured keys.
733  */
734 
735  const PString & GetSecurityKey() const { return securityKey; }
736  /* Get the security keys name in the configuration file section.
737 
738  @return
739  String for the security values key.
740  */
741 
742  const PString & GetExpiryDateKey() const { return expiryDateKey; }
743  /* Get the expiry date keys name in the configuration file section.
744 
745  @return
746  String for the expiry date values key.
747  */
748 
749  const PString & GetOptionBitsKey() const { return optionBitsKey; }
750  /* Get the Option Bits keys name in the configuration file section.
751 
752  @return
753  String for the Option Bits values key.
754  */
755 
756  const PString & GetPendingPrefix() const { return pendingPrefix; }
757  /* Get the pending prefix name in the configuration file section.
758 
759  @return
760  String for the pending prefix.
761  */
762 
763  void GetProductKey(
764  PTEACypher::Key & productKey // Variable to receive the product key.
765  ) const;
766  /* Get the pending prefix name in the configuration file section.
767 
768  @return
769  String for the pending prefix.
770  */
771 
772 
778  Invalid
779  };
780  ValidationState GetValidation() const;
781  /* Check the current values attached to the keys specified in the
782  constructor against an encoded validation key.
783 
784  @return
785  State of the validation keys.
786  */
787 
788  PBoolean ValidatePending();
789  /* Validate a pending secured option list for the product. All secured
790  keys with the <CODE>pendingPrefix</CODE> name will be checked against
791  the value of the field <CODE>securityKey</CODE>. If they match then
792  they are copied to the secured variables.
793 
794  @return
795  true if secure key values are valid.
796  */
797 
798  void ResetPending();
799  /* "Unvalidate" a security configuration going back to a pending state,
800  usually used after an <CODE>Invalid</CODE> response was recieved from
801  the <A>GetValidation()</A> function.
802  */
803 
804 
805  protected:
812 };
813 
814 #endif // P_CONFIG_FILE
815 
816 #endif // PTLIB_CYPHER_H
817 
818 
819 // End Of File ///////////////////////////////////////////////////////////////
virtual void Start()=0
Begin a Message Digest operation, initialising the object instance.
Definition: cypher.h:774
Definition: cypher.h:487
const PString & GetOptionBitsKey() const
Definition: cypher.h:749
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
This class is used to encode/decode data using the MIME standard base64 encoding mechanism as defined...
Definition: cypher.h:70
friend class PMessageDigestSHA1
Definition: cypher.h:233
A class representing a configuration for the application.
Definition: config.h:67
PString optionBitsKey
Definition: cypher.h:810
Definition: cypher.h:225
void StartDecoding()
const PStringArray & GetSecuredKeys() const
Definition: cypher.h:728
Definition: cypher.h:775
static PString Encode(const PString &str, const char *endOfLine="\n")
virtual void InternalProcess(const void *dataBlock, PINDEX length)=0
void StartEncoding(bool useCRLFs=true)
Definition: cypher.h:776
This is an array collection class of PString objects.
Definition: pstring.h:2024
PStringArray securedKeys
Definition: cypher.h:807
PBoolean IsDecodeOK()
Return a flag to indicate that the input was decoded without any extraneous or illegal characters in ...
Definition: cypher.h:172
PBoolean ProcessDecoding(const PString &str)
Incorporate the specified data into the base 64 decoding.
PINDEX blockSize
Size of each encryption block in bytes.
Definition: cypher.h:621
BOOL PBoolean
Definition: object.h:102
BlockChainMode
Mechanism by which sequential blocks are linked.
Definition: cypher.h:484
Array of unsigned characters.
Definition: array.h:670
PBYTEArray GetDecodedData()
const PString & GetPendingPrefix() const
Definition: cypher.h:756
static PString Encode(const PString &str)
Encode the data in memory to and MD5 hash value.
Source
Description of the standard source for configuration information.
Definition: config.h:76
Definition: cypher.h:491
Definition: cypher.h:217
virtual void InternalCompleteDigest(Result &result)
virtual void InternalProcess(const void *dataBlock, PINDEX length)
This abstract class defines an encryption/decryption algortihm.
Definition: cypher.h:478
virtual PINDEX GetSize() const
Get the current size of the container.
Definition: cypher.h:698
virtual PString Complete()
PString securityKey
Definition: cypher.h:808
PString expiryDateKey
Definition: cypher.h:809
PMessageDigest5()
Create a new message digestor.
The character string class.
Definition: pstring.h:108
Definition: cypher.h:639
PMessageDigest()
Create a new message digestor.
PBYTEArray key
Key for the encryption/decryption.
Definition: cypher.h:619
const BYTE * GetPointer() const
Definition: cypher.h:228
MD5 Message Digest.
Definition: cypher.h:286
virtual PString CompleteDigest()
Complete the message digest and return the magic number result.
const PString & GetSecurityKey() const
Definition: cypher.h:735
BlockChainMode chainMode
Mode for sequential encryption each block.
Definition: cypher.h:623
Definition: cypher.h:485
PBase64()
Construct a base 64 encoder/decoder and initialise both encode and decode members as in StartEncoding...
PINDEX GetSize() const
Definition: cypher.h:227
virtual void InternalCompleteDigest(Result &result)=0
ValidationState
Definition: cypher.h:773
void Start()
Begin a Message Digest operation, initialising the object instance.
PString pendingPrefix
Definition: cypher.h:811
PTEACypher::Key productKey
Definition: cypher.h:806
void ProcessEncoding(const PString &str)
Definition: cypher.h:489
static PString Decode(const PString &str)
Convert a printable text string to binary data using the Internet MIME standard base 64 content trans...
PString CompleteEncoding()
Complete the base 64 encoding and return the remainder of the encoded Base64 string.
PString GetEncodedString()
Get the partial Base64 string for the data encoded so far.
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
const PString & GetExpiryDateKey() const
Definition: cypher.h:742
virtual void Process(const void *dataBlock, PINDEX length)
Definition: cypher.h:777
Tiny Encryption Algorithm.
Definition: cypher.h:634
Definition: cypher.h:341