Skip to content

Commit c806bac

Browse files
committed
Code cleanup and ui fix.
1 parent c4e24ab commit c806bac

File tree

6 files changed

+100
-36
lines changed

6 files changed

+100
-36
lines changed

AES-Encryption.asm

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,91 @@ section .data
1111
message TIMES 16 db 0
1212
key TIMES 16 db 0
1313
PromptMessage db "Enter the message: ",0
14+
PromptHex db "Enter the 16 hex bytes without leading '0x', pad the bytes to be 2 digits wide:", 0xA
1415
PromptKey db "Enter the key: ", 0
1516
PromptDec db "1.Encrypt", 0xA, "2.Decrypt", 0xA
1617
section .text
1718
_start:
18-
; mov eax, [esp+8]
19-
; call SetSocketFromArg
20-
; mov esi, prompt
21-
; mov ecx, 30
22-
; call WriteString
23-
; mov bx, [socket]
24-
; xchg bl, bh
25-
; movzx eax, bx
26-
; call WriteDec
27-
; call WriteLine
28-
29-
; call InitialiseServer
30-
mov esi, PromptMessage
19+
cmp DWORD [esp], 1
20+
ja .server
21+
;Ask whether to encrypt or decrypt
22+
mov esi, PromptDec
3123
mov ecx, 20
3224
call WriteString
25+
call ReadChar
26+
cmp al, 0x31
27+
jne .decrypt
3328

29+
;Print prompt for message
30+
mov esi, PromptMessage
31+
mov ecx, 20
32+
call WriteString
33+
;Read message
3434
mov esi, message
3535
mov ecx, 18
3636
call ReadString
37-
37+
;Print prompt for key
3838
mov esi, PromptKey
3939
mov ecx, 15
4040
call WriteString
41-
41+
;Read key
4242
mov esi, key
4343
mov ecx, 17
4444
call ReadString
45-
46-
mov esi, PromptDec
47-
mov ecx, 20
48-
call WriteString
49-
50-
call ReadChar
51-
cmp al, 0x31
52-
jne .decrypt
45+
;Encrypt the message
5346
mov esi, message
5447
mov edi, key
5548
call Encrypt
49+
50+
;Print the encrypted message
51+
mov esi, message
52+
call Print4x4Matrix
5653
jmp .done
54+
5755
.decrypt:
56+
;Print promp for encrypted message
57+
mov esi, PromptHex
58+
mov ecx, 80
59+
call WriteString
60+
;Read 16 bytes
61+
mov esi, message
62+
mov ecx, 16
63+
.input:
64+
call ReadHexByte
65+
mov BYTE[esi], al
66+
inc esi
67+
loop .input
68+
;Print prompt for key
69+
mov esi, PromptKey
70+
mov ecx, 15
71+
call WriteString
72+
;Read key
73+
mov esi, key
74+
mov ecx, 17
75+
call ReadString
76+
;Decrypt the string
5877
mov esi, message
5978
mov edi, key
6079
call Decrypt
61-
.done:
80+
;Print decrypted string
6281
mov esi, message
6382
mov ecx, 16
6483
call WriteString
84+
jmp .done
85+
.server:
86+
mov eax, [esp+8]
87+
call SetSocketFromArg
88+
mov esi, prompt
89+
mov ecx, 30
90+
call WriteString
91+
mov bx, [socket]
92+
xchg bl, bh
93+
movzx eax, bx
94+
call WriteDec
95+
call WriteLine
96+
97+
call InitialiseServer
98+
.done:
6599
;Return zero
66100
mov eax, 0x1
67101
mov ebx, 0

ColumnMixing.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ section .text
239239
jl .nextColumn
240240
ret
241241

242-
242+
;Prepares the galios field during the decryption phase
243243
GaliosDecryption:
244244
push eax
245245
mov ecx, 16

Encryption.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
%include "Substitution.inc" ; Substitution functions
2-
%include "Rotation.inc" ; Rotation functions
3-
%include "ColumnMixing.inc" ; Column mixing functions
4-
%include "RoundKey.inc" ; Round key generation functions
5-
%include "AuxillaryFunctions.inc"
1+
%include "Substitution.inc" ; Substitution functions
2+
%include "Rotation.inc" ; Rotation functions
3+
%include "ColumnMixing.inc" ; Column mixing functions
4+
%include "RoundKey.inc" ; Round key generation functions
5+
%include "AuxillaryFunctions.inc" ; Auxillary misc. functions
66
section .data
77
mixedMessage TIMES 16 db 0
88
currentRoundKey TIMES 16 db 0

IO.inc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ReadChar:
2424
mov eax, 3 ;sys_read systemcall
2525
mov ebx, 0 ;STDIN file descriptor
2626
mov ecx, esp ;move the allocated dword address to ecx
27-
mov edx, 1 ;Reading one byte
27+
mov edx, 2 ;Reading one byte
2828
int 80h ;Invoke the syscall
2929

3030
mov al, BYTE [esp] ;Move the read value into al
@@ -103,6 +103,38 @@ WriteDec:
103103
.done:
104104
ret
105105

106+
; Returns:
107+
; al: value of the hex byte
108+
ReadHexByte:
109+
push ecx
110+
push esi
111+
sub esp, 4 ;allocate a dword on the stack
112+
mov eax, 3 ;sys_read systemcall
113+
mov ebx, 0 ;STDIN file descriptor
114+
mov ecx, esp ;move the allocated dword address to ecx
115+
mov edx, 3 ;Reading 2 bytes
116+
int 80h ;Invoke the syscall
117+
mov esi, esp
118+
119+
mov ecx, 2
120+
.nextbyte:
121+
cmp BYTE[esi], '9'
122+
jle .zeroto9
123+
sub BYTE[esi], 0x27
124+
.zeroto9:
125+
sub BYTE[esi], 0x30
126+
inc esi
127+
loop .nextbyte
128+
129+
movzx ax, BYTE [esp] ;Move the read value into ax
130+
shl ax, 8
131+
mov al, BYTE [esp+1]
132+
shl al, 4
133+
shr ax, 4
134+
add esp, 4 ;Clean up the stack
135+
pop esi
136+
pop ecx
137+
ret
106138
; Takes:
107139
; eax: value
108140
; Prints the value in hexa (base-16)

Socket.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
section .data
2-
response db 'HTTP/1.1 200 OK', 0Dh, 0Ah, 'Content-Type: text/html', 0Dh, 0Ah, 'Content-Length: 16', 0Dh, 0Ah, 0Dh, 0Ah, 'ABCDABCDABCDABCD', 0Dh, 0Ah, 0h
2+
response db 'HTTP/1.1 200 OK', 0Dh, 0Ah, 'Content-Type: text/html', 0Dh, 0Ah, 'Content-Length: 16', 0Dh, 0Ah, 0Dh, 0Ah, 'TestMessage12345', 0Dh, 0Ah, 0h
33
prompt db 'Running on localhost at port: '
44
section .bss
55
buffer resb 255, ; variable to store request headers
@@ -62,7 +62,7 @@ InitialiseServer:
6262

6363
jmp .accept ; jmp in parent process to _accept
6464
65-
;Read the sent request
65+
;Read the received request
6666
.read:
6767
mov edx, 255 ; number of bytes to read (we will only read the first 255 bytes for simplicity)
6868
mov ecx, buffer ; move the memory address of our buffer variable into ecx

Substitution.inc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
%include "SBOX.inc" ; File contining SBOX and its functions
22
%include "SBOXInverse.inc"
3-
section .data
4-
tmpbh db 0
3+
54
section .text
65
;Takes:
76
; cl: value
@@ -31,7 +30,6 @@ section .text
3130
SubstituteMessage:
3231
.substitute:
3332
push ecx
34-
mov [tmpbh],bh
3533
mov cl, [esi]
3634
call GetLowerNibble
3735
call GetHigherNibble

0 commit comments

Comments
 (0)