Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IDEAL
- MODEL small
- STACK 100h
- DATASEG
- ;messages
- startMsg db 'File encryptor V.0.1', 10, 13, 10, 13,\
- ' (C) 2015 (MY REAL LIFE NAME), (MY REAL LIFE SCHOOL)', 10, 13,\
- ' instructor: (MY ASSEMBLY TEACHER)', 10, 13, 10, 13,\
- 'Enter your choice: ', 10, 13, 10, 13,\
- ' (1) Encrypt file', 10, 13,\
- ' (2) Decrypt file', 10, 13,\
- ' (3) Exit', 10, 13, 10, 13,\
- 'Choice: ', '$'
- newlineMsg db 10, 13, '$'
- spaceMsg db ' $'
- pathRequestMsg db 'Please enter the path of the file to be $'
- pathRequestEncMsg db 'Please enter the path of the resulting encrypted file.$'
- pathRequestDecMsg db 'Please enter the path of the resulting decrypted file.$'
- successMsg db 'The file has been successfully $'
- passwordEncMsg db 'Please enter the password to encrypt the file with.$'
- passwordDecMsg db 'Please enter the password of the encrypted file.$'
- encryptedMsg db 'encrypted.$'
- decryptedMsg db 'decrypted.$'
- openErrorMsg db 'Error opening file, error code returned: 0x$'
- noFileMsg db 'Error: file does not exist.$'
- invalidChoiceMsg db 'Error: You somehow chose something invalid.$'
- fileOpenError db 'File opening failed with error code $'
- endMsg db 'Press any key to continue . . .$'
- ;AES related variables
- GLT_EXP db 256 dup(?) ;Galois exponent lookup table
- GLT_LOG db 256 dup(?) ;Galois log lookup table
- rijndaelSbox db 256 dup(?) ;s-box lookup table
- rijndaelState db 16 dup(?)
- rijndaelKey db 16 dup(?)
- rijndaelExpandedKey db 160 dup(?)
- rijndaelTempColumn db 4 dup(?)
- IV db 16 dup(?)
- ;SHA3 related variables
- keccakState dd 25 dup(?)
- keccakTempColumn dd 5 dup(?)
- keccakRoundConstants dd 22 dup(?)
- keccakRotateTable db 24 dup(?)
- keccakTransformationTable db 24 dup(?)
- keccakResult db 32 dup(?)
- ;file IO
- currentBlockBuffer db 16 dup(?)
- pathSrc db 0FFh, 0, 255 dup(?), 8 dup (0)
- pathDst db 0FFh, 0, 255 dup(?), 8 dup (0)
- srcHandle dw ?
- dstHandle dw ?
- passwordBuffer db 0FFh, 0, 255 dup(?)
- ;static values
- ;keccakCapacity equ 64 ;not really needed
- keccakRate equ 36
- keccakDigestSize equ 32
- rijndaelReducingPolynomial equ 1Bh
- choiceCount equ 3
- ;colors
- mainScreenColor equ 02h
- selectionColor equ 03h
- finalSelectionColor equ 30h
- CODESEG
- ;print a string
- proc printString
- ;DX - message offset
- push AX
- mov AH, 09h
- int 21h
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;print the DOS file error code
- proc printDosError
- push DX
- mov DX, offset newlineMsg
- call printString
- cmp AL, 02h
- ;if an error occurred, print the error code
- je noFileError
- mov DX, offset openErrorMsg
- call printString
- call printHex
- pop DX
- ret
- noFileError:
- mov DX, offset noFileMsg
- call printString
- pop DX
- ret
- endp
- ;clear the screen to a specific color, and set background and foreground text colors
- proc clearScreen
- ;BH - foreground and background color
- push AX
- push BX
- push CX
- push DX
- mov AX, 0600h ;clear screen
- ;mov bh, 0Fh ;color
- mov CX, 0000h ;bounds
- mov DX, 0FFFFh ;bounds
- int 10h
- mov AH, 02h
- mov BH, 0
- mov DX, 0000h
- int 10h
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;clear a rectangular area and set the background and foreground colors in it
- proc clearRectangle
- ;BH - foreground and background colors
- ;CX & DX - rectangle bounds
- push AX
- push BX
- push CX
- push DX
- mov AX, 0600h ;clear screen
- ;mov BH, 0Fh ;color
- ;mov CX, 0000h ;bounds
- ;mov DX, 0FFFFh ;bounds
- int 10h
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;set the cursor position
- proc setCursorPos
- ;DX - cursor position
- push AX
- push BX
- push CX
- push DX
- mov AH, 02h
- mov BH, 0
- int 10h
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;write a character at the cursor position
- proc writeChar
- ;AL - character
- ;BL - color
- push AX
- push BX
- push CX
- push DX
- mov AH, 09h
- mov BH, 0 ;page number
- mov CX, 1
- int 10h
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;set the cursor blink shape
- ;set CX to 0x2607 to hide the cursor
- ;set CX to 0x0607 to make the cursor the normal shape
- proc setCursorShape
- ;CH = scan row start
- ;CL = scan row end
- push AX
- mov AH, 01h
- int 10h
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;sleep for the set amount of milliseconds
- proc sleep
- ;AX = time to halt for (ms)
- push AX
- push BX
- push CX
- push DX
- mov BX, 1000
- mul BX
- mov CX, AX
- xchg CX, DX
- mov AH, 86h
- int 15h
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;print AL in hexadecimal
- proc printHex
- ;AL = byte to write
- push AX
- push DX
- mov DH, AL
- mov DL, AL
- mov AH, 02h ;print character code
- shr DL, 4 ;high nibble
- and DH, 00001111b ;low nibble
- ;write the actual byte
- cmp DL, 9
- ja phln1 ;print hex letter nibble
- add DL, 30h
- int 21h
- jmp phln
- phln1:
- add DL, 37h
- int 21h
- phln: ;print hex low nibble
- mov DL, DH
- cmp DL, 9
- ja phln2 ;print hex letter nibble
- add DL, 30h
- int 21h
- pop DX
- pop AX
- ret
- phln2:
- add DL, 37h
- int 21h
- pop DX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;generate the expanded key from rijndaelKey
- proc rijndaelGenerateKey
- push AX
- push BX
- push CX
- push DX
- mov DH, 1
- mov CX, 4
- xor BX, BX
- ;rotate the last column
- rgk1:
- mov DL, [rijndaelKey + BX + 12]
- add BX, 3
- and BX, 11b
- mov [rijndaelTempColumn + BX], DL
- add BX, 2
- and BX, 11b
- loop rgk1
- mov CX, 4
- xor AL, AL
- xor BX, BX
- ;apply the sbox to the rotated column
- rgk2:
- mov BL, AL
- mov BL, [rijndaelTempColumn + BX]
- mov BL, [rijndaelSbox + BX]
- mov AH, BL
- mov BL, AL
- mov [rijndaelTempColumn + BX], AH
- inc AL
- loop rgk2
- ;xor the first item of the column
- ;with the round constant
- xor [rijndaelTempColumn], DH
- shl DH, 1
- jnc rgksr1
- xor DH, rijndaelReducingPolynomial
- rgksr1: ;rijndael generate key skip reduction
- mov CX, 4
- xor BX, BX
- ;xor the column with the first column
- ;of the key and set it as
- ;the first column of the first round key
- rgk3:
- mov DL, [rijndaelKey + BX]
- xor DL, [rijndaelTempColumn + BX]
- mov [rijndaelExpandedKey + BX], DL
- inc BL
- loop rgk3
- mov CX, 12
- mov BX, 4
- ;generate the rest of the first round key
- rgk4:
- mov DL, [rijndaelKey + BX]
- sub BX, 4
- xor DL, [rijndaelExpandedKey + bx]
- add BX, 4
- mov [rijndaelExpandedKey + BX], DL
- inc BX
- loop rgk4
- mov CX, 9
- xor AL, AL
- ;now, generate the other 9 round keys using the same method
- rgk5:
- push CX
- mov CX, 4
- xor BX, BX
- ;rotate the last column of the previous round key
- rgk6:
- add BL, AL
- mov DL, [rijndaelExpandedKey + BX + 12]
- sub BL, AL
- add BX, 3
- and BX, 11b
- mov [rijndaelTempColumn + BX], DL
- add BX, 2
- and BX, 11b
- loop rgk6
- mov CX, 4
- xor AH, AH
- xor DL, DL
- xor BX, BX
- ;apply the sbox to the rotated column
- rgk7:
- mov BL, AH
- mov BL, [rijndaelTempColumn + BX]
- mov BL, [rijndaelSbox + BX]
- mov DL, BL
- mov BL, AH
- mov [rijndaelTempColumn + BX], DL
- inc AH
- loop rgk7
- ;the loop is too big so i have to put an "island"
- ;to jump to to make the jump size stay in range
- jmp fsrj2
- fsrj1: ;fuck short relative jumps
- jmp rgk5
- fsrj2: ;fuck short relative jumps
- ;xor the first item of the column
- ;with the round constant
- xor [rijndaelTempColumn], DH
- shl DH, 1
- jnc rgksr2
- xor DH, rijndaelReducingPolynomial
- rgksr2: ;rijndael generate key skip reduction
- mov CX, 4
- xor BX, BX
- ;xor the column with the first column
- ;of the last round key and set it as
- ;the first column of the next round key
- rgk8:
- add BL, AL
- mov DL, [rijndaelExpandedKey + BX]
- sub BL, AL
- xor DL, [rijndaelTempColumn + BX]
- add BL, AL
- mov [rijndaelExpandedKey + BX + 16], DL
- sub BL, AL
- inc BX
- loop rgk8
- mov CX, 12
- xor BH, BH
- mov BL, AL
- ;generate the rest of the next key
- rgk9:
- mov DL, [rijndaelExpandedKey + BX + 16]
- xor DL, [rijndaelExpandedKey + BX + 4]
- mov [rijndaelExpandedKey + BX + 20], DL
- inc BX
- loop rgk9
- pop CX
- add AL, 16
- loop fsrj1
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;encrypts the rijndaelState block with the rijndaelKey
- proc rijndaelEncryptBlock
- push AX
- push BX
- push CX
- push DX
- xor BX, BX
- mov CX, 16
- ;xor the state with the key
- reb1: ;rijndael encrypt block
- mov DL, [rijndaelKey + BX]
- xor [rijndaelState + BX], DL
- inc BX
- loop reb1
- mov CX, 9
- xor AX, AX
- ;repeat 9 times
- reb2:
- push CX
- mov CX, 16
- xor AL, AL
- ;apply the sbox to each byte of the state
- reb3:
- mov BL, AL
- mov BL, [rijndaelState + BX]
- mov BL, [rijndaelSbox + BX]
- mov DL, BL
- mov BL, AL
- mov [rijndaelState + BX], DL
- inc AL
- loop reb3
- xor BX, BX
- xor DX, DX
- mov CX, 4
- ;shift rows
- reb4:
- mov BL, DH
- xor AL, AL
- ;copy a row to the tempColumn array
- reb5:
- mov DL, [rijndaelState + BX]
- xchg BL, AL
- mov [rijndaelTempColumn + BX], DL
- xchg BL, AL
- add BL, 4
- inc AL
- cmp AL, 4
- jne reb5
- mov AL, DH
- mov BL, DH
- ;copy the tempColumn to its corresponding row after being rotated
- reb6:
- mov DL, [rijndaelTempColumn + BX]
- xchg BL, AL
- mov [rijndaelState + BX], DL
- xchg BL, AL
- add AL, 4
- inc BL
- and BL, 11b ;BL = Bl % 4
- cmp AL, 16
- jb reb6
- inc DH
- loop reb4
- xor CL, CL
- xor BX, BX
- ;mix columns
- reb7:
- xor CH, CH
- mov BL, CL
- ;copy the column to the tempColumn array
- reb8:
- mov DL, [rijndaelState + BX]
- sub BL, CL
- mov [rijndaelTempColumn + BX], DL
- add BL, CL
- inc BL
- inc CH
- cmp CH, 4
- jb reb8
- xor CH, CH
- ;copy the tempColumn multiplied by the matrix back to the state matrix
- mov BL, CH
- ;multiply column by (3x^3 + x^2 + x^1 + 2) mod (x^4 + 1)
- reb9:
- xor DL, DL
- mov DH, [rijndaelTempColumn + BX]
- shl DH, 1
- jnc reb10
- xor DH, rijndaelReducingPolynomial
- reb10:
- xor DL, DH
- inc BL
- and BL, 11b ;BL = BL % 4
- mov DH, [rijndaelTempColumn + BX]
- mov AL, DH
- shl DH, 1
- pushf
- xor DH, AL
- popf
- jnc reb11
- xor DH, rijndaelReducingPolynomial
- reb11:
- xor DL, DH
- inc BL
- and BL, 11b ;BL = BL % 4
- xor DL, [rijndaelTempColumn + BX]
- inc BL
- and BL, 11b ;BL = BL % 4
- xor DL, [rijndaelTempColumn + BX]
- inc BL
- and BL, 11b ;BL = BL % 4
- ;put the result in the state matrix
- add BL, CL
- mov [rijndaelState + BX], DL
- sub BL, CL
- inc BL
- and BL, 11b ;BL = BL % 4
- cmp BL, 0 ;after one iteration BL increments by one, and starts at CH.
- jne reb9 ;so, after the last iteration it wraps back to CH
- add CL, 4
- cmp CL, 16
- jb reb7
- ;the loop is too big so i have to put an "island"
- ;to jump to to make the jump size stay in range
- jmp fsrj4
- fsrj3: ;fuck short relative jumps
- jmp reb2
- fsrj4: ;fuck short relative jumps
- mov CX, 16
- mov AL, AH
- xor BX, BX
- ;add the round key
- reb12:
- mov DL, [rijndaelState + BX]
- xchg BL, AL
- xor DL, [rijndaelExpandedKey + BX]
- xchg BL, AL
- mov [rijndaelState + BX], DL
- inc AL
- inc BL
- loop reb12
- add AH, 16
- pop CX
- loop fsrj3
- ;last round, mix columns stage is omitted
- mov CX, 16
- xor AL, AL
- ;apply the sbox to each byte of the state
- reb13:
- mov BL, AL
- mov BL, [rijndaelState + BX]
- mov BL, [rijndaelSbox + BX]
- mov DL, BL
- mov BL, AL
- mov [rijndaelState + BX], DL
- inc AL
- loop reb13
- xor BX, BX
- xor DX, DX
- mov CX, 4
- ;shift rows
- reb14:
- mov BL, DH
- xor AL, AL
- ;copy a row to the tempColumn array
- reb15:
- mov DL, [rijndaelState + BX]
- xchg BL, AL
- mov [rijndaelTempColumn + BX], DL
- xchg BL, AL
- add BL, 4
- inc AL
- cmp AL, 4
- jne reb15
- mov AL, DH
- mov BL, DH
- ;copy the tempColumn to its corresponding row after being rotated
- reb16:
- mov DL, [rijndaelTempColumn + BX]
- xchg BL, AL
- mov [rijndaelState + BX], DL
- xchg BL, AL
- add AL, 4
- inc BL
- and BL, 11b ;BL = Bl % 4
- cmp AL, 16
- jb reb16
- inc DH
- loop reb14
- mov CX, 16
- mov AL, AH
- xor BX, BX
- ;add the round key
- reb17:
- mov DL, [rijndaelState + BX]
- xchg BL, AL
- xor DL, [rijndaelExpandedKey + BX]
- xchg BL, AL
- mov [rijndaelState + BX], DL
- inc AL
- inc BL
- loop reb17
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;preform the keccak sponge function on the keccakState memory block
- proc keccakRound
- push AX
- push BX
- push CX
- push DX
- xor CX, CX
- xor BX, BX
- kr1:
- push CX
- xor CX, CX
- ;theta
- kr2:
- xor DX, DX
- xor AX, AX
- mov BX, CX
- ;DX = high word, AX = low word
- kr3:
- xor DX, [word ptr keccakState + BX + 0]
- xor AX, [word ptr keccakState + BX + 2]
- add BX, 20
- cmp BX, 100
- jb kr3
- mov BX, CX
- mov [word ptr keccakTempColumn + BX + 0], DX
- mov [word ptr keccakTempColumn + BX + 2], AX
- add CX, 4
- cmp CX, 20
- jb kr2
- xor CX, CX
- kr4:
- ;t = temp[(i + 4) % 5] ^ ROTL32(temp[(i + 1) % 5], 1)
- ;DX:AX = temp[(i + 1) % 5]
- mov BL, CL
- add BL, 4
- cmp BL, 20
- jb kr5
- sub BL, 20
- kr5:
- mov DX, [word ptr keccakTempColumn + BX + 0]
- mov AX, [word ptr keccakTempColumn + BX + 2]
- ;rotate DX:AX one bit to the left
- shl AX, 1
- pushf
- shl DX, 1
- jnc kr6
- or AX, 1
- kr6:
- popf
- jnc kr7
- or DX, 1
- kr7:
- ;DX:AX ^= temp[(i + 4) % 5]
- mov BL, CL
- add BL, 16
- cmp BL, 20
- jb kr8
- sub BL, 20
- kr8:
- xor DX, [word ptr keccakTempColumn + BX + 0]
- xor AX, [word ptr keccakTempColumn + BX + 2]
- mov BL, CL
- ;for (j = 0; j < 25; j += 5){st[j + i] ^= t;}
- kr9:
- xor [word ptr keccakState + BX + 0], DX
- xor [word ptr keccakState + BX + 2], AX
- add BL, 20
- cmp BL, 100
- jb kr9
- add CL, 4
- cmp CL, 20
- jb kr4
- xor BX, BX
- ;kkk:
- ; mov [word ptr keccakState + BX], 0a5a5h
- ;add BX, 4
- ;cmp BX, 100
- ;jb kkk
- xor CX, CX
- mov DX, [word ptr keccakState + 4]
- mov AX, [word ptr keccakState + 6]
- ;rho + pi
- kr10:
- ;j = keccakTransformationTable[i]
- mov BL, CL
- mov BL, [keccakTransformationTable + BX]
- push AX
- push DX
- ;temp[0] = st[j]
- mov DX, [word ptr keccakState + BX + 0]
- mov AX, [word ptr keccakState + BX + 2]
- mov [word ptr keccakTempColumn + 0], DX
- mov [word ptr keccakTempColumn + 2], AX
- pop DX
- pop AX
- mov BL, CL
- mov CH, [keccakRotateTable + BX]
- ;st[j] = ROTL32(t, keccakRotateTable[i])
- kr11:
- shl AX, 1
- pushf
- shl DX, 1
- jnc kr12
- or AX, 1
- kr12:
- popf
- jnc kr13
- or DX, 1
- kr13:
- dec CH
- cmp CH, 0
- jne kr11
- mov BL, CL
- mov BL, [keccakTransformationTable + BX]
- mov [word ptr keccakState + BX + 0], DX
- mov [word ptr keccakState + BX + 2], AX
- ;t = temp[0];
- mov DX, [word ptr keccakTempColumn + 0]
- mov AX, [word ptr keccakTempColumn + 2]
- inc CL
- cmp CL, 24
- jb kr10
- xor CX, CX
- ;chi
- kr14:
- xor CH, CH
- mov BL, CL
- ;for(i = 0; i < 5; i++){tempColumn[i] = st[j + i];}
- kr15:
- mov DX, [word ptr keccakState + BX + 0]
- mov AX, [word ptr keccakState + BX + 2]
- xchg CH, BL
- mov [word ptr keccakTempColumn + BX + 0], DX
- mov [word ptr keccakTempColumn + BX + 2], AX
- xchg CH, BL
- add BL, 4
- add CH, 4
- cmp CH, 20
- jb kr15
- ;for(i = 0; i < 5; i++){st[j + i] ^= (~tempColumn[(i + 1) % 5]) & tempColumn[(i + 2) % 5];}
- kr16:
- mov BL, CH
- add BL, 4
- cmp BL, 20
- jb kr17
- sub BL, 20
- kr17:
- mov DX, [word ptr keccakTempColumn + BX + 0]
- mov AX, [word ptr keccakTempColumn + BX + 2]
- not DX
- not AX
- add BL, 4
- cmp BL, 20
- jb kr18
- sub BL, 20
- kr18:
- and DX, [word ptr keccakTempColumn + BX + 0]
- and AX, [word ptr keccakTempColumn + BX + 2]
- mov BL, CH
- add BL, CL
- xor [word ptr keccakState + BX + 0], DX
- xor [word ptr keccakState + BX + 2], AX
- add CH, 4
- cmp CH, 20
- jb kr16
- add CL, 20
- cmp CL, 100
- jb kr14
- pop CX
- mov BX, CX
- shl BX, 2
- mov DX, [word ptr keccakRoundConstants + BX + 0]
- mov AX, [word ptr keccakRoundConstants + BX + 2]
- xor [word ptr keccakState + 0], DX
- xor [word ptr keccakState + 2], AX
- inc CX
- cmp CX, 22
- jae kr19
- jmp kr1
- kr19:
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;gets the keccak hash of DS:BX of length AX and puts it in keccakResult
- proc getKeccakHash
- push AX
- push BX
- push CX
- push DX
- ;clear the state
- mov CX, 100
- push BX
- xor BX, BX
- xor DL, DL
- gkh1:
- mov [byte ptr keccakState + BX], DL
- inc BX
- loop gkh1
- pop BX
- ;absorb all blocks
- gkh2:
- cmp AX, keccakRate
- jb gkh4
- xor CX, CX
- gkh3:
- mov DL, [BX]
- xchg BX, CX
- xor [byte ptr keccakState + BX], DL
- xchg BX, CX
- inc BX
- inc CX
- cmp CX, keccakRate
- jb gkh3
- sub AX, keccakRate
- call keccakRound
- jmp gkh2
- gkh4:
- ;if there is no last block skip padding
- cmp AX, 0
- je gkh5
- xor CX, CX
- mov DH, keccakRate
- sub DH, AL
- ;last block and padding
- gkh6:
- mov DL, [BX]
- xchg BX, CX
- xor [byte ptr keccakState + BX], DL
- xchg BX, CX
- inc BX
- inc CX
- dec AX
- cmp AX, 0
- ja gkh6
- mov BX, CX
- xor CX, CX
- mov CL, DH
- mov DH, 01h
- xor [byte ptr keccakState + BX + 1], DH
- add BX, CX
- mov DH, 80h
- xor [byte ptr keccakState + BX], DH
- call keccakRound
- gkh5:
- mov CX, 32
- xor BX, BX
- ;copy to keccakResult
- gkh7:
- mov DL, [byte ptr keccakState + BX]
- mov [keccakResult + BX], DL
- inc BX
- loop gkh7
- ;now, the result of the hash is stored in keccakResult
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;initialize all the lookup tables and constant AES and SHA3 need
- proc initializeTables
- push AX
- push BX
- push CX
- push DX
- ;---- initialize Galois field exponent and log tables ----
- mov [GLT_EXP], 1
- mov AX, 1
- mov CX, 0FFh
- xor BX, BX
- it1:
- inc BL
- ; GF multiply by 3:
- mov DL, AL
- shl AL, 1
- pushf
- xor AL, DL
- popf
- jnc it2
- xor AL, rijndaelReducingPolynomial
- it2:
- ; gf_exp[i] = x;
- ; gf_log[x] = i;
- mov [GLT_EXP + BX], AL
- xchg AL, BL
- mov [GLT_LOG + BX], AL
- xchg AL, BL
- loop it1
- ; gf_log[gf_exp[0]] = 0;
- mov BL, [GLT_EXP]
- mov [GLT_LOG + BX], 0
- ;---- generate AES' S-box ----
- ;sbox[i] = i^-1; (in GF(2^8)
- ;sbox[0] = 0;
- mov CX, 0FFh
- xor AX, AX
- mov [rijndaelSbox], 0
- ;prepare sub box: multiplicative inverse
- it3:
- inc AX
- ;x^-1 = g^-log(x)
- mov BX, AX
- mov BL, [GLT_LOG + BX]
- not BL ;bl = -bl (mod 255)
- mov BL, [GLT_EXP + BX]
- mov DL, BL
- mov BX, AX
- mov [rijndaelSbox + BX], DL
- loop it3
- ;sbox[i] = 0x63 ^
- ; sbox[i] ^
- ; ROTL8(sbox[i], 1) ^
- ; ROTL8(sbox[i], 2) ^
- ; ROTL8(sbox[i], 3) ^
- ; ROTL8(sbox[i], 4);
- mov CX, 100h
- xor BX, BX
- ;prepare sub box: affine transformation
- it4:
- mov AL, [rijndaelSbox + BX]
- mov DL, AL
- rol DL, 1
- xor AL, DL
- rol DL, 1
- xor AL, DL
- rol DL, 1
- xor AL, DL
- rol DL, 1
- xor AL, DL
- xor AL, 63h
- mov [rijndaelSbox + BX], AL
- inc BX
- loop it4
- ;---- prepare SHA3 constants ----
- ;prepare shift values (pretty much triangular numbers modulo 32)
- mov CX, 24
- xor AX, AX
- xor DX, DX
- xor BX, BX
- ;prepare keccak shift values
- it5:
- inc DL
- add AL, DL
- and AL, 11111b ;mod 32
- mov [keccakRotateTable + BX], AL
- inc BX
- loop it5
- ;prepare PI transformation values
- ;DL, DH: current X and Y values of the vector
- mov DL, 1
- mov DH, 0
- mov CX, 24
- xor BX, BX
- ; repeat matrix multiplication:
- ; |DL| = |0 1| * |DL|
- ; |DH| |2 3| |DH|
- ;prepare keccak transformation values
- it6:
- ;AL += 2 * DL
- xor AL, AL
- shl DL, 1
- add AL, DL
- ;AL += 3 * DH
- mov AH, DH
- add AL, AH
- shl AH, 1
- add AL, AH
- ;AL = AL % 5
- mov DL, 5
- xor AH, AH
- div DL
- ;finally, reorder DL and DH to get the transformed vector
- mov DL, DH
- mov DH, AH
- ;AL = DL + 5 * DH (array flattening)
- mov AX, DX
- add AL, AH
- shl AH, 2
- add AL, AH
- shl AL, 2 ;multiply by 4 because each value takes 4 bytes
- mov [keccakTransformationTable + BX], AL
- inc BX
- loop it6
- xor CX, CX
- mov BH, 1
- ;prepare keccak round constants
- it7:
- xor CL, CL
- xor AX, AX
- xor DX, DX
- it8:
- test BH, 1
- jz it9
- push BX
- mov BX, 1
- cmp CL, 16
- jae it10
- shl BX, CL
- or AX, BX
- jmp it11
- it10:
- sub CX, 16
- shl BX, CL
- or DX, BX
- add CX, 16
- it11:
- pop BX
- it9:
- shl BH, 1
- jnc it12
- xor BH, 01110001b
- it12:
- shl CL, 1
- inc CL
- cmp CL, 32
- jb it8
- shl BH, 1
- jnc it13
- xor BH, 01110001b
- it13:
- push BX
- xor BX, BX
- mov BL, CH
- mov [word ptr keccakRoundConstants + BX + 0], AX
- mov [word ptr keccakRoundConstants + BX + 2], DX
- pop BX
- add CH, 4
- cmp CH, 88
- jb it7
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;remove some bytes from the end of the file
- proc truncateFile
- ;BX = file handle
- ;DX = number of bytes to remove
- push AX
- push BX
- push CX
- push DX
- mov AL, 2
- mov CX, 0FFFFh
- not DX
- mov AH, 42h
- int 21h
- mov CX, 0
- mov AH, 40h
- int 21h
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;##########################################################################################
- ;##########################################################################################
- ;##########################################################################################
- ;copy a block of memory to another location
- proc memcpy
- ;AX = source address
- ;BX = destination address
- ;CX = count
- push AX
- push BX
- push CX
- push DX
- mcl:
- xchg AX, BX
- mov DL, [BX]
- inc BX
- xchg AX, BX
- mov [BX], DL
- inc BX
- loop mcl
- pop DX
- pop CX
- pop BX
- pop AX
- ret
- endp
- ;---wait for a key press snippet---
- ; mov ah, 0
- ; int 16h ;wait for a keypress
- ;################################################################################
- ;################################################################################
- ;################################################################################
- ;################################ PROGRAM START #############w##################
- ;################################################################################
- ;################################################################################
- ;################################################################################
- start:
- mov AX, @data
- mov DS, AX
- ;code
- ;######################### SETUP #########################
- ;hide cursor
- mov CX, 2607h
- call setCursorShape
- call initializeTables
- getChoice:
- mov BH, mainScreenColor
- call clearScreen
- mov DX, offset startMsg
- call printString
- ;############################## INPUT CHOICE #############################
- ;---- read untill a valid choice is read ----
- GFVI:
- ;read char
- mov AH, 00h
- int 16h
- sub AL, 31h
- cmp AL, choiceCount
- jae GFVI
- add AL, 31h
- mov BL, selectionColor
- call writeChar
- ;---- read untill enter is pressed ----
- GI:
- mov DL, AL
- GII:
- ;read char
- mov AH, 00h
- int 16h
- cmp AL, 0Dh ;enter key
- je EIL
- sub AL, 31h
- cmp AL, choiceCount
- jae GII
- add AL, 31h
- mov BL, selectionColor
- call writeChar
- jmp GI
- EIL: ;exit input loop
- ;---- finally print the choice in a different color/style ----
- mov CX, 2607h
- call setCursorShape
- mov AL, DL
- mov BL, finalSelectionColor
- call writeChar
- sub DL, 31h ;convert DL from ASCII to 0 indexed
- ;;pause for a second
- ;mov AX, 500
- ;call sleep
- ;####################### DO SOMETHING ACCORDING TO INPUT ################################
- ;---- switch the choice ----
- cmp DL, 0
- je sc0
- cmp DL, 1
- je sc1
- cmp DL, 2
- je sc2
- sc0:
- jmp fileEncrypt
- sc1:
- jmp fileDecrypt
- sc2:
- jmp exitSequence
- ;---- if the choice is invalid, display error and exit the program
- mov DX, offset invalidChoiceMsg
- call printString
- jmp exit
- ;#####################################################################################
- ;################################# FILE ENCRYPTION ###################################
- ;#####################################################################################
- fileEncrypt:
- mov DX, offset newlineMsg
- call printString
- mov DX, offset pathRequestMsg
- call printString
- mov DX, offset encryptedMsg
- call printString
- mov DX, offset newlineMsg
- call printString
- ;#################### get the file path ####################
- mov AH, 0Ah
- mov DX, offset pathSrc
- int 21h
- ;replace the CR EOS with null (ASCIIZ)
- xor BX, BX
- mov BL, [pathSrc + 1]
- mov [pathSrc + BX + 2], 0
- ;---- open the file ----
- mov AH, 3Dh
- mov AL, 0
- mov DX, offset pathSrc
- add DX, 2
- int 21h
- jnc fileSucceed1
- call printDosError
- mov AH, 0
- int 16h ;wait for a keypress
- jmp getChoice
- fileSucceed1:
- mov [srcHandle], AX
- ;#################### get the file destination path ####################
- mov DX, offset newlineMsg
- call printString
- mov DX, offset pathRequestEncMsg
- call printString
- mov DX, offset newlineMsg
- call printString
- mov AH, 0Ah
- mov DX, offset pathDst
- int 21h
- ;replace the CR EOS with null (ASCIIZ)
- xor BX, BX
- mov BL, [pathDst + 1]
- mov [pathDst + BX + 2], 0
- ;---- create the file ----
- mov AH, 3Ch
- mov AL, 1
- mov DX, offset pathDst
- add DX, 2
- xor CX, CX
- int 21h
- jnc fileSucceed2
- call printDosError
- mov AH, 0
- int 16h ;wait for a keypress
- ;close the first file
- mov AH, 3Eh
- mov BX, [word ptr srcHandle]
- int 21h
- jmp getChoice
- fileSucceed2:
- mov [dstHandle], AX
- ;###################### get password ######################
- mov DX, offset newlineMsg
- call printString
- mov DX, offset passwordEncMsg
- call printString
- mov DX, offset newlineMsg
- call printString
- mov AH, 0Ah
- mov DX, offset passwordBuffer
- int 21h
- ;###################### hash password ######################
- xor AX, AX
- mov AL, [passwordBuffer + 1]
- mov BX, offset passwordBuffer
- add BX, 2
- call getKeccakHash
- mov AX, offset keccakResult
- mov BX, offset rijndaelKey
- mov CX, 16
- call memcpy
- mov AX, offset keccakResult
- add AX, 16
- mov BX, offset IV
- call memcpy
- ;encrypt <s>with visuals</s>
- call rijndaelGenerateKey
- mov AX, offset IV
- mov BX, offset rijndaelState
- mov CX, 16
- call memcpy
- ;encrypt/decrypt using OFB
- ;in OFB encryption and decryption are exactly the same
- encryptLoop:
- ;get the next block of the OTP by encrypting the previous block
- call rijndaelEncryptBlock
- ;read a block of 16 bytes from the plaintext
- mov AH, 3Fh
- mov BX, [srcHandle]
- mov CX, 16
- mov DX, offset currentBlockBuffer
- int 21h
- ;check how many bytes were successfully read
- cmp AX, 16
- jne encryptLoopExit
- xor BX, BX
- ;xor the block of plaintext with the OTP
- encryptXorBlock:
- mov DL, [rijndaelState + BX]
- xor [currentBlockBuffer + BX], DL
- inc BX
- cmp BX, 16
- jb encryptXorBlock
- ;write to file
- mov AH, 40h
- mov BX, [dstHandle]
- mov CX, 16
- mov DX, offset currentBlockBuffer
- int 21h
- jmp encryptLoop
- encryptLoopExit:
- ;encrypt the last partial block
- xor BX, BX
- ;xor the block of plaintext with the OTP
- encryptXorPartial:
- mov DL, [rijndaelState + BX]
- xor [currentBlockBuffer + BX], DL
- inc BX
- cmp BX, AX
- jb encryptXorPartial
- mov CX, AX
- mov AH, 40h
- mov BX, [dstHandle]
- mov DX, offset currentBlockBuffer
- int 21h
- mov DX, offset newlineMsg
- call printString
- mov DX, offset successMsg
- call printString
- mov DX, offset encryptedMsg
- call printString
- mov AH, 3Eh
- mov BX, [word ptr srcHandle]
- int 21h
- mov AH, 3Eh
- mov BX, [word ptr dstHandle]
- int 21h
- mov AH, 0
- int 16h ;wait for a keypress
- jmp workDone
- ;#####################################################################################
- ;################################# FILE DECRYPTION ###################################
- ;#####################################################################################
- fileDecrypt:
- mov DX, offset newlineMsg
- call printString
- mov DX, offset pathRequestMsg
- call printString
- mov DX, offset decryptedMsg
- call printString
- mov DX, offset newlineMsg
- call printString
- ;#################### get the file path ####################
- mov AH, 0Ah
- mov DX, offset pathSrc
- int 21h
- ;replace the CR EOS with null (ASCIIZ)
- xor BX, BX
- mov BL, [pathSrc + 1]
- mov [pathSrc + BX + 2], 0
- ;---- open the file ----
- mov AH, 3Dh
- mov AL, 0
- mov DX, offset pathSrc
- add DX, 2
- int 21h
- jnc fileSucceed3
- call printDosError
- mov AH, 0
- int 16h ;wait for a keypress
- jmp getChoice
- fileSucceed3:
- mov [srcHandle], AX
- ;#################### get the file destination path ####################
- mov DX, offset newlineMsg
- call printString
- mov DX, offset pathRequestDecMsg
- call printString
- mov DX, offset newlineMsg
- call printString
- mov AH, 0Ah
- mov DX, offset pathDst
- int 21h
- ;replace the CR EOS with null (ASCIIZ)
- xor BX, BX
- mov BL, [pathDst + 1]
- mov [pathDst + BX + 2], 0
- ;---- create the file ----
- mov AH, 3Ch
- mov AL, 1
- mov DX, offset pathDst
- add DX, 2
- xor CX, CX
- int 21h
- jnc fileSucceed4
- call printDosError
- mov AH, 0
- int 16h ;wait for a keypress
- ;close the first file
- mov AH, 3Eh
- mov BX, [word ptr srcHandle]
- int 21h
- jmp getChoice
- fileSucceed4:
- mov [dstHandle], AX
- ;###################### get password ######################
- mov DX, offset newlineMsg
- call printString
- mov DX, offset passwordDecMsg
- call printString
- mov DX, offset newlineMsg
- call printString
- mov AH, 0Ah
- mov DX, offset passwordBuffer
- int 21h
- ;###################### hash password ######################
- xor AX, AX
- mov AL, [passwordBuffer + 1]
- mov BX, offset passwordBuffer
- add BX, 2
- call getKeccakHash
- mov AX, offset keccakResult
- mov BX, offset rijndaelKey
- mov CX, 16
- call memcpy
- mov AX, offset keccakResult
- add AX, 16
- mov BX, offset IV
- call memcpy
- ;encrypt <s>with visuals</s>
- call rijndaelGenerateKey
- mov AX, offset IV
- mov BX, offset rijndaelState
- mov CX, 16
- call memcpy
- ;encrypt/decrypt using OFB
- ;in OFB encryption and decryption are exactly the same
- decryptLoop:
- ;get the next block of the OTP by encrypting the previous block
- call rijndaelEncryptBlock
- ;read a block of 16 bytes from the plaintext
- mov AH, 3Fh
- mov BX, [srcHandle]
- mov CX, 16
- mov DX, offset currentBlockBuffer
- int 21h
- ;check how many bytes were successfully read
- cmp AX, 16
- jne decryptLoopExit
- xor BX, BX
- ;xor the block of plaintext with the OTP
- decryptXorBlock:
- mov DL, [rijndaelState + BX]
- xor [currentBlockBuffer + BX], DL
- inc BX
- cmp BX, 16
- jb decryptXorBlock
- ;write to file
- mov AH, 40h
- mov BX, [dstHandle]
- mov CX, 16
- mov DX, offset currentBlockBuffer
- int 21h
- jmp decryptLoop
- decryptLoopExit:
- ;encrypt the last partial block
- xor BX, BX
- ;xor the block of plaintext with the OTP
- decryptXorPartial:
- mov DL, [rijndaelState + BX]
- xor [currentBlockBuffer + BX], DL
- inc BX
- cmp BX, AX
- jb decryptXorPartial
- mov CX, AX
- mov AH, 40h
- mov BX, [dstHandle]
- mov DX, offset currentBlockBuffer
- int 21h
- mov DX, offset newlineMsg
- call printString
- mov DX, offset successMsg
- call printString
- mov DX, offset decryptedMsg
- call printString
- mov AH, 3Eh
- mov BX, [word ptr srcHandle]
- int 21h
- mov AH, 3Eh
- mov BX, [word ptr dstHandle]
- int 21h
- mov AH, 0
- int 16h ;wait for a keypress
- jmp workDone
- workDone:
- jmp start
- exitSequence:
- ;jmp exit
- ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- ;@@@@@@@@@@@@@@@@@@@@@ PROGRAM EXIT @@@@@@@@@@@@@@@@@@@@@@@@@
- ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- exit:
- ;pause the program when its done
- mov BH, 07h
- call clearScreen
- mov DX, offset endMsg
- call printString
- mov AH, 0
- int 16h ;wait for a keypress
- mov BH, 07h
- call clearScreen
- mov CX, 0607h
- call setCursorShape
- mov AX, 4c00h
- int 21h
- END start
- ;https://www-s.acm.illinois.edu/sigops/roll_your_own/hardware/kb.html
- ;http://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
- ;http://www.moserware.com/2009/09/stick-figure-guide-to-advanced.html
- ;http://swag.outpostbbs.net/FILES/0084.PAS.html
- ;http://en.wikipedia.org/wiki/MS-DOS_API
- ;http://stanislavs.org/helppc/int_21.html
- ;https://courses.engr.illinois.edu/ece390/books/artofasm/CH13/CH13-6.html#HEADING6-321
- ;http://stanislavs.org/helppc/int_21-40.html
- ;http://stanislavs.org/helppc/int_21-3f.html
- ;http://stanislavs.org/helppc/int_21-3e.html
- ;http://stanislavs.org/helppc/int_21-3d.html
- ;http://stanislavs.org/helppc/int_21.html
- ;YKULVAARLCK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement