PTLib
Version 2.12.9
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
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: 29414 $
27
* $Author: rjongbloed $
28
* $Date: 2013-04-03 07:39:30 +1100 (Wed, 03 Apr 2013) $
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
108
PString
GetEncodedString
();
109
116
PString
CompleteEncoding
();
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
147
PBoolean
ProcessDecoding
(
148
const
PString
& str
// String to be encoded
149
);
150
PBoolean
ProcessDecoding
(
151
const
char
* cstr
// C String to be encoded
152
);
153
159
PBoolean
GetDecodedData
(
160
void
* dataBlock,
// Pointer to data to be decoded from base64
161
PINDEX length
// Length of the data block.
162
);
163
PBYTEArray
GetDecodedData
();
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
BYTE saveTriple[3];
206
PINDEX saveCount;
207
PINDEX nextLine;
208
PString
endOfLine;
209
210
bool
perfectDecode;
211
PINDEX quadPosition;
212
PBYTEArray
decodedData;
213
PINDEX decodeSize;
214
};
215
216
class
PMessageDigest
:
public
PObject
217
{
218
PCLASSINFO
(
PMessageDigest
,
PObject
)
219
220
public
:
222
PMessageDigest
();
223
224
class
Result
:
public
PBYTEArray
{
225
public
:
226
virtual
void
PrintOn
(ostream & strm)
const
;
227
228
PString
AsBase64
()
const
{
return
PBase64::Encode
(*
this
,
""
); }
229
PString
AsHex
()
const
;
230
};
231
233
virtual
void
Start
() = 0;
234
235
virtual
void
Process
(
236
const
void
* dataBlock,
237
PINDEX length
238
);
239
241
virtual
void
Process
(
242
const
PString
& str
243
);
245
virtual
void
Process
(
246
const
char
* cstr
247
);
249
virtual
void
Process
(
250
const
PBYTEArray
& data
251
);
252
260
virtual
PString
CompleteDigest
();
261
virtual
void
CompleteDigest
(
262
Result & result
263
);
264
265
protected
:
266
virtual
void
InternalProcess
(
267
const
void
* dataBlock,
268
PINDEX length
269
) = 0;
270
271
virtual
void
InternalCompleteDigest
(
272
Result & result
273
) = 0;
274
};
275
276
281
class
PHMAC
:
public
PObject
282
{
283
public
:
284
typedef
PMessageDigest::Result
Result
;
285
virtual
void
Hash
(
const
BYTE * data, PINDEX len,
Result
& result) = 0;
286
287
virtual
PString
Encode
(
const
BYTE * data, PINDEX len);
288
virtual
PString
Encode
(
const
PBYTEArray
& data);
289
virtual
PString
Encode
(
const
PString
& str);
290
291
virtual
void
Process
(
const
BYTE * data, PINDEX len,
Result
& result);
292
virtual
void
Process
(
const
PBYTEArray
& data,
Result
& result);
293
virtual
void
Process
(
const
PString
& str,
Result
& result);
294
295
protected
:
296
virtual
int
GetL
()
const
= 0;
297
virtual
int
GetB
()
const
{
return
64; }
298
virtual
void
Initialise
(
const
BYTE * key, PINDEX len);
299
virtual
void
InternalProcess
(
const
BYTE * data, PINDEX len,
PHMAC::Result
& result);
300
301
PBYTEArray
m_key
;
302
};
303
304
template
<
class
hash_
class
>
305
class
PHMACTemplate
:
public
PHMAC
306
{
307
public
:
308
PHMACTemplate
(
const
PString
& key) {
Initialise
((
const
BYTE *)(
const
char
*)key, key.
GetLength
()); }
309
PHMACTemplate
(
const
PBYTEArray
& key) {
Initialise
((
const
BYTE *)key, key.
GetSize
()); }
310
PHMACTemplate
(
const
BYTE * key, PINDEX len) {
Initialise
(key, len); }
311
312
virtual
void
Hash
(
const
BYTE * data, PINDEX len,
Result
& result)
313
{ hash_class::Encode(data, len, result); }
314
virtual
int
GetL
()
const
{
return
20; };
315
};
316
322
class
PMessageDigest5
:
public
PMessageDigest
323
{
324
PCLASSINFO
(
PMessageDigest5
,
PMessageDigest
)
325
326
public
:
328
PMessageDigest5
();
329
331
void
Start
();
332
334
static
PString
Encode
(
335
const
PString
& str
336
);
338
static
void
Encode
(
339
const
PString
& str,
340
Result
& result
341
);
343
static
PString
Encode
(
344
const
char
* cstr
345
);
347
static
void
Encode
(
348
const
char
* cstr,
349
Result
& result
350
);
352
static
PString
Encode
(
353
const
PBYTEArray
& data
354
);
356
static
void
Encode
(
357
const
PBYTEArray
& data,
358
Result
& result
359
);
361
static
PString
Encode
(
362
const
void
* dataBlock,
363
PINDEX length
364
);
370
static
void
Encode
(
371
const
void
* dataBlock,
372
PINDEX length,
373
Result
& result
374
);
375
376
// backwards compatibility functions
377
class
Code
{
378
private
:
379
PUInt32l value[4];
380
friend
class
PMessageDigest5
;
381
};
382
384
static
void
Encode
(
385
const
PString
& str,
386
Code
& result
387
);
389
static
void
Encode
(
390
const
char
* cstr,
391
Code
& result
392
);
394
static
void
Encode
(
395
const
PBYTEArray
& data,
396
Code
& result
397
);
403
static
void
Encode
(
404
const
void
* dataBlock,
405
PINDEX length,
406
Code
& result
407
);
408
virtual
void
Complete
(
409
Code
& result
410
);
411
virtual
PString
Complete
();
412
413
protected
:
414
virtual
void
InternalProcess
(
415
const
void
* dataBlock,
416
PINDEX length
417
);
418
419
virtual
void
InternalCompleteDigest
(
420
Result
& result
421
);
422
423
private
:
424
void
Transform(
const
BYTE * block);
425
427
BYTE buffer[64];
429
DWORD state[4];
431
PUInt64 count;
432
};
433
434
typedef
PHMACTemplate<PMessageDigest5>
PHMAC_MD5
;
435
436
#if P_SSL
437
442
class
PMessageDigestSHA1 :
public
PMessageDigest
443
{
444
PCLASSINFO
(PMessageDigestSHA1,
PMessageDigest
)
445
446
public
:
448
PMessageDigestSHA1();
449
~PMessageDigestSHA1();
450
452
void
Start
();
453
455
static
PString
Encode(
456
const
PString
& str
457
);
459
static
void
Encode(
460
const
PString
& str,
461
Result & result
462
);
464
static
PString
Encode(
465
const
char
* cstr
466
);
468
static
void
Encode(
469
const
char
* cstr,
470
Result & result
471
);
473
static
PString
Encode(
474
const
PBYTEArray
& data
475
);
477
static
void
Encode(
478
const
PBYTEArray
& data,
479
Result & result
480
);
482
static
PString
Encode(
483
const
void
* dataBlock,
484
PINDEX length
485
);
491
static
void
Encode(
492
const
void
* dataBlock,
493
PINDEX length,
494
Result & result
495
);
496
497
protected
:
498
virtual
void
InternalProcess
(
499
const
void
* dataBlock,
500
PINDEX length
501
);
502
503
void
InternalCompleteDigest
(
504
Result & result
505
);
506
507
private
:
508
void
* shaContext;
509
};
510
511
typedef
PHMACTemplate<PMessageDigestSHA1>
PHMAC_SHA1;
512
513
#endif
514
518
class
PCypher
:
public
PObject
519
{
520
PCLASSINFO
(
PCypher
,
PObject
)
521
522
public
:
524
enum
BlockChainMode
{
525
ElectronicCodebook
,
526
ECB
=
ElectronicCodebook
,
527
CypherBlockChaining
,
528
CBC
=
CypherBlockChaining
,
529
OutputFeedback
,
530
OFB
=
OutputFeedback
,
531
CypherFeedback
,
532
CFB
=
CypherFeedback
,
533
NumBlockChainModes
534
};
535
536
// New functions for class
538
PString
Encode
(
539
const
PString
& str
540
);
542
PString
Encode
(
543
const
PBYTEArray
& clear
544
);
546
PString
Encode
(
547
const
void
* data,
548
PINDEX length
549
);
551
void
Encode
(
552
const
PBYTEArray
& clear,
553
PBYTEArray
& coded
554
);
570
void
Encode
(
571
const
void
* data,
// Clear text binary data to be encoded.
572
PINDEX length,
// Number of bytes of data to be encoded.
573
PBYTEArray
& coded
// Encoded data.
574
);
575
577
PString
Decode
(
578
const
PString
& cypher
579
);
581
PBoolean
Decode
(
582
const
PString
& cypher,
583
PString
& clear
584
);
586
PBoolean
Decode
(
587
const
PString
& cypher,
588
PBYTEArray
& clear
589
);
591
PINDEX
Decode
(
592
const
PString
& cypher,
593
void
* data,
594
PINDEX length
595
);
597
PINDEX
Decode
(
598
const
PBYTEArray
& coded,
599
void
* data,
600
PINDEX length
601
);
617
PBoolean
Decode
(
618
const
PBYTEArray
& coded,
619
PBYTEArray
& clear
620
);
621
622
623
protected
:
627
PCypher
(
628
PINDEX
blockSize
,
629
BlockChainMode
chainMode
630
);
631
PCypher
(
632
const
void
* keyData,
633
PINDEX keyLength,
634
PINDEX
blockSize
,
635
BlockChainMode
chainMode
636
);
637
638
640
virtual
void
Initialise
(
641
PBoolean
encoding
642
) = 0;
643
645
virtual
void
EncodeBlock
(
646
const
void
* in,
647
void
* out
648
) = 0;
649
650
652
virtual
void
DecodeBlock
(
653
const
void
* in,
654
void
* out
655
) = 0;
656
657
659
PBYTEArray
key
;
661
PINDEX
blockSize
;
663
BlockChainMode
chainMode
;
664
};
665
666
674
class
PTEACypher
:
public
PCypher
675
{
676
PCLASSINFO
(
PTEACypher
,
PCypher
)
677
678
public
:
679
struct
Key
{
680
BYTE
value
[16];
681
};
682
687
PTEACypher
(
688
BlockChainMode
chainMode
=
ElectronicCodebook
689
);
690
PTEACypher
(
691
const
Key
& keyData,
692
BlockChainMode
chainMode
=
ElectronicCodebook
693
);
694
695
697
void
SetKey
(
698
const
Key
& newKey
699
);
700
702
void
GetKey
(
703
Key
& newKey
704
)
const
;
705
706
708
static
void
GenerateKey
(
709
Key
& newKey
710
);
711
712
713
protected
:
715
virtual
void
Initialise
(
716
PBoolean
encoding
717
);
718
720
virtual
void
EncodeBlock
(
721
const
void
* in,
722
void
* out
723
);
724
726
virtual
void
DecodeBlock
(
727
const
void
* in,
728
void
* out
729
);
730
731
private
:
732
DWORD k0, k1, k2, k3;
733
};
734
735
736
#ifdef P_CONFIG_FILE
737
738
class
PSecureConfig
:
public
PConfig
739
{
740
PCLASSINFO
(
PSecureConfig
,
PConfig
)
741
/* This class defines a set of configuration keys which may be secured by an
742
encrypted hash function. Thus values contained in keys specified by this
743
class cannot be changed without invalidating the hash function.
744
*/
745
746
public
:
747
PSecureConfig
(
748
const
PTEACypher::Key
&
productKey
,
// Key to decrypt validation code.
749
const
PStringArray
&
securedKeys
,
// List of secured keys.
750
Source
src =
Application
// Standard source for the configuration.
751
);
752
PSecureConfig
(
753
const
PTEACypher::Key
& productKey,
// Key to decrypt validation code.
754
const
char
*
const
* securedKeyArray,
// List of secured keys.
755
PINDEX count,
// Number of secured keys in list.
756
Source
src =
Application
// Standard source for the configuration.
757
);
758
/* Create a secured configuration. The default section for the
759
configuration keys is "Secured Options", the default security key is
760
"Validation" and the defualt prefix string is "Pending:".
761
762
The user can descend from this class and change any of the member
763
variable for the names of keys or the configuration file section.
764
*/
765
766
767
// New functions for class
768
const
PStringArray
&
GetSecuredKeys
()
const
{
return
securedKeys
; }
769
/* Get the list of secured keys in the configuration file section.
770
771
@return
772
Array of strings for the secured keys.
773
*/
774
775
const
PString
&
GetSecurityKey
()
const
{
return
securityKey
; }
776
/* Get the security keys name in the configuration file section.
777
778
@return
779
String for the security values key.
780
*/
781
782
const
PString
&
GetExpiryDateKey
()
const
{
return
expiryDateKey
; }
783
/* Get the expiry date keys name in the configuration file section.
784
785
@return
786
String for the expiry date values key.
787
*/
788
789
const
PString
&
GetOptionBitsKey
()
const
{
return
optionBitsKey
; }
790
/* Get the Option Bits keys name in the configuration file section.
791
792
@return
793
String for the Option Bits values key.
794
*/
795
796
const
PString
&
GetPendingPrefix
()
const
{
return
pendingPrefix
; }
797
/* Get the pending prefix name in the configuration file section.
798
799
@return
800
String for the pending prefix.
801
*/
802
803
void
GetProductKey
(
804
PTEACypher::Key
& productKey
// Variable to receive the product key.
805
)
const
;
806
/* Get the pending prefix name in the configuration file section.
807
808
@return
809
String for the pending prefix.
810
*/
811
812
813
enum
ValidationState
{
814
Defaults
,
815
Pending
,
816
IsValid
,
817
Expired
,
818
Invalid
819
};
820
ValidationState
GetValidation
()
const
;
821
/* Check the current values attached to the keys specified in the
822
constructor against an encoded validation key.
823
824
@return
825
State of the validation keys.
826
*/
827
828
PBoolean
ValidatePending
();
829
/* Validate a pending secured option list for the product. All secured
830
keys with the <CODE>pendingPrefix</CODE> name will be checked against
831
the value of the field <CODE>securityKey</CODE>. If they match then
832
they are copied to the secured variables.
833
834
@return
835
true if secure key values are valid.
836
*/
837
838
void
ResetPending
();
839
/* "Unvalidate" a security configuration going back to a pending state,
840
usually used after an <CODE>Invalid</CODE> response was recieved from
841
the <A>GetValidation()</A> function.
842
*/
843
844
845
protected
:
846
PTEACypher::Key
productKey
;
847
PStringArray
securedKeys
;
848
PString
securityKey
;
849
PString
expiryDateKey
;
850
PString
optionBitsKey
;
851
PString
pendingPrefix
;
852
};
853
854
#endif // P_CONFIG_FILE
855
856
#endif // PTLIB_CYPHER_H
857
858
859
// End Of File ///////////////////////////////////////////////////////////////
include
ptclib
cypher.h
Generated on Mon Feb 17 2014 13:12:51 for PTLib by
1.8.3.1