-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.dbc
More file actions
667 lines (612 loc) · 23.4 KB
/
Encryption.dbc
File metadata and controls
667 lines (612 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
;;**********************************************************************
;;
;; Title: Encryption.dbc
;;
;; Type: Class
;;
;; Description: Synergy Encryption routines
;;
;; Author: William Hawkins, Synergex Professional Services Group
;;
;; Copyright © 2009 Synergex International Corporation. All rights reserved.
;;
;; WARNING: All content constituting or related to this code ("Code") is the
;; property of Synergex International Corporation ("Synergex") and
;; is protected by U.S. and international copyright laws.
;; If you were given this Code by a Synergex employee then you may
;; use and modify it freely for use within your applications.
;;
;; However, you may use the Code only for your personal use.
;; Any other use, unless otherwise authorized in writing by
;; Synergex is strictly prohibited. You may not under any
;; circumstances distribute this Code, or any modified version
;; or part of this Code, to any third party without first
;; obtaining written permission to do so from Synergex.
;; In using this Code you accept that it is provided as is,
;; and without support or warranty of any kind.
;;
;; Neither Synergex nor the author accept any responsibility
;; for any losses or damages of any nature which may arise
;; from the use of this Code. This header information must
;; remain unaltered in the Code at all times. Possession
;; of this Code, or any modified version or part of this Code,
;; indicates your acceptance of these terms.
;;
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
namespace SynPSG.Core.Utilities.Encryption
.define DC_NONE 0
public enum EncryptionType
None ,DC_NONE
DES3
AES128
AES192
AES256
endenum
public class SynEncrypt
private record
mEncryptionType ,SynPSG.Core.Utilities.Encryption.EncryptionType
mCode ,string ;encryption code (aka password/key)
mSaltLen ,i4 ;0 or 8
mSalt ,a8 ;salt
mIVLen ,i4 ;0 or 8 (3DES) or 16 (AES)
mIV ,a16 ;initialization vector
mPadBuffer ,i4 ;additional pad buffer (0 or 1)
mBlockSize ,i4 ;0 or 8 (3DES) or 16 (AES)
mDbgLogFile ,string ;Debugging log file
endrecord
public property Salt ,a
method get
proc
if(mSaltLen < 1)
mreturn ""
mreturn mSalt(1:mSaltLen)
endmethod
method set
proc
if(mSaltLen < 1 || mIVLen < 1)
getLengths(mSaltLen, mIVLen)
clear mSalt
if(mSaltLen > 0)
mSalt(1:mSaltLen) = value
endmethod
endproperty
public property IV ,a
method get
proc
if(mIVLen < 1)
mreturn ""
mreturn mIV(1:mIVLen)
endmethod
method set
proc
if(mSaltLen < 1 || mIVLen < 1)
getLengths(mSaltLen, mIVLen)
clear mIV
if(mIVLen > 0)
mIV(1:mIVLen) = value
endmethod
endproperty
public property PadBuffer ,i
method get
proc
mreturn mPadBuffer
endmethod
method set
proc
if(value == 0) then
mPadBuffer = 0
else
mPadBuffer = 1
endmethod
endproperty
public property DbgLogFile ,string
method get
proc
mreturn mDbgLogFile
endmethod
method set
proc
mDbgLogFile = value
endmethod
endproperty
;;;<summary>
;;;Get Encryption Code
;;;</summary>
;;;<remarks>
;;;This is the only public mechanism to access the Encyption code.
;;;Due to the sensitive nature of the EncryptionCode, you probably
;;;need to store it seperately/centrally, and encapsulate it inside
;;;this class, instead of the application providing it.
;;;There is no way to reconstruct the EncryptionCode if you no
;;;longer know the EncryptionCode value.
;;;</remark>
public property EncryptionCode ,string
method get
proc
mreturn mCode
endmethod
method set
proc
mCode = value
endmethod
endproperty
;;;<summary>
;;;Get Encryption Code (called from constructor)
;;;</summary>
private method getEncryptionCode, string
endparams
record lm_site
,a6
lm_licensee ,a50
lm_regstr ,a12
lm_regdat ,d8
lm_timout ,d8
endrecord
record
status ,i4
endrecord
proc
;;use the Synergy License Manager licensee name as the encryption code
clear lm_site, status
.ifndef OS_VMS
lm_info(status, lm_site)
.else
lm_licensee = 'this is a dummy string for OpenVMS'
.endc ;OS_VMS
mreturn lm_licensee(1:%trim(lm_licensee))
endmethod
;;;<summary>
;;;constructor (no encryption)
;;;</summary>
public method SynEncrypt
endparams
this(EncryptionType.None)
proc
mreturn
endmethod
;;;<summary>
;;;"default" constructor
;;;</summary>
;;; <param name="aEncryptionType">Encryption type</param>
public method SynEncrypt
req in aEncryptionType ,SynPSG.Core.Utilities.Encryption.EncryptionType
endparams
record
logical ,a256
loglen ,i4
endrecord
proc
using aEncryptionType select
(EncryptionType.DES3),
mBlockSize = 8
(EncryptionType.AES128),
mBlockSize = 16
(EncryptionType.AES192),
mBlockSize = 16
(EncryptionType.AES256),
mBlockSize = 16
(EncryptionType.None),
mBlockSize = 0
(),
throw new Exception("Invalid Encryption Type")
endusing
mEncryptionType = aEncryptionType
getLengths(mSaltLen, mIVLen)
clear mSalt, mIV
mPadBuffer = 1
mCode = getEncryptionCode()
getlog('ENCRYPTION_DEBUG_FILENAME', logical, loglen)
if(loglen > 0)
mDbgLogFile = logical(1:loglen)
mreturn
endmethod
;;;<summary>
;;;constructor
;;;</summary>
;;; <param name="aEncryptionType">Encryption type</param>
;;; <param name="aSalt">Salt</param>
;;; <param name="aIV">IV<param>
public method SynEncrypt
req in aEncryptionType ,SynPSG.Core.Utilities.Encryption.EncryptionType
req in aSalt ,a
req in aIV ,a
endparams
this(aEncryptionType, aSalt, aIV, TRUE)
proc
mreturn
endmethod
;;;<summary>
;;;primary constructor (for encryption)
;;;</summary>
;;; <param name="aEncryptionType">Encryption type</param>
;;; <param name="aSalt">Salt</param>
;;; <param name="aIV">IV<param>
;;; <param name="aPadBuffer">Pad buffer if data is same length as encryption key<param>
public method SynEncrypt
req in aEncryptionType ,SynPSG.Core.Utilities.Encryption.EncryptionType
req in aSalt ,a
req in aIV ,a
req in aPadBuffer ,i
endparams
this(aEncryptionType)
proc
using %trimz(aSalt) select
(1 thru mSaltLen),
begin
if(mSaltLen > ^size(mSalt))
throw new Exception("Salt too small")
clear mSalt
mSalt(1:mSaltLen) = aSalt
end
(),
throw new Exception("Invalid Salt")
endusing
using %trimz(aIV) select
(1 thru mIVLen),
begin
if(mIVLen > ^size(mIV))
throw new Exception("Initialization Vector too small")
clear mIV
mIV(1:mIVLen) = aIV
end
(),
throw new Exception("Invalid Initialization Vector")
endusing
mPadBuffer = aPadBuffer
mreturn
endmethod
;;;<summary>
;;;Encrypt data using provide encryption code
;;;</summary>
;;; <param name="aData">Data to be encrypted</param>
public method Encrypt ,a
req in aData ,a
endparams
record
eLen ,i4
endrecord
proc
;; Call Encrypt method that takes Synergy alpha parameters
mreturn Encrypt(aData, eLen)
endmethod
;;;<summary>
;;;Encrypt data using provide encryption code (primary routine)
;;;</summary>
;;; <param name="aData">Data to be encrypted</param>
public method Encrypt ,a
req in aData ,a
req out aLen ,i
endparams
record
dHdl ,i4
dLen ,i4
eType ,i4
endrecord
proc
if(usingEncryption() == FALSE)
begin
aLen = ^size(aData)
mreturn aData
end
eType = getEncryptionType()
dHdl = %mem_proc(DM_ALLOC+DM_BLANK, getEncryptedLength(aData))
dLen = 0
dbg_data_encrypt(eType, mCode, aData, ^m(dHdl), dLen, mSalt(1:mSaltLen), mIV(1:mIVLen), mPadBuffer)
if(mSaltLen > 0 && mIVLen > 0) then
data_encrypt(eType, mCode, aData, ^m(dHdl), dLen, mSalt(1:mSaltLen), mIV(1:mIVLen), mPadBuffer)
else
data_encrypt(eType, mCode, aData, ^m(dHdl), dLen,,, mPadBuffer)
aLen = dLen
mreturn ^m(dHdl)
endmethod
;;;<summary>
;;;Encrypt data using provide encryption code
;;;</summary>
;;; <param name="aData">Data to be encrypted</param>
public method Encrypt ,string
req in aData ,string
endparams
record
dHdl ,i4
eLen ,i4
EncryptedString ,string
endrecord
proc
if(usingEncryption() == FALSE)
mreturn aData
dHdl = %mem_proc(DM_ALLOC, getEncryptedLength(aData))
^m(dHdl) = aData
;; Call Encrypt method that takes Synergy alpha parameters
EncryptedString = Encrypt(^m(dHdl), eLen)
mreturn EncryptedString
endmethod
;;;<summary>
;;;Encrypt data using provide encryption code
;;;</summary>
;;; <param name="aData">Data to be decrypted</param>
public method Decrypt ,a
req in aData ,a
endparams
record
eLen ,i4
endrecord
proc
;; Call Decrypt method that takes Synergy alpha parameters
mreturn Decrypt(aData, eLen)
endmethod
;;;<summary>
;;;Decrypt data using provide encryption code (primary routine)
;;;</summary>
;;; <param name="aData">Data to be decrypted</param>
;;; <param name="aLen">Length of decrypted data</param>
public method Decrypt ,a
req in aData ,a
req out aLen ,i
endparams
record
dHdl ,i4
dLen ,i4
eType ,i4
endrecord
structure Decrypted
data ,a1
endstructure
proc
if(usingEncryption() == FALSE)
begin
aLen = ^size(aData)
mreturn aData
end
eType = getEncryptionType()
;; get the size of the decrypted data
dlen = 0
if(mSaltLen > 0 && mIVLen > 0) then
data_decrypt(eType, mCode, aData,, dLen, mSalt(1:mSaltLen), mIV(1:mIVLen), mPadBuffer)
else
data_decrypt(eType, mCode, aData,, dLen,,, mPadBuffer)
;; decrypt data
dHdl = %mem_proc(DM_ALLOC+DM_BLANK, dLen)
dLen = 0
if(mSaltLen > 0 && mIVLen > 0) then
begin
dbg_data_decrypt(eType, mCode, aData, ^m(dHdl), dLen, mSalt(1:mSaltLen), mIV(1:mIVLen), mPadBuffer)
data_decrypt(eType, mCode, aData, ^m(dHdl), dLen, mSalt(1:mSaltLen), mIV(1:mIVLen), mPadBuffer)
end
else
begin
dbg_data_decrypt(eType, mCode, aData, ^m(dHdl), dLen,,, mPadBuffer)
data_decrypt(eType, mCode, aData, ^m(dHdl), dLen,,, mPadBuffer)
end
aLen = dLen
mreturn ^m(Decrypted.data(1:dLen), dHdl)
endmethod
;;;<summary>
;;;Encrypt data using provide encryption code
;;;</summary>
;;; <param name="aData">Data to be decrypted</param>
public method Decrypt ,string
req in aData ,string
endparams
record
dHdl ,i4
eLen ,i4
DecryptedString ,string
endrecord
proc
if(usingEncryption() == FALSE)
mreturn aData
dHdl = %mem_proc(DM_ALLOC, ^size(aData))
^m(dHdl) = aData
;; Call Decrypt method that takes Synergy alpha parameters
DecryptedString = Decrypt(^m(dHdl), eLen)
mreturn DecryptedString
endmethod
;;;<summary>
;;;Encryption length, returns zero if not using encryption
;;;</summary>
;;; <param name="aData">Data top be encrypted</param>
public method getEncryptedLength ,int
req in aData ,a
endparams
record
stringData ,string
endrecord
proc
stringData = aData
mreturn getEncryptedLength(stringData)
endmethod
;;;<summary>
;;;Encryption length, returns zero if not using encryption
;;;</summary>
public method getEncryptedLength ,int
req in aData ,string
endparams
record
dataSize ,int
padBuffer ,int
endrecord
proc
dataSize = ^size(aData)
;;check to see if we need any padding
if(mPadBuffer == 0) then
begin
;; if dataSize is an exact multiple of blockSize, no padding is required
if((%trunc(dataSize / mBlockSize) * mBlockSize) == dataSize) then
padBuffer = 0
else
padBuffer = 1
end
else
padBuffer = mPadBuffer
;;round up to next blocksize (if needed) and add Pad buffer (if needed)
mreturn ((%trunc(dataSize / mBlockSize) + padBuffer) * mBlockSize)
endmethod
;;;<summary>
;;;Is Encryption in use
;;;</summary>
private method usingEncryption ,boolean
endparams
proc
if(getEncryptionType() == DC_NONE)
mreturn FALSE
if(mCode == ^NULL)
mreturn FALSE
mreturn TRUE
endmethod
;;;<summary>
;;;Get Salt and IV lengths
;;;</summary>
;;; <param name="aSaltLen">Length of Salt</param>
;;; <param name="aIVLen">Length of IV</param>
private method getLengths ,void
req out aSaltLen ,i
req out aIVLen ,i
endparams
record
eType ,i4
endrecord
proc
eType = getEncryptionType()
if(eType == DC_NONE) then
clear aSaltLen, aIVLen
else
begin
data_saltiv(eType,, aSaltLen,, aIVLen)
dbg_data_saltiv(eType,, aSaltLen,, aIVLen)
end
mreturn
endmethod
;;;<summary>
;;;Get Encryption Type (for Synergy API)
;;;</summary>
private method getEncryptionType ,int
endparams
proc
using mEncryptionType select
(EncryptionType.DES3),
mreturn DC_3DES
(EncryptionType.AES128),
mreturn DC_AES128
(EncryptionType.AES192),
mreturn DC_AES192
(EncryptionType.AES256),
mreturn DC_AES256
endusing
mreturn DC_NONE
endmethod
;;;<summary>
;;;Debug logging for data_encrypt
;;;</summary>
;;; <param name="aEncryptionType">Encryption type</param>
;;; <param name="aCode">Encryption code</param>
;;; <param name="aData">Data to be encrypted</param>
;;; <param name="aEncodedData">Encoded data</param>
;;; <param name="aLen">Length of encoded data</param>
;;; <param name="aSalt">Salt</param>
;;; <param name="aIV">IV</param>
;;; <param name="aPadBuffer">Pad Buffer</param>
private method dbg_data_encrypt ,void
req in aEncryptionType ,i
req in aCode ,a
req in aData ,a
req in aEncodedData ,a
req in aLen ,i
opt in aSalt ,a
opt in aIV ,a
req in aPadBuffer ,i
endparams
record
chan ,i4
endrecord
proc
if(mDbgLogFile == ^NULL || mDbgLogFile == ' ')
mreturn
open(chan=%syn_freechn, A, mDbgLogFile)
if(^passed(aSalt) && ^passed(aIV)) then
begin
writes(chan, 'data_encrypt('+%string(aEncryptionType)+',"'+aCode+'","'+aData+'","'+aEncodedData+'",'+%string(aLen)+',"'+aSalt+'","'+aIV+'",'+%string(aPadBuffer)+')')
writes(chan, 'n,'+%string(^size(aCode))+','+%string(^size(aData))+','+%string(^size(aEncodedData))+',n,'+%string(^size(aSalt))+','+%string(^size(aIV))+',n')
end
else
begin
writes(chan, 'data_encrypt('+%string(aEncryptionType)+',"'+aCode+'","'+aData+'","'+aEncodedData+'",'+%string(aLen)+',,,'+%string(aPadBuffer)+')')
writes(chan, 'n,'+%string(^size(aCode))+','+%string(^size(aData))+','+%string(^size(aEncodedData))+',n,,,,n')
end
close chan
endmethod
;;;<summary>
;;;Debug logging for data_decrypt
;;;</summary>
;;; <param name="aEncryptionType">Encryption type</param>
;;; <param name="aCode">Encryption code</param>
;;; <param name="aData">Data to be encrypted</param>
;;; <param name="aDecodedData">Decoded data</param>
;;; <param name="aLen">Length of decoded data</param>
;;; <param name="aSalt">Salt</param>
;;; <param name="aIV">IV</param>
;;; <param name="aPadBuffer">Pad Buffer</param>
private method dbg_data_decrypt ,void
req in aEncryptionType ,i
req in aCode ,a
req in aData ,a
req in aDecodedData ,a
req in aLen ,i
opt in aSalt ,a
opt in aIV ,a
req in aPadBuffer ,i
endparams
record
chan ,i4
endrecord
proc
if(mDbgLogFile == ^NULL || mDbgLogFile == ' ')
mreturn
open(chan=%syn_freechn, A, mDbgLogFile)
if(^passed(aSalt) && ^passed(aIV)) then
begin
writes(chan, 'data_decrypt('+%string(aEncryptionType)+',"'+aCode+'","'+aData+'","'+aDecodedData+'",'+%string(aLen)+',"'+aSalt+'","'+aIV+'",'+%string(aPadBuffer)+')')
writes(chan, 'n,'+%string(^size(aCode))+','+%string(^size(aData))+','+%string(^size(aDecodedData))+',n,'+%string(^size(aSalt))+','+%string(^size(aIV))+',n')
end
else
begin
writes(chan, 'data_decrypt('+%string(aEncryptionType)+',"'+aCode+'","'+aData+'","'+aDecodedData+'",'+%string(aLen)+',,,'+%string(aPadBuffer)+')')
writes(chan, 'n,'+%string(^size(aCode))+','+%string(^size(aData))+','+%string(^size(aDecodedData))+',n,,,n')
end
close chan
endmethod
;;;<summary>
;;;Debug logging for data_saltiv
;;;</summary>
;;; <param name="aEncryptionType">Encryption type</param>
;;; <param name="aSalt">Salt</param>
;;; <param name="aSaltLEn">Length of Salt</param>
;;; <param name="aIV">IV</param>
;;; <param name="aIVLen">Length of IV</param>
private method dbg_data_saltiv ,void
req in aEncryptionType ,i
opt in aSalt ,a
req out aSaltLen ,i
opt in aIV ,a
req out aIVLen ,i
endparams
record
chan ,i4
endrecord
proc
if(mDbgLogFile == ^NULL || mDbgLogFile == ' ')
mreturn
open(chan=%syn_freechn, A, mDbgLogFile)
if(^passed(aSalt) && ^passed(aIV)) then
begin
writes(chan, 'data_saltiv('+%string(aEncryptionType)+',"'+aSalt+'",'+%string(aSaltLen)+',"'+aIV+'",'+%string(aIVLen)+')')
writes(chan, 'n,'+%string(^size(aSalt))+',n,'+%string(^size(aIV))+',n')
end
else
begin
writes(chan, 'data_saltiv('+%string(aEncryptionType)+',,'+%string(aSaltLen)+',,'+%string(aIVLen)+')')
writes(chan, 'n,,n,,n')
end
close chan
endmethod
endclass
endnamespace