-
Notifications
You must be signed in to change notification settings - Fork 4
/
enigma.asm
854 lines (739 loc) · 15 KB
/
enigma.asm
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
title enigma
include Irvine32.inc
; Created by Sam Marshall
; Created on November 01, 2011, 12:32 PM
;Copyright (c) 2010-2011, Sam Marshall
;All rights reserved.
;Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
;1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
;2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
;3. All advertising materials mentioning features or use of this software must display the following acknowledgement:
; This product includes software developed by the Sam Marshall.
;4. Neither the name of the Sam Marshall nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
;
;THIS SOFTWARE IS PROVIDED BY Sam Marshall ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
; IN NO EVENT SHALL Sam Marshall BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.data
menu_title byte "Select an option: ",0
menu_select byte "Select a menu item: ",0
setup_rotor_title byte "Setup Rotors:",0
rotor_str1 byte "Current Rotors:",0
rtr_a byte "1. A: ",0
rtr_b byte "2. B: ",0
rtr_c byte "3. C: ",0
rtr_r byte "4. R: ",0
rtr_1_menu byte " 1. Edit a Rotor"
byte 0dh,0ah," 2. Use these Rotors",0
rtrprompt byte "Enter the Rotor you want to edit (1-4): ",0
rtr_1_title byte "Rotor Properties:",0
rtr_1_prop1 byte " Rotor: ",0
rtr_1_prop2 byte " Reverse: ",0
rtr_1_prop3 byte " Position: ",0
rtr_1_prop4 byte " Step: ",0
rtr_1_menu_item1 byte " 1. Select a Rotor",0
rtr_1_menu_item2 byte " 2. Create a Rotor",0
rtr_1_menu_item3 byte " 3. Set a position",0
rtr_1_menu_item4 byte " 4. Set step increment",0
rtr_1_menu_item5 byte " 5. Back",0
rtr_1_menu_item6 byte " 3. Back",0
rtr_2_menu byte "Select a Rotor: ",0dh,0ah," 1. Service - I",0dh,0ah," 2. Service - II",0dh,0ah," 3. Service - III",0dh,0ah," 4. Service - IV",0dh,0ah," 5. Service - V",0dh,0ah," 6. Service - VI",0dh,0ah," 7. Service - VII",0dh,0ah," 8. Service - VIII",0dh,0ah," 9. Service - UKW A",0dh,0ah," 10. Service - UKW B",0dh,0ah," 11. Service - UKW C",0
rtr_input_prompt byte "Enter a rotor sequence: ",0
rtr_input byte 26 dup(" ")
rtr_term byte 0
rtr_pos_prompt byte "Enter the starting position for this rotor: ",0
rtr_step_prompt byte "Enter the step increment for this rotor: ",0
setup_plugboard_title byte "Setup Plugboard:",0
pb_str1 byte "Current Plugboard: ",0
pb_menu byte " 1. Add a connection",0dh,0ah," 2. Reset Plugboard",0dh,0ah," 3. Use this Plugboard",0
pb_str2 byte "Enter Plugboard Key: ",0
pkey1 byte "0",0
pkey2 byte "0",0
p1index byte 0
p2index byte 0
inputprompt byte "Enter text to encode: ",0
input byte 255 dup(?),0
inputlen byte 0
output byte 255 dup(?),0
s1 byte "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
s2 byte "AJDKSIRUXBLHWTMCQGZNPYFVOE"
s3 byte "BDFHJLCPRTXVZNYEIWGAKMUSQO"
s4 byte "ESOVPZJAYQUIRHXLNFTGKDCMWB"
s5 byte "VZBRGITYUPSDNHLXAWMJQOFECK"
s6 byte "JPGVOUMFYQBENHZRDKASXLICTW"
s7 byte "NZJHGRCXMYSWBOUFAIVLPEKQDT"
s8 byte "FKQHTLXOCBJSPDZRAMEWNIUYGV"
s9 byte "EJMZALYXVBWFCRQUONTSPIKHGD"
s10 byte "YRUHQSLDPXNGOKMIEBFZCWVJAT"
s11 byte "FVPJIAOYEDRZXWGCTKUQSBNMHL"
E byte "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
A_ byte 26 dup(" ")
byte 0
A_R byte 26 dup(" ")
byte 0
astep byte 0
ashift byte 1
B_ byte 26 dup(" ")
byte 0
B_R byte 26 dup(" ")
byte 0
bstep byte 0
bshift byte 2
C_ byte 26 dup(" ")
byte 0
C_R byte 26 dup(" ")
byte 0
cstep byte 0
cshift byte 1
R byte 26 dup(" ")
byte 0
plug byte "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
byte 0
.code
main proc
call ClearRegisters
call SetupRotors
call SetupPlugBoard
call GetInputString
call ClearRegisters
mov cl, inputlen
mov esi, offset input
mov edi, offset output
Encrypt:
;check character
mov eax, [esi]
cmp al, 65
jl isfalse
cmp al, 90
jg isfalse
;we are working with a letter
mov bl, ashift
add astep, bl
cmp astep, 26
jge incb
jmp startencoding
incb:
sub astep, 26
movzx ebx, bshift
add bstep, bl
cmp bstep, 26
jl startencoding
incc:
sub bstep, 26
movzx ebx, cshift
add cstep, bl
cmp cstep, 26
jl startencoding
resetc:
sub cstep, 26
startencoding:
push esi
push ecx
;send to plugboard
mov esi, offset plug
mov edx, 0
mov ecx, lengthof plug
call PassThroughRotor
mov esi, offset A_
movzx edx, astep
mov ecx, lengthof A_
call PassThroughRotor
mov esi, offset B_
movzx edx, bstep
mov ecx, lengthof B_
call PassThroughRotor
mov esi, offset C_
movzx edx, cstep
mov ecx, lengthof C_
call PassThroughRotor
mov esi, offset R
mov edx, 0
mov ecx, lengthof R
call PassThroughRotor
mov esi, offset C_R
movzx edx, cstep
mov ecx, lengthof C_R
call PassThroughRotor
mov esi, offset B_R
movzx edx, bstep
mov ecx, lengthof B_R
call PassThroughRotor
mov esi, offset A_R
movzx edx, astep
mov ecx, lengthof A_R
call PassThroughRotor
mov esi, offset plug
mov edx, 0
mov ecx, lengthof plug
call PassThroughRotor
pop ecx
pop esi
isfalse: ;add non-letter character to output
mov [edi], al
;next character
inc esi
inc edi
dec ecx
cmp ecx, 0
jne Encrypt
call crlf
call crlf
mov edx, offset output
call writeline
exit
main endp
ClearRegisters proc
mov eax, 0
mov ebx, 0
mov ecx, 0
mov edx, 0
mov esi, 0
mov edi, 0
ret
ClearRegisters endp
GetInputString proc
mov edx, offset inputprompt
call writestring
mov edx, offset input
mov ecx, lengthof input
mov eax, 0
call readstring
mov inputlen, al
invoke str_ucase, addr input
ret
GetInputString endp
GetIndexInEntry proc uses ebx ecx esi
mov ecx, lengthof E
mov esi, offset E
ScanEntry:
cmp al, [esi]
je FoundIndex
inc esi
loop ScanEntry
jmp gotoEnd
FoundIndex:
mov ebx, lengthof E
sub ebx, ecx
mov eax, ebx
gotoEnd:
ret
GetIndexInEntry endp
PassThroughRotor proc uses ebx ecx edx edi esi
; eax input letter
push esi
push ecx
call GetIndexInEntry
pop ecx
pop esi
; eax is positon
; esi offset of rotor array
; edx steps for rotor
; ecx lengthof rotor array
mov edi, ecx
add esi, edx
movzx ebx, dl
add ebx, eax
;bl = distance from (offset + steps) (0 - 25)
sub edi, ebx
;ecx = distance from (offset + steps) to position 26
sub ecx, edi
add esi, ecx
mov ecx, [esi]
mov eax, ecx
ret
PassThroughRotor endp
SetupPlugBoard proc uses edx eax esi edi
Setupkey:
call clrscr
mov edx, offset setup_plugboard_title
call writestring
call crlf
mov edx, offset pb_str1
call writestring
mov edx, offset plug
call writestring
call crlf
mov edx, offset menu_title
call writeline
mov edx, offset pb_menu
call writeline
call crlf
mov edx, offset menu_select
call writestring
mov eax, 0
call readint
cmp al, 1
jl invalid
jmp valid1
valid1:
cmp al, 3
jg invalid
jmp valid2
valid2:
cmp al, 2
jl one
je two
jg three
one:
call crlf
mov edx, offset pb_str2
call writestring
mov edx, offset pkey1
mov ecx, lengthof pkey1
call readstring
mov edx, offset pb_str2
call writestring
mov edx, offset pkey2
mov ecx, lengthof pkey2
call readstring
call ClearRegisters
mov esi, offset pkey1
mov al, [esi]
call GetIndexInEntry
mov p1index, al
call ClearRegisters
mov esi, offset pkey2
mov al, [esi]
call GetIndexInEntry
mov p2index, al
mov eax, offset E
mov ebx, offset plug
add al, p1index
add bl, p1index
mov ecx, [eax]
mov edx, [ebx]
cmp ecx, edx
jne usedplug
plug1ok:
call ClearRegisters
mov eax, offset E
mov ebx, offset plug
add al, p2index
add bl, p2index
mov ecx, [eax]
mov edx, [ebx]
cmp ecx, edx
jne usedplug
plug2ok:
call ClearRegisters
mov eax, offset plug
mov ebx, offset plug
mov cl, p1index
mov dl, p2index
add al, cl
add bl, dl
mov ecx, 0
mov edx, 0
mov cl, [eax]
mov dl, [ebx]
xchg ecx, edx
mov [eax], cl
mov [ebx], dl
usedplug:
jmp Setupkey
two:
mov esi, offset E
mov edi, offset plug
mov ecx, lengthof E
resetPlug:
mov eax, [esi]
mov [edi], al
inc esi
inc edi
loop resetPlug
invalid:
jmp Setupkey
three:
call clrscr
ret
SetupPlugBoard endp
SetupRotors proc uses eax edx ecx edx esi edi
Setuprtr:
call clrscr
mov edx, offset setup_rotor_title
call writeline
call crlf
mov edx, offset rtr_a
call writestring
mov edx, offset A_
call writeline
mov edx, offset rtr_b
call writestring
mov edx, offset B_
call writeline
mov edx, offset rtr_c
call writestring
mov edx, offset C_
call writeline
mov edx, offset rtr_r
call writestring
mov edx, offset R
call writeline
call crlf
mov edx, offset menu_title
call writeline
mov edx, offset rtr_1_menu
call writeline
mov edx, offset menu_select
call writestring
mov eax, 0
call readint
cmp eax, 1
je rtredit
cmp eax, 2
je rtruse
jne Setuprtr
rtredit:
call crlf
mov edx, offset rtrprompt
call writestring
mov eax, 0
call readint
cmp eax, 1
je rtrone
cmp eax, 2
je rtrtwo
cmp eax, 3
je rtrthree
cmp eax, 4
je rtrfour
cmp eax, 5
je Setuprtr
jne rtredit
rtrone:
call ClearRegisters
movzx ebx, astep
movzx ecx, ashift
mov esi, offset A_
mov edi, offset A_R
call SetRotorProp
mov astep, bl
mov ashift, cl
jmp Setuprtr
rtrtwo:
call ClearRegisters
movzx ebx, bstep
movzx ecx, bshift
mov esi, offset B_
mov edi, offset B_R
call SetRotorProp
mov bstep, bl
mov bshift, cl
jmp Setuprtr
rtrthree:
call ClearRegisters
movzx ebx, cstep
movzx ecx, cshift
mov esi, offset C_
mov edi, offset C_R
call SetRotorProp
mov cstep, bl
mov cshift, cl
jmp Setuprtr
rtrfour:
call ClearRegisters
mov esi, offset R
call SetReflectorProp
jmp Setuprtr
rtruse:
ret
SetupRotors endp
;ebx = step
;ecx = start
;esi = rotor array
;edi = reverse rotor array
SetRotorProp proc uses edx eax
setproperty:
call clrscr
mov edx, offset rtr_1_title
call writeline
call crlf
mov edx, offset rtr_1_prop1
call writestring
mov edx, esi
call writeline
mov edx, offset rtr_1_prop2
call writestring
mov edx, edi
call writeline
mov edx, offset rtr_1_prop3
call writestring
mov eax, ebx
call writeint
call crlf
mov edx, offset rtr_1_prop4
call writestring
mov eax, ecx
call writeint
call crlf
call crlf
mov edx, offset menu_title
call writeline
mov edx, offset rtr_1_menu_item1
call writeline
mov edx, offset rtr_1_menu_item2
call writeline
mov edx, offset rtr_1_menu_item3
call writeline
mov edx, offset rtr_1_menu_item4
call writeline
mov edx, offset rtr_1_menu_item5
call writeline
call crlf
mov edx, offset menu_select
call writestring
mov eax, 0
call readint
cmp eax, 1
je selectrtr
cmp eax, 2
je creatertr
cmp eax, 3
je setpos
cmp eax, 4
je setstep
cmp eax, 5
je gotoend
jmp setproperty
selectrtr:
call clrscr
push ebx
call ExistingRotorSelect
cmp eax, 0
jle selectrtr
cmp eax, 12
jge selectrtr
SetTheRotor:
push ecx
push edi
mov edi, esi
mov ecx, 26
FillRotor:
mov edx, [ebx]
mov [edi], dl
inc ebx
inc edi
loop FillRotor
pop edi
mov ebx, esi
call GenerateReverse
mov esi, ebx
pop ecx
pop ebx
jmp setproperty
creatertr:
push ebx
push ecx
call crlf
mov edx, offset rtr_input_prompt
call writestring
mov edx, offset rtr_input
mov ecx, 27
mov eax, 0
call readstring
push esi
mov esi, edx
push eax
call VerifyRotor
pop eax
pop esi
cmp ebx, 0
je creatertr
call writeint
cmp eax, 26
jne creatertr
pop ecx
mov ebx, edx
jmp SetTheRotor
setpos:
mov edx, offset rtr_pos_prompt
call writestring
mov eax, 0
call readint
mov ebx, 0
mov ebx, eax
jmp setproperty
setstep:
mov edx, offset rtr_step_prompt
call writestring
mov eax, 0
call readint
mov ecx, 0
mov ecx, eax
jmp setproperty
gotoend:
ret
SetRotorProp endp
GenerateReverse proc uses ebx ecx eax edx
; edi reverse rotor
; esi rotor
; ebx E
; eax value
mov ebx, offset E
mov ecx, 26
FillReverse:
mov eax, [esi]
call GetIndexInEntry
mov edx, [ebx]
push esi
mov esi, edi
add edi, eax
mov [edi], dl
mov edi, esi
pop esi
inc esi
inc ebx
loop FillReverse
ret
GenerateReverse endp
ExistingRotorSelect proc
invalidrotor:
call clrscr
mov edx, offset rtr_2_menu
call writeline
call crlf
mov edx, offset menu_select
call writestring
mov eax, 0
call readint
cmp eax, 0
jle invalidrotor
cmp eax, 12
jge invalidrotor
cmp eax, 1
mov ebx, offset s1
je fill_loop
cmp eax, 2
mov ebx, offset s2
je fill_loop
cmp eax, 3
mov ebx, offset s3
je fill_loop
cmp eax, 4
mov ebx, offset s4
je fill_loop
cmp eax, 5
mov ebx, offset s5
je fill_loop
cmp eax, 6
mov ebx, offset s6
je fill_loop
cmp eax, 7
mov ebx, offset s7
je fill_loop
cmp eax, 8
mov ebx, offset s8
je fill_loop
cmp eax, 9
mov ebx, offset s9
je fill_loop
cmp eax, 10
mov ebx, offset s10
je fill_loop
cmp eax, 11
mov ebx, offset s11
fill_loop:
ret
ExistingRotorSelect endp
SetReflectorProp proc uses edx eax ebx edx ecx edi
setreflect:
call clrscr
mov edx, offset rtr_1_title
call writeline
call crlf
mov edx, offset rtr_1_prop1
call writestring
mov edx, esi
call writeline
call crlf
mov edx, offset menu_title
call writeline
mov edx, offset rtr_1_menu_item1
call writeline
mov edx, offset rtr_1_menu_item2
call writeline
mov edx, offset rtr_1_menu_item6
call writeline
call crlf
mov edx, offset menu_select
call writestring
mov eax, 0
call readint
cmp eax, 1
je selectref
cmp eax, 2
je customref
cmp eax, 3
je GotoEndRef
jmp setreflect
selectref:
call clrscr
call ExistingRotorSelect
cmp eax, 0
jle setreflect
cmp eax, 12
jge setreflect
chooseref:
mov edi, esi
mov ecx, 26
FillReflector:
mov edx, [ebx]
mov [edi], dl
inc ebx
inc edi
loop FillReflector
jmp setreflect
customref:
push ecx
call crlf
mov edx, offset rtr_input_prompt
call writestring
mov edx, offset rtr_input
mov ecx, 27
mov eax, 0
call readstring
push esi
mov esi, edx
push eax
call VerifyRotor
pop eax
pop esi
cmp ebx, 0
je customref
call writeint
cmp eax, 26
jne customref
pop ecx
mov ebx, edx
jmp chooseref
GotoEndRef:
ret
SetReflectorProp endp
writeline proc
call writestring
call crlf
ret
writeline endp
VerifyRotor proc uses ecx edx
push edi
mov edi, esi
mov ebx, 1
mov edx, 64
Nextletter:
inc edx
mov ecx, 26
mov esi, edi
CheckLetters:
mov eax, [esi]
cmp eax, edx
je Nextletter
inc esi
loop CheckLetters
cmp edx, 90
jge GotoEndVerify
mov ebx, 0
GotoEndVerify:
mov esi, edi
pop edi
ret
VerifyRotor endp
end main