Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. sys_exit equ 1
  2. sys_write equ 4
  3. stdout equ 1
  4. stdin equ 0
  5.  
  6. %define pos_R r8b
  7. %define pos_L r12b
  8.  
  9. %macro turn 0
  10. xor ecx,ecx ;eax = 0
  11. add pos_R,BYTE 1 ;Turn R
  12. cmp pos_R,BYTE 42 ;Check if 'overflow permitted chars'
  13. cmovae r8d,ecx ;If so, set to 0 again (42 -> 0 is equivalent of 'Z' -> '1')
  14.  
  15. xor r15d,r15d ;r15d = 0
  16. mov r14d,1
  17.  
  18. ;check if R is in any of rotation positions ('L','T','R'):
  19. ;if so rotate L too (change rotations from 0 to 1 -> r15)
  20. cmp pos_R,33 ;33 = 'R' - '1' (So it checks R pos)
  21. cmove r15d,r14d ;L rotation = 1
  22.  
  23. cmp pos_R,27 ;33 = 'L' - '1' (So it checks R pos)
  24. cmove r15d,r14d ;L rotation = 1
  25.  
  26. cmp pos_R,35 ;35 = 'T' - '1' (So it checks R pos)
  27. cmove r15d,r14d ;L rotation = 1
  28.  
  29. xor ecx,ecx ;rotate L modulo 42 (similarly to R rotation)
  30. add pos_L,r15b
  31. cmp pos_L,BYTE 42
  32. cmovae r12d,ecx
  33. %endmacro
  34.  
  35. %macro print 2 ;prints arg1 of len equal to arg2
  36. mov rcx,%1
  37. mov rdx,%2
  38. mov rax,sys_write
  39. mov rbx,stdout
  40. int 80h
  41. %endmacro
  42.  
  43. %macro rot_Q_1 2 ;Q-1 permutation of arg1 (parameter eual to arg2)
  44. mov r11b,%1
  45. sub r11b,%2
  46. sub %1,%2
  47.  
  48. xor ecx,ecx ;ecx = 0
  49. mov eax,42
  50. cmp r11,48 ;arg1 += arg2 modulo 42 (only permited chars)
  51. cmovbe ecx,eax
  52. mov eax,ecx
  53. add %1,al
  54. %endmacro
  55.  
  56. %macro rot_Q 2 ;Q permutation of arg1 (parameter eual to arg2)
  57. ;similarly to rot_Q_1
  58. mov r11b,%1
  59. add r11b,%2
  60. add %1,%2
  61.  
  62. xor ecx,ecx
  63. mov eax,42
  64. cmp r11,91
  65. cmovae ecx,eax
  66. mov eax,ecx
  67. sub %1,al
  68. %endmacro
  69.  
  70. %macro rot_1_R 1 ;R-1 permutation of arg1
  71. xor r11b,r11b ;r11b = 0
  72. %%loop:
  73. mov al,r11b ;al -> counter 1,2,3...90
  74. cmp al,91
  75. jge error
  76.  
  77. movzx rcx,al
  78. mov al,[R + rcx] ;get R(counter)
  79. add r11,1 ;counter++
  80. cmp al,%1 ;if R(counter) == arg1
  81. jne %%loop
  82. mov %1,r11b ;'return' char corresponding to counter
  83. add %1,48
  84. %endmacro
  85.  
  86. %macro rot_1_L 1 ;L-1 permutation of arg1
  87. ;similarly to R-1 rotation
  88. xor r11b, r11b
  89. %%loop:
  90. mov al,r11b
  91. cmp al,91
  92. jge error
  93.  
  94. movzx rcx, al
  95. mov al,[L + rcx]
  96. add r11,1
  97. cmp al,%1
  98. jne %%loop
  99. mov %1,r11b
  100. add %1,48
  101. %endmacro
  102.  
  103. %macro rot_R 1 ;R permutation of arg1
  104. sub %1,49 ;char to index ('1' -> 0, '2' -> 1...)
  105. movzx rcx,%1
  106. mov %1,[R + rcx] ;new char = R[old char]
  107. %endmacro
  108.  
  109. %macro rot_L 1 ;R permutation of arg1, similarly to R
  110. sub %1,49
  111. movzx rcx,%1
  112. mov %1,[L + rcx]
  113. %endmacro
  114.  
  115. %macro rot_T 1 ;R permutation of arg1, similarly to R
  116. sub %1,49
  117. movzx rcx,%1
  118. mov %1,[T + rcx]
  119. %endmacro
  120.  
  121. %macro rot_R_ 1 ;R-1 permutation of arg1 (using prepered R-1 string)
  122. ;similarly to R
  123. sub %1,49
  124. movzx rcx,%1
  125. mov %1,[R_ + rcx]
  126. %endmacro
  127.  
  128. %macro rot_L_ 1 ;L-1 permutation of arg1 (using prepered L-1 string)
  129. ;similarly to R
  130. sub %1,49
  131. movzx rcx,%1
  132. mov %1,[L_ + rcx]
  133. %endmacro
  134.  
  135. %macro calc_1 2 ;calculate string for reverse permutation of arg1 and save it to arg2
  136. mov r8b,48
  137. %%loop_: ;loop over chars from '1' to 'Z'
  138. add r8b,1
  139. cmp r8b,91
  140. je %%end_loop
  141.  
  142. mov r12b,r8b ;calc reverse permutation
  143. %1 r12b
  144. movzx rax,r8b
  145. sub rax,49
  146. ;save it
  147. mov [%2 + rax],BYTE r12b
  148. jmp %%loop_
  149. %%end_loop:
  150. %endmacro
  151.  
  152. %macro get_arg 3 ;Read cli arg of len = arg3 from stack (rsp + arg1) and save it to arg2
  153. mov rsi,[rsp + %1] ;get arg addres
  154. xor rax,rax
  155. %%arg_loop: ;loop over chars in argument
  156. mov r14b,[rsi+rax] ;get next char from argument
  157.  
  158. cmp r14b,91 ;check if it's still correct char ['1','Z']
  159. jge %%end_arg_loop
  160. cmp r14b,48
  161. jle %%end_arg_loop
  162.  
  163. mov [%2+rax],r14b ;save readed char in
  164. add rax,1 ;counter++
  165.  
  166. cmp rax,%3 ;check if counter <> len of arg
  167. jg %%end_arg_loop
  168. jmp %%arg_loop
  169. %%end_arg_loop:
  170. cmp r14b,0 ;check if last char was equal to 0
  171. jnz error
  172. %endmacro
  173.  
  174. section .bss
  175. input resb 4096
  176. output resb 4096
  177. L resb 48
  178. R resb 48
  179. T resb 48
  180. KEY resb 8
  181. R_ resb 48
  182. L_ resb 48
  183.  
  184.  
  185. section .text
  186. align 8
  187. global _start
  188. _start:
  189.  
  190. pop rcx ;get num of cli args
  191. cmp rcx,5 ;check if it's correct number of args
  192. jne error
  193. push rcx
  194.  
  195. get_arg 16,L,42 ;get L from cli args
  196. cmp rax,42
  197. jne error
  198.  
  199. get_arg 24,R,42 ;get R from cli args
  200. cmp rax,42
  201. jne error
  202.  
  203. get_arg 32,T,42 ;get T from cli args
  204. cmp rax,42
  205. jne error
  206.  
  207. get_arg 40,KEY,2 ;get KEY from cli args
  208. cmp rax,2
  209. jne error
  210.  
  211. calc_1 rot_1_R,R_ ;calc rev permutation string for R
  212. calc_1 rot_1_L,L_ ;calc rev permutation string for L
  213.  
  214. mov pos_L,[KEY] ;set start L position
  215. sub pos_L,49
  216.  
  217. mov pos_R,[KEY+1] ;set start R position
  218. sub pos_R,49
  219.  
  220. read_loop: ;loop over input
  221.  
  222. mov rax, 3 ;get block of 4096 bytes of input
  223. mov rbx, stdin
  224. mov rcx, input
  225. mov rdx, 4096
  226. int 80h
  227.  
  228. mov r13,rax ;get number of readed bytes
  229.  
  230. cmp rax,1 ;check if no more chars on input
  231. jle end_read_loop ;if so -> end loop
  232.  
  233. xor r10,r10 ;counter during data block loop
  234. loop_block: ;loop over data block
  235.  
  236. lea r15,[input + r10] ;load addres of char to encrypt
  237. mov r9b,[r15] ;get this char
  238.  
  239. cmp r9b,10 ;check ascii code of char is not equal to 10
  240. je end_read_loop
  241.  
  242. cmp r9b,48 ;check if current char is permited ['1','Z']
  243. jle error ;exit with error if not
  244. cmp r9b,91
  245. jge error
  246.  
  247. turn
  248. ;ENCRYPTION:
  249.  
  250. rot_Q r9b,pos_R ;Qr
  251. rot_R r9b ;R
  252. rot_Q_1 r9b,pos_R ;Qr-1
  253. rot_Q r9b,pos_L ;Ql
  254. rot_L r9b ;L
  255. rot_Q_1 r9b,pos_L ;Ql-1
  256. rot_T r9b ;T
  257. rot_Q r9b,pos_L ;Ql
  258. rot_L_ r9b ;L-1
  259. rot_Q_1 r9b,pos_L ;Ql-1
  260. rot_Q r9b,pos_R ;Qr
  261. rot_R_ r9b ;R-1
  262. rot_Q_1 r9b,pos_R ;Qr-1
  263.  
  264.  
  265. mov [output + r10],r9b ;save encrypted char
  266.  
  267. add r10,1 ;next char
  268.  
  269. cmp r10,r13 ;check if char counter equal to number of chars (last char in block)
  270. jl loop_block ;if its not last char, encrypt next one
  271.  
  272. cmp r13,4096 ;if last -> check if block was 'full'
  273. jl end_read_loop ;if not -> there are no more bytes on input
  274.  
  275. end_loop_block:
  276.  
  277. print output,r13 ;print encrypted block
  278.  
  279. jmp read_loop
  280. end_read_loop:
  281.  
  282. print output,r13 ;print (last) encrypted block
  283.  
  284. exit:
  285. mov rax,60
  286. xor rdi,rdi
  287. syscall
  288.  
  289. error:
  290. print output,r13 ;exit with error
  291.  
  292. mov rax, 60
  293. mov rdi, 1
  294. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement