Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sys_exit equ 1
- sys_write equ 4
- stdout equ 1
- stdin equ 0
- %define pos_R r8b
- %define pos_L r12b
- %macro turn 0
- xor ecx,ecx ;eax = 0
- add pos_R,BYTE 1 ;Turn R
- cmp pos_R,BYTE 42 ;Check if 'overflow permitted chars'
- cmovae r8d,ecx ;If so, set to 0 again (42 -> 0 is equivalent of 'Z' -> '1')
- xor r15d,r15d ;r15d = 0
- mov r14d,1
- ;check if R is in any of rotation positions ('L','T','R'):
- ;if so rotate L too (change rotations from 0 to 1 -> r15)
- cmp pos_R,33 ;33 = 'R' - '1' (So it checks R pos)
- cmove r15d,r14d ;L rotation = 1
- cmp pos_R,27 ;33 = 'L' - '1' (So it checks R pos)
- cmove r15d,r14d ;L rotation = 1
- cmp pos_R,35 ;35 = 'T' - '1' (So it checks R pos)
- cmove r15d,r14d ;L rotation = 1
- xor ecx,ecx ;rotate L modulo 42 (similarly to R rotation)
- add pos_L,r15b
- cmp pos_L,BYTE 42
- cmovae r12d,ecx
- %endmacro
- %macro print 2 ;prints arg1 of len equal to arg2
- mov rcx,%1
- mov rdx,%2
- mov rax,sys_write
- mov rbx,stdout
- int 80h
- %endmacro
- %macro rot_Q_1 2 ;Q-1 permutation of arg1 (parameter eual to arg2)
- mov r11b,%1
- sub r11b,%2
- sub %1,%2
- xor ecx,ecx ;ecx = 0
- mov eax,42
- cmp r11,48 ;arg1 += arg2 modulo 42 (only permited chars)
- cmovbe ecx,eax
- mov eax,ecx
- add %1,al
- %endmacro
- %macro rot_Q 2 ;Q permutation of arg1 (parameter eual to arg2)
- ;similarly to rot_Q_1
- mov r11b,%1
- add r11b,%2
- add %1,%2
- xor ecx,ecx
- mov eax,42
- cmp r11,91
- cmovae ecx,eax
- mov eax,ecx
- sub %1,al
- %endmacro
- %macro rot_1_R 1 ;R-1 permutation of arg1
- xor r11b,r11b ;r11b = 0
- %%loop:
- mov al,r11b ;al -> counter 1,2,3...90
- cmp al,91
- jge error
- movzx rcx,al
- mov al,[R + rcx] ;get R(counter)
- add r11,1 ;counter++
- cmp al,%1 ;if R(counter) == arg1
- jne %%loop
- mov %1,r11b ;'return' char corresponding to counter
- add %1,48
- %endmacro
- %macro rot_1_L 1 ;L-1 permutation of arg1
- ;similarly to R-1 rotation
- xor r11b, r11b
- %%loop:
- mov al,r11b
- cmp al,91
- jge error
- movzx rcx, al
- mov al,[L + rcx]
- add r11,1
- cmp al,%1
- jne %%loop
- mov %1,r11b
- add %1,48
- %endmacro
- %macro rot_R 1 ;R permutation of arg1
- sub %1,49 ;char to index ('1' -> 0, '2' -> 1...)
- movzx rcx,%1
- mov %1,[R + rcx] ;new char = R[old char]
- %endmacro
- %macro rot_L 1 ;R permutation of arg1, similarly to R
- sub %1,49
- movzx rcx,%1
- mov %1,[L + rcx]
- %endmacro
- %macro rot_T 1 ;R permutation of arg1, similarly to R
- sub %1,49
- movzx rcx,%1
- mov %1,[T + rcx]
- %endmacro
- %macro rot_R_ 1 ;R-1 permutation of arg1 (using prepered R-1 string)
- ;similarly to R
- sub %1,49
- movzx rcx,%1
- mov %1,[R_ + rcx]
- %endmacro
- %macro rot_L_ 1 ;L-1 permutation of arg1 (using prepered L-1 string)
- ;similarly to R
- sub %1,49
- movzx rcx,%1
- mov %1,[L_ + rcx]
- %endmacro
- %macro calc_1 2 ;calculate string for reverse permutation of arg1 and save it to arg2
- mov r8b,48
- %%loop_: ;loop over chars from '1' to 'Z'
- add r8b,1
- cmp r8b,91
- je %%end_loop
- mov r12b,r8b ;calc reverse permutation
- %1 r12b
- movzx rax,r8b
- sub rax,49
- ;save it
- mov [%2 + rax],BYTE r12b
- jmp %%loop_
- %%end_loop:
- %endmacro
- %macro get_arg 3 ;Read cli arg of len = arg3 from stack (rsp + arg1) and save it to arg2
- mov rsi,[rsp + %1] ;get arg addres
- xor rax,rax
- %%arg_loop: ;loop over chars in argument
- mov r14b,[rsi+rax] ;get next char from argument
- cmp r14b,91 ;check if it's still correct char ['1','Z']
- jge %%end_arg_loop
- cmp r14b,48
- jle %%end_arg_loop
- mov [%2+rax],r14b ;save readed char in
- add rax,1 ;counter++
- cmp rax,%3 ;check if counter <> len of arg
- jg %%end_arg_loop
- jmp %%arg_loop
- %%end_arg_loop:
- cmp r14b,0 ;check if last char was equal to 0
- jnz error
- %endmacro
- section .bss
- input resb 4096
- output resb 4096
- L resb 48
- R resb 48
- T resb 48
- KEY resb 8
- R_ resb 48
- L_ resb 48
- section .text
- align 8
- global _start
- _start:
- pop rcx ;get num of cli args
- cmp rcx,5 ;check if it's correct number of args
- jne error
- push rcx
- get_arg 16,L,42 ;get L from cli args
- cmp rax,42
- jne error
- get_arg 24,R,42 ;get R from cli args
- cmp rax,42
- jne error
- get_arg 32,T,42 ;get T from cli args
- cmp rax,42
- jne error
- get_arg 40,KEY,2 ;get KEY from cli args
- cmp rax,2
- jne error
- calc_1 rot_1_R,R_ ;calc rev permutation string for R
- calc_1 rot_1_L,L_ ;calc rev permutation string for L
- mov pos_L,[KEY] ;set start L position
- sub pos_L,49
- mov pos_R,[KEY+1] ;set start R position
- sub pos_R,49
- read_loop: ;loop over input
- mov rax, 3 ;get block of 4096 bytes of input
- mov rbx, stdin
- mov rcx, input
- mov rdx, 4096
- int 80h
- mov r13,rax ;get number of readed bytes
- cmp rax,1 ;check if no more chars on input
- jle end_read_loop ;if so -> end loop
- xor r10,r10 ;counter during data block loop
- loop_block: ;loop over data block
- lea r15,[input + r10] ;load addres of char to encrypt
- mov r9b,[r15] ;get this char
- cmp r9b,10 ;check ascii code of char is not equal to 10
- je end_read_loop
- cmp r9b,48 ;check if current char is permited ['1','Z']
- jle error ;exit with error if not
- cmp r9b,91
- jge error
- turn
- ;ENCRYPTION:
- rot_Q r9b,pos_R ;Qr
- rot_R r9b ;R
- rot_Q_1 r9b,pos_R ;Qr-1
- rot_Q r9b,pos_L ;Ql
- rot_L r9b ;L
- rot_Q_1 r9b,pos_L ;Ql-1
- rot_T r9b ;T
- rot_Q r9b,pos_L ;Ql
- rot_L_ r9b ;L-1
- rot_Q_1 r9b,pos_L ;Ql-1
- rot_Q r9b,pos_R ;Qr
- rot_R_ r9b ;R-1
- rot_Q_1 r9b,pos_R ;Qr-1
- mov [output + r10],r9b ;save encrypted char
- add r10,1 ;next char
- cmp r10,r13 ;check if char counter equal to number of chars (last char in block)
- jl loop_block ;if its not last char, encrypt next one
- cmp r13,4096 ;if last -> check if block was 'full'
- jl end_read_loop ;if not -> there are no more bytes on input
- end_loop_block:
- print output,r13 ;print encrypted block
- jmp read_loop
- end_read_loop:
- print output,r13 ;print (last) encrypted block
- exit:
- mov rax,60
- xor rdi,rdi
- syscall
- error:
- print output,r13 ;exit with error
- mov rax, 60
- mov rdi, 1
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement