Advertisement
Guest User

Assembly AES SHA3 File Encryptor

a guest
Apr 8th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. IDEAL
  2. MODEL small
  3. STACK 100h
  4. DATASEG
  5.     ;messages
  6.     startMsg db 'File encryptor V.0.1', 10, 13, 10, 13,\
  7.                 ' (C) 2015 (MY REAL LIFE NAME), (MY REAL LIFE SCHOOL)', 10, 13,\
  8.                 ' instructor: (MY ASSEMBLY TEACHER)', 10, 13, 10, 13,\
  9.                 'Enter your choice: ', 10, 13, 10, 13,\
  10.                 ' (1) Encrypt file', 10, 13,\
  11.                 ' (2) Decrypt file', 10, 13,\
  12.                 ' (3) Exit', 10, 13, 10, 13,\
  13.                 'Choice: ', '$'
  14.                
  15.     newlineMsg db 10, 13, '$'
  16.     spaceMsg db ' $'
  17.     pathRequestMsg db 'Please enter the path of the file to be $'
  18.     pathRequestEncMsg db 'Please enter the path of the resulting encrypted file.$'
  19.     pathRequestDecMsg db 'Please enter the path of the resulting decrypted file.$'
  20.     successMsg db 'The file has been successfully $'
  21.     passwordEncMsg db 'Please enter the password to encrypt the file with.$'
  22.     passwordDecMsg db 'Please enter the password of the encrypted file.$'
  23.     encryptedMsg db 'encrypted.$'
  24.     decryptedMsg db 'decrypted.$'
  25.     openErrorMsg db 'Error opening file, error code returned: 0x$'
  26.     noFileMsg db 'Error: file does not exist.$'
  27.     invalidChoiceMsg db 'Error: You somehow chose something invalid.$'
  28.     fileOpenError db 'File opening failed with error code $'
  29.    
  30.     endMsg db 'Press any key to continue . . .$'
  31.    
  32.     ;AES related variables
  33.     GLT_EXP db 256 dup(?) ;Galois exponent lookup table
  34.     GLT_LOG db 256 dup(?) ;Galois log lookup table
  35.     rijndaelSbox db 256 dup(?) ;s-box lookup table
  36.     rijndaelState db 16 dup(?)
  37.     rijndaelKey db 16 dup(?)
  38.     rijndaelExpandedKey db 160 dup(?)
  39.     rijndaelTempColumn db 4 dup(?)
  40.     IV db 16 dup(?)
  41.    
  42.     ;SHA3 related variables
  43.     keccakState dd 25 dup(?)
  44.     keccakTempColumn dd 5 dup(?)
  45.     keccakRoundConstants dd 22 dup(?)
  46.     keccakRotateTable db 24 dup(?)
  47.     keccakTransformationTable db 24 dup(?)
  48.     keccakResult db 32 dup(?)
  49.    
  50.     ;file IO
  51.     currentBlockBuffer db 16 dup(?)
  52.     pathSrc db 0FFh, 0, 255 dup(?), 8 dup (0)
  53.     pathDst db 0FFh, 0, 255 dup(?), 8 dup (0)
  54.     srcHandle dw ?
  55.     dstHandle dw ?
  56.     passwordBuffer db 0FFh, 0, 255 dup(?)
  57.    
  58.     ;static values
  59.     ;keccakCapacity equ 64 ;not really needed
  60.     keccakRate equ 36
  61.     keccakDigestSize equ 32
  62.     rijndaelReducingPolynomial equ 1Bh
  63.     choiceCount equ 3
  64.    
  65.     ;colors
  66.     mainScreenColor equ 02h
  67.     selectionColor equ 03h
  68.     finalSelectionColor equ 30h
  69. CODESEG
  70.  
  71. ;print a string
  72. proc printString
  73.     ;DX - message offset
  74.     push AX
  75.    
  76.     mov AH, 09h
  77.     int 21h
  78.    
  79.     pop AX
  80.    
  81.     ret
  82. endp
  83.  
  84. ;##########################################################################################
  85. ;##########################################################################################
  86. ;##########################################################################################
  87.  
  88. ;print the DOS file error code
  89. proc printDosError
  90.     push DX
  91.    
  92.     mov DX, offset newlineMsg
  93.     call printString
  94.    
  95.     cmp AL, 02h
  96.     ;if an error occurred, print the error code
  97.     je noFileError
  98.         mov DX, offset openErrorMsg
  99.         call printString
  100.         call printHex
  101.        
  102.         pop DX
  103.         ret
  104.     noFileError:
  105.         mov DX, offset noFileMsg
  106.         call printString
  107.        
  108.         pop DX
  109.         ret
  110. endp
  111.  
  112. ;clear the screen to a specific color, and set background and foreground text colors
  113. proc clearScreen
  114.     ;BH - foreground and background color
  115.     push AX
  116.     push BX
  117.     push CX
  118.     push DX
  119.    
  120.     mov AX, 0600h ;clear screen
  121.     ;mov bh, 0Fh ;color
  122.     mov CX, 0000h ;bounds
  123.     mov DX, 0FFFFh ;bounds
  124.     int 10h
  125.        
  126.     mov AH, 02h
  127.     mov BH, 0
  128.     mov DX, 0000h
  129.     int 10h
  130.    
  131.     pop DX
  132.     pop CX
  133.     pop BX
  134.     pop AX
  135.    
  136.     ret
  137. endp
  138.  
  139. ;##########################################################################################
  140. ;##########################################################################################
  141. ;##########################################################################################
  142.  
  143. ;clear a rectangular area and set the background and foreground colors in it
  144. proc clearRectangle
  145.     ;BH - foreground and background colors
  146.     ;CX & DX - rectangle bounds
  147.     push AX
  148.     push BX
  149.     push CX
  150.     push DX
  151.    
  152.     mov AX, 0600h ;clear screen
  153.     ;mov BH, 0Fh ;color
  154.     ;mov CX, 0000h ;bounds
  155.     ;mov DX, 0FFFFh ;bounds
  156.     int 10h
  157.    
  158.     pop DX
  159.     pop CX
  160.     pop BX
  161.     pop AX
  162.    
  163.     ret
  164. endp
  165.  
  166. ;##########################################################################################
  167. ;##########################################################################################
  168. ;##########################################################################################
  169.  
  170. ;set the cursor position
  171. proc setCursorPos
  172.     ;DX - cursor position
  173.     push AX
  174.     push BX
  175.     push CX
  176.     push DX
  177.    
  178.     mov AH, 02h
  179.     mov BH, 0
  180.     int 10h
  181.    
  182.     pop DX
  183.     pop CX
  184.     pop BX
  185.     pop AX
  186.    
  187.     ret
  188. endp
  189.  
  190. ;##########################################################################################
  191. ;##########################################################################################
  192. ;##########################################################################################
  193.  
  194. ;write a character at the cursor position
  195. proc writeChar
  196.     ;AL - character
  197.     ;BL - color
  198.    
  199.     push AX
  200.     push BX
  201.     push CX
  202.     push DX
  203.    
  204.     mov AH, 09h
  205.     mov BH, 0 ;page number
  206.     mov CX, 1
  207.     int 10h
  208.    
  209.     pop DX
  210.     pop CX
  211.     pop BX
  212.     pop AX
  213.    
  214.     ret
  215. endp
  216.  
  217. ;##########################################################################################
  218. ;##########################################################################################
  219. ;##########################################################################################
  220.  
  221. ;set the cursor blink shape
  222. ;set CX to 0x2607 to hide the cursor
  223. ;set CX to 0x0607 to make the cursor the normal shape
  224. proc setCursorShape
  225.     ;CH = scan row start
  226.     ;CL = scan row end
  227.    
  228.     push AX
  229.    
  230.     mov AH, 01h
  231.     int 10h
  232.    
  233.     pop AX
  234.    
  235.     ret
  236. endp
  237.  
  238. ;##########################################################################################
  239. ;##########################################################################################
  240. ;##########################################################################################
  241.  
  242. ;sleep for the set amount of milliseconds
  243. proc sleep
  244.     ;AX = time to halt for (ms)
  245.     push AX
  246.     push BX
  247.     push CX
  248.     push DX
  249.    
  250.     mov BX, 1000
  251.     mul BX
  252.     mov CX, AX
  253.     xchg CX, DX
  254.     mov AH, 86h
  255.     int 15h
  256.    
  257.     pop DX
  258.     pop CX
  259.     pop BX
  260.     pop AX
  261.    
  262.     ret
  263. endp
  264.  
  265. ;##########################################################################################
  266. ;##########################################################################################
  267. ;##########################################################################################
  268.  
  269. ;print AL in hexadecimal
  270. proc printHex
  271.     ;AL = byte to write
  272.     push AX
  273.     push DX
  274.    
  275.     mov DH, AL
  276.     mov DL, AL
  277.    
  278.     mov AH, 02h ;print character code
  279.     shr DL, 4 ;high nibble
  280.     and DH, 00001111b ;low nibble
  281.    
  282.     ;write the actual byte
  283.     cmp DL, 9
  284.     ja phln1 ;print hex letter nibble
  285.         add DL, 30h
  286.         int 21h
  287.     jmp phln
  288.     phln1:
  289.         add DL, 37h
  290.         int 21h
  291.     phln: ;print hex low nibble
  292.    
  293.     mov DL, DH
  294.     cmp DL, 9
  295.     ja phln2 ;print hex letter nibble
  296.         add DL, 30h
  297.         int 21h
  298.        
  299.         pop DX
  300.         pop AX
  301.        
  302.         ret
  303.     phln2:
  304.         add DL, 37h
  305.         int 21h
  306.        
  307.         pop DX
  308.         pop AX
  309.        
  310.         ret
  311. endp
  312.  
  313. ;##########################################################################################
  314. ;##########################################################################################
  315. ;##########################################################################################
  316.  
  317. ;generate the expanded key from rijndaelKey
  318. proc rijndaelGenerateKey
  319.     push AX
  320.     push BX
  321.     push CX
  322.     push DX
  323.    
  324.     mov DH, 1
  325.    
  326.     mov CX, 4
  327.     xor BX, BX
  328.    
  329.     ;rotate the last column
  330.     rgk1:
  331.         mov DL, [rijndaelKey + BX + 12]
  332.         add BX, 3
  333.         and BX, 11b
  334.         mov [rijndaelTempColumn + BX], DL
  335.         add BX, 2
  336.         and BX, 11b
  337.     loop rgk1
  338.    
  339.     mov CX, 4
  340.     xor AL, AL
  341.     xor BX, BX
  342.    
  343.     ;apply the sbox to the rotated column
  344.     rgk2:
  345.         mov BL, AL
  346.         mov BL, [rijndaelTempColumn + BX]
  347.         mov BL, [rijndaelSbox + BX]
  348.         mov AH, BL
  349.         mov BL, AL
  350.         mov [rijndaelTempColumn + BX], AH
  351.         inc AL
  352.     loop rgk2
  353.    
  354.     ;xor the first item of the column
  355.     ;with the round constant
  356.     xor [rijndaelTempColumn], DH
  357.     shl DH, 1
  358.     jnc rgksr1
  359.         xor DH, rijndaelReducingPolynomial
  360.     rgksr1: ;rijndael generate key skip reduction
  361.    
  362.     mov CX, 4
  363.     xor BX, BX
  364.    
  365.     ;xor the column with the first column
  366.     ;of the key and set it as
  367.     ;the first column of the first round key
  368.     rgk3:
  369.         mov DL, [rijndaelKey + BX]
  370.         xor DL, [rijndaelTempColumn + BX]
  371.         mov [rijndaelExpandedKey + BX], DL
  372.         inc BL
  373.     loop rgk3
  374.    
  375.     mov CX, 12
  376.     mov BX, 4
  377.    
  378.     ;generate the rest of the first round key
  379.     rgk4:
  380.         mov DL, [rijndaelKey + BX]
  381.         sub BX, 4
  382.         xor DL, [rijndaelExpandedKey + bx]
  383.         add BX, 4
  384.         mov [rijndaelExpandedKey + BX], DL
  385.         inc BX
  386.     loop rgk4
  387.    
  388.     mov CX, 9
  389.     xor AL, AL
  390.    
  391.     ;now, generate the other 9 round keys using the same method
  392.     rgk5:
  393.         push CX
  394.        
  395.         mov CX, 4
  396.         xor BX, BX
  397.        
  398.         ;rotate the last column of the previous round key
  399.         rgk6:
  400.             add BL, AL
  401.             mov DL, [rijndaelExpandedKey + BX + 12]
  402.             sub BL, AL
  403.             add BX, 3
  404.             and BX, 11b
  405.             mov [rijndaelTempColumn + BX], DL
  406.             add BX, 2
  407.             and BX, 11b
  408.         loop rgk6
  409.        
  410.         mov CX, 4
  411.         xor AH, AH
  412.         xor DL, DL
  413.         xor BX, BX
  414.        
  415.         ;apply the sbox to the rotated column  
  416.         rgk7:
  417.             mov BL, AH
  418.             mov BL, [rijndaelTempColumn + BX]
  419.             mov BL, [rijndaelSbox + BX]
  420.             mov DL, BL
  421.             mov BL, AH
  422.             mov [rijndaelTempColumn + BX], DL
  423.             inc AH
  424.         loop rgk7
  425.        
  426.         ;the loop is too big so i have to put an "island"
  427.         ;to jump to to make the jump size stay in range
  428.         jmp fsrj2
  429.         fsrj1: ;fuck short relative jumps
  430.         jmp rgk5
  431.         fsrj2: ;fuck short relative jumps
  432.        
  433.         ;xor the first item of the column
  434.         ;with the round constant
  435.         xor [rijndaelTempColumn], DH
  436.         shl DH, 1
  437.         jnc rgksr2
  438.             xor DH, rijndaelReducingPolynomial
  439.         rgksr2: ;rijndael generate key skip reduction
  440.        
  441.         mov CX, 4
  442.         xor BX, BX
  443.        
  444.         ;xor the column with the first column
  445.         ;of the last round key and set it as
  446.         ;the first column of the next round key
  447.         rgk8:
  448.             add BL, AL
  449.             mov DL, [rijndaelExpandedKey + BX]
  450.             sub BL, AL
  451.             xor DL, [rijndaelTempColumn + BX]
  452.             add BL, AL
  453.             mov [rijndaelExpandedKey + BX + 16], DL
  454.             sub BL, AL
  455.             inc BX
  456.         loop rgk8
  457.        
  458.         mov CX, 12
  459.         xor BH, BH
  460.         mov BL, AL
  461.        
  462.         ;generate the rest of the next key
  463.         rgk9:
  464.             mov DL, [rijndaelExpandedKey + BX + 16]
  465.             xor DL, [rijndaelExpandedKey + BX + 4]
  466.             mov [rijndaelExpandedKey + BX + 20], DL
  467.             inc BX
  468.         loop rgk9
  469.        
  470.         pop CX
  471.         add AL, 16
  472.     loop fsrj1
  473.    
  474.     pop DX
  475.     pop CX
  476.     pop BX
  477.     pop AX
  478.    
  479.     ret
  480. endp
  481.  
  482. ;##########################################################################################
  483. ;##########################################################################################
  484. ;##########################################################################################
  485.  
  486. ;encrypts the rijndaelState block with the rijndaelKey
  487. proc rijndaelEncryptBlock
  488.     push AX
  489.     push BX
  490.     push CX
  491.     push DX
  492.    
  493.     xor BX, BX
  494.     mov CX, 16
  495.    
  496.     ;xor the state with the key
  497.     reb1: ;rijndael encrypt block
  498.         mov DL, [rijndaelKey + BX]
  499.         xor [rijndaelState + BX], DL
  500.         inc BX
  501.     loop reb1
  502.    
  503.     mov CX, 9
  504.     xor AX, AX
  505.    
  506.     ;repeat 9 times
  507.     reb2:
  508.         push CX
  509.        
  510.         mov CX, 16
  511.         xor AL, AL
  512.        
  513.         ;apply the sbox to each byte of the state
  514.         reb3:
  515.             mov BL, AL
  516.             mov BL, [rijndaelState + BX]
  517.             mov BL, [rijndaelSbox + BX]
  518.             mov DL, BL
  519.             mov BL, AL
  520.             mov [rijndaelState + BX], DL
  521.             inc AL
  522.         loop reb3
  523.        
  524.         xor BX, BX
  525.         xor DX, DX
  526.         mov CX, 4
  527.        
  528.         ;shift rows
  529.         reb4:
  530.             mov BL, DH
  531.             xor AL, AL
  532.            
  533.             ;copy a row to the tempColumn array
  534.             reb5:
  535.                 mov DL, [rijndaelState + BX]
  536.                 xchg BL, AL
  537.                 mov [rijndaelTempColumn + BX], DL
  538.                 xchg BL, AL
  539.                 add BL, 4
  540.                 inc AL
  541.             cmp AL, 4
  542.             jne reb5
  543.            
  544.             mov AL, DH
  545.             mov BL, DH
  546.            
  547.             ;copy the tempColumn to its corresponding row after being rotated
  548.             reb6:
  549.                 mov DL, [rijndaelTempColumn + BX]
  550.                 xchg BL, AL
  551.                 mov [rijndaelState + BX], DL
  552.                 xchg BL, AL
  553.                 add AL, 4
  554.                 inc BL
  555.                 and BL, 11b ;BL = Bl % 4
  556.             cmp AL, 16
  557.             jb reb6
  558.         inc DH
  559.         loop reb4
  560.        
  561.         xor CL, CL
  562.         xor BX, BX
  563.        
  564.         ;mix columns
  565.         reb7:
  566.             xor CH, CH
  567.             mov BL, CL
  568.            
  569.             ;copy the column to the tempColumn array
  570.             reb8:
  571.                 mov DL, [rijndaelState + BX]
  572.                 sub BL, CL
  573.                 mov [rijndaelTempColumn + BX], DL
  574.                 add BL, CL
  575.                 inc BL
  576.             inc CH
  577.             cmp CH, 4
  578.             jb reb8
  579.            
  580.             xor CH, CH
  581.            
  582.             ;copy the tempColumn multiplied by the matrix back to the state matrix
  583.             mov BL, CH
  584.            
  585.             ;multiply column by (3x^3 + x^2 + x^1 + 2) mod (x^4 + 1)
  586.             reb9:
  587.                 xor DL, DL
  588.                
  589.                 mov DH, [rijndaelTempColumn + BX]
  590.                 shl DH, 1
  591.                 jnc reb10
  592.                     xor DH, rijndaelReducingPolynomial
  593.                 reb10:
  594.                 xor DL, DH
  595.                 inc BL
  596.                 and BL, 11b ;BL = BL % 4
  597.                
  598.                 mov DH, [rijndaelTempColumn + BX]
  599.                 mov AL, DH
  600.                 shl DH, 1
  601.                 pushf
  602.                 xor DH, AL
  603.                 popf
  604.                 jnc reb11
  605.                     xor DH, rijndaelReducingPolynomial
  606.                 reb11:
  607.                 xor DL, DH
  608.                 inc BL
  609.                 and BL, 11b ;BL = BL % 4
  610.                
  611.                 xor DL, [rijndaelTempColumn + BX]
  612.                 inc BL
  613.                 and BL, 11b ;BL = BL % 4
  614.                
  615.                 xor DL, [rijndaelTempColumn + BX]
  616.                 inc BL
  617.                 and BL, 11b ;BL = BL % 4
  618.                
  619.                 ;put the result in the state matrix
  620.                 add BL, CL
  621.                 mov [rijndaelState + BX], DL
  622.                 sub BL, CL
  623.                 inc BL
  624.                 and BL, 11b ;BL = BL % 4
  625.             cmp BL, 0 ;after one iteration BL increments by one, and starts at CH.
  626.             jne reb9 ;so, after the last iteration it wraps back to CH
  627.         add CL, 4
  628.         cmp CL, 16
  629.         jb reb7
  630.        
  631.         ;the loop is too big so i have to put an "island"
  632.         ;to jump to to make the jump size stay in range
  633.         jmp fsrj4
  634.         fsrj3: ;fuck short relative jumps
  635.         jmp reb2
  636.         fsrj4: ;fuck short relative jumps
  637.        
  638.         mov CX, 16
  639.         mov AL, AH
  640.         xor BX, BX
  641.        
  642.         ;add the round key
  643.         reb12:
  644.             mov DL, [rijndaelState + BX]
  645.             xchg BL, AL
  646.             xor DL, [rijndaelExpandedKey + BX]
  647.             xchg BL, AL
  648.             mov [rijndaelState + BX], DL
  649.             inc AL
  650.             inc BL
  651.         loop reb12
  652.        
  653.         add AH, 16
  654.         pop CX
  655.     loop fsrj3
  656.    
  657.     ;last round, mix columns stage is omitted
  658.     mov CX, 16
  659.     xor AL, AL
  660.    
  661.     ;apply the sbox to each byte of the state
  662.     reb13:
  663.         mov BL, AL
  664.         mov BL, [rijndaelState + BX]
  665.         mov BL, [rijndaelSbox + BX]
  666.         mov DL, BL
  667.         mov BL, AL
  668.         mov [rijndaelState + BX], DL
  669.         inc AL
  670.     loop reb13
  671.    
  672.     xor BX, BX
  673.     xor DX, DX
  674.     mov CX, 4
  675.    
  676.     ;shift rows
  677.     reb14:
  678.         mov BL, DH
  679.         xor AL, AL
  680.        
  681.         ;copy a row to the tempColumn array
  682.         reb15:
  683.             mov DL, [rijndaelState + BX]
  684.             xchg BL, AL
  685.             mov [rijndaelTempColumn + BX], DL
  686.             xchg BL, AL
  687.             add BL, 4
  688.             inc AL
  689.         cmp AL, 4
  690.         jne reb15
  691.        
  692.         mov AL, DH
  693.         mov BL, DH
  694.        
  695.         ;copy the tempColumn to its corresponding row after being rotated
  696.         reb16:
  697.             mov DL, [rijndaelTempColumn + BX]
  698.             xchg BL, AL
  699.             mov [rijndaelState + BX], DL
  700.             xchg BL, AL
  701.             add AL, 4
  702.             inc BL
  703.             and BL, 11b ;BL = Bl % 4
  704.         cmp AL, 16
  705.         jb reb16
  706.     inc DH
  707.     loop reb14
  708.    
  709.     mov CX, 16
  710.     mov AL, AH
  711.     xor BX, BX
  712.    
  713.     ;add the round key
  714.     reb17:
  715.         mov DL, [rijndaelState + BX]
  716.         xchg BL, AL
  717.         xor DL, [rijndaelExpandedKey + BX]
  718.         xchg BL, AL
  719.         mov [rijndaelState + BX], DL
  720.         inc AL
  721.         inc BL
  722.     loop reb17
  723.        
  724.     pop DX
  725.     pop CX
  726.     pop BX
  727.     pop AX
  728.    
  729.     ret
  730. endp
  731.  
  732. ;##########################################################################################
  733. ;##########################################################################################
  734. ;##########################################################################################
  735.  
  736. ;preform the keccak sponge function on the keccakState memory block
  737. proc keccakRound
  738.     push AX
  739.     push BX
  740.     push CX
  741.     push DX
  742.    
  743.     xor CX, CX
  744.     xor BX, BX
  745.    
  746.     kr1:
  747.         push CX
  748.        
  749.         xor CX, CX
  750.        
  751.         ;theta
  752.         kr2:
  753.             xor DX, DX
  754.             xor AX, AX
  755.             mov BX, CX
  756.            
  757.             ;DX = high word, AX = low word
  758.             kr3:
  759.                 xor DX, [word ptr keccakState + BX + 0]
  760.                 xor AX, [word ptr keccakState + BX + 2]
  761.             add BX, 20
  762.             cmp BX, 100
  763.             jb kr3
  764.            
  765.             mov BX, CX
  766.            
  767.             mov [word ptr keccakTempColumn + BX + 0], DX
  768.             mov [word ptr keccakTempColumn + BX + 2], AX
  769.         add CX, 4
  770.         cmp CX, 20
  771.         jb kr2
  772.        
  773.         xor CX, CX
  774.        
  775.         kr4:
  776.             ;t = temp[(i + 4) % 5] ^ ROTL32(temp[(i + 1) % 5], 1)
  777.             ;DX:AX = temp[(i + 1) % 5]
  778.             mov BL, CL
  779.             add BL, 4
  780.             cmp BL, 20
  781.             jb kr5
  782.                 sub BL, 20
  783.             kr5:
  784.            
  785.             mov DX, [word ptr keccakTempColumn + BX + 0]
  786.             mov AX, [word ptr keccakTempColumn + BX + 2]
  787.            
  788.             ;rotate DX:AX one bit to the left
  789.             shl AX, 1
  790.             pushf
  791.             shl DX, 1
  792.             jnc kr6
  793.                 or AX, 1
  794.             kr6:
  795.             popf
  796.             jnc kr7
  797.                 or DX, 1
  798.             kr7:
  799.            
  800.             ;DX:AX ^= temp[(i + 4) % 5]
  801.             mov BL, CL
  802.             add BL, 16
  803.             cmp BL, 20
  804.             jb kr8
  805.                 sub BL, 20
  806.             kr8:
  807.            
  808.             xor DX, [word ptr keccakTempColumn + BX + 0]
  809.             xor AX, [word ptr keccakTempColumn + BX + 2]
  810.            
  811.             mov BL, CL
  812.            
  813.             ;for (j = 0; j < 25; j += 5){st[j + i] ^= t;}
  814.             kr9:
  815.                 xor [word ptr keccakState + BX + 0], DX
  816.                 xor [word ptr keccakState + BX + 2], AX
  817.             add BL, 20
  818.             cmp BL, 100
  819.             jb kr9
  820.         add CL, 4
  821.         cmp CL, 20
  822.         jb kr4
  823.        
  824.         xor BX, BX
  825.        
  826.         ;kkk:
  827.         ;   mov [word ptr keccakState + BX], 0a5a5h
  828.         ;add BX, 4
  829.         ;cmp BX, 100
  830.         ;jb kkk
  831.        
  832.         xor CX, CX
  833.         mov DX, [word ptr keccakState + 4]
  834.         mov AX, [word ptr keccakState + 6]
  835.        
  836.         ;rho + pi
  837.         kr10:
  838.             ;j = keccakTransformationTable[i]
  839.             mov BL, CL
  840.             mov BL, [keccakTransformationTable + BX]
  841.            
  842.             push AX
  843.             push DX
  844.            
  845.             ;temp[0] = st[j]
  846.             mov DX, [word ptr keccakState + BX + 0]
  847.             mov AX, [word ptr keccakState + BX + 2]
  848.             mov [word ptr keccakTempColumn + 0], DX
  849.             mov [word ptr keccakTempColumn + 2], AX
  850.            
  851.             pop DX
  852.             pop AX
  853.            
  854.             mov BL, CL
  855.             mov CH, [keccakRotateTable + BX]
  856.            
  857.             ;st[j] = ROTL32(t, keccakRotateTable[i])
  858.             kr11:
  859.                 shl AX, 1
  860.                 pushf
  861.                 shl DX, 1
  862.                 jnc kr12
  863.                     or AX, 1
  864.                 kr12:
  865.                 popf
  866.                 jnc kr13
  867.                     or DX, 1
  868.                 kr13:
  869.             dec CH
  870.             cmp CH, 0
  871.             jne kr11
  872.            
  873.             mov BL, CL
  874.             mov BL, [keccakTransformationTable + BX]
  875.            
  876.             mov [word ptr keccakState + BX + 0], DX
  877.             mov [word ptr keccakState + BX + 2], AX
  878.            
  879.             ;t = temp[0];
  880.             mov DX, [word ptr keccakTempColumn + 0]
  881.             mov AX, [word ptr keccakTempColumn + 2]
  882.         inc CL
  883.         cmp CL, 24
  884.         jb kr10
  885.        
  886.         xor CX, CX
  887.            
  888.         ;chi
  889.         kr14:
  890.             xor CH, CH
  891.             mov BL, CL
  892.            
  893.             ;for(i = 0; i < 5; i++){tempColumn[i] = st[j + i];}
  894.             kr15:
  895.                 mov DX, [word ptr keccakState + BX + 0]
  896.                 mov AX, [word ptr keccakState + BX + 2]
  897.                 xchg CH, BL
  898.                 mov [word ptr keccakTempColumn + BX + 0], DX
  899.                 mov [word ptr keccakTempColumn + BX + 2], AX
  900.                 xchg CH, BL
  901.             add BL, 4
  902.             add CH, 4
  903.             cmp CH, 20
  904.             jb kr15
  905.            
  906.             ;for(i = 0; i < 5; i++){st[j + i] ^= (~tempColumn[(i + 1) % 5]) & tempColumn[(i + 2) % 5];}
  907.             kr16:
  908.                 mov BL, CH
  909.                
  910.                 add BL, 4
  911.                 cmp BL, 20
  912.                 jb kr17
  913.                     sub BL, 20
  914.                 kr17:
  915.                
  916.                 mov DX, [word ptr keccakTempColumn + BX + 0]
  917.                 mov AX, [word ptr keccakTempColumn + BX + 2]
  918.                 not DX
  919.                 not AX
  920.                
  921.                 add BL, 4
  922.                 cmp BL, 20
  923.                 jb kr18
  924.                     sub BL, 20
  925.                 kr18:
  926.                
  927.                 and DX, [word ptr keccakTempColumn + BX + 0]
  928.                 and AX, [word ptr keccakTempColumn + BX + 2]
  929.                
  930.                 mov BL, CH
  931.                 add BL, CL
  932.                
  933.                 xor [word ptr keccakState + BX + 0], DX
  934.                 xor [word ptr keccakState + BX + 2], AX
  935.             add CH, 4
  936.             cmp CH, 20
  937.             jb kr16
  938.         add CL, 20
  939.         cmp CL, 100
  940.         jb kr14
  941.        
  942.         pop CX
  943.        
  944.         mov BX, CX
  945.         shl BX, 2
  946.        
  947.         mov DX, [word ptr keccakRoundConstants + BX + 0]
  948.         mov AX, [word ptr keccakRoundConstants + BX + 2]
  949.         xor [word ptr keccakState + 0], DX
  950.         xor [word ptr keccakState + 2], AX
  951.     inc CX
  952.     cmp CX, 22
  953.     jae kr19
  954.         jmp kr1
  955.     kr19:
  956.    
  957.     pop DX
  958.     pop CX
  959.     pop BX
  960.     pop AX
  961.    
  962.     ret
  963. endp
  964.  
  965. ;##########################################################################################
  966. ;##########################################################################################
  967. ;##########################################################################################
  968.  
  969. ;gets the keccak hash of DS:BX of length AX and puts it in keccakResult
  970. proc getKeccakHash
  971.     push AX
  972.     push BX
  973.     push CX
  974.     push DX
  975.    
  976.     ;clear the state
  977.     mov CX, 100
  978.     push BX
  979.     xor BX, BX
  980.     xor DL, DL
  981.    
  982.     gkh1:
  983.         mov [byte ptr keccakState + BX], DL
  984.         inc BX
  985.     loop gkh1
  986.    
  987.     pop BX
  988.    
  989.     ;absorb all blocks
  990.     gkh2:
  991.     cmp AX, keccakRate
  992.     jb gkh4
  993.         xor CX, CX
  994.        
  995.         gkh3:
  996.             mov DL, [BX]
  997.             xchg BX, CX
  998.             xor [byte ptr keccakState + BX], DL
  999.             xchg BX, CX
  1000.             inc BX
  1001.         inc CX
  1002.         cmp CX, keccakRate
  1003.         jb gkh3
  1004.        
  1005.         sub AX, keccakRate
  1006.        
  1007.         call keccakRound
  1008.     jmp gkh2
  1009.     gkh4:
  1010.    
  1011.     ;if there is no last block skip padding
  1012.     cmp AX, 0
  1013.     je gkh5
  1014.         xor CX, CX
  1015.         mov DH, keccakRate
  1016.         sub DH, AL
  1017.        
  1018.         ;last block and padding
  1019.         gkh6:
  1020.             mov DL, [BX]
  1021.             xchg BX, CX
  1022.             xor [byte ptr keccakState + BX], DL
  1023.             xchg BX, CX
  1024.             inc BX
  1025.             inc CX
  1026.         dec AX
  1027.         cmp AX, 0
  1028.         ja gkh6
  1029.        
  1030.         mov BX, CX
  1031.         xor CX, CX
  1032.         mov CL, DH
  1033.        
  1034.         mov DH, 01h
  1035.         xor [byte ptr keccakState + BX + 1], DH
  1036.         add BX, CX
  1037.         mov DH, 80h
  1038.         xor [byte ptr keccakState + BX], DH
  1039.        
  1040.         call keccakRound
  1041.     gkh5:
  1042.    
  1043.     mov CX, 32
  1044.     xor BX, BX
  1045.    
  1046.     ;copy to keccakResult
  1047.     gkh7:
  1048.         mov DL, [byte ptr keccakState + BX]
  1049.         mov [keccakResult + BX], DL
  1050.         inc BX
  1051.     loop gkh7
  1052.     ;now, the result of the hash is stored in keccakResult
  1053.    
  1054.     pop DX
  1055.     pop CX
  1056.     pop BX
  1057.     pop AX
  1058.    
  1059.     ret
  1060. endp
  1061.  
  1062. ;##########################################################################################
  1063. ;##########################################################################################
  1064. ;##########################################################################################
  1065.  
  1066. ;initialize all the lookup tables and constant AES and SHA3 need
  1067. proc initializeTables
  1068.     push AX
  1069.     push BX
  1070.     push CX
  1071.     push DX
  1072.    
  1073.     ;---- initialize Galois field exponent and log tables ----
  1074.     mov [GLT_EXP], 1
  1075.     mov AX, 1
  1076.     mov CX, 0FFh
  1077.     xor BX, BX
  1078.    
  1079.     it1:
  1080.         inc BL
  1081.        
  1082.         ; GF multiply by 3:
  1083.         mov DL, AL
  1084.         shl AL, 1
  1085.         pushf
  1086.         xor AL, DL
  1087.         popf
  1088.         jnc it2
  1089.             xor AL, rijndaelReducingPolynomial
  1090.         it2:
  1091.        
  1092.         ; gf_exp[i] = x;
  1093.         ; gf_log[x] = i;
  1094.         mov [GLT_EXP + BX], AL
  1095.         xchg AL, BL
  1096.         mov [GLT_LOG + BX], AL
  1097.         xchg AL, BL
  1098.     loop it1
  1099.    
  1100.     ; gf_log[gf_exp[0]] = 0;
  1101.     mov BL, [GLT_EXP]
  1102.     mov [GLT_LOG + BX], 0
  1103.    
  1104.     ;---- generate AES' S-box ----
  1105.    
  1106.     ;sbox[i] = i^-1; (in GF(2^8)
  1107.     ;sbox[0] = 0;
  1108.    
  1109.     mov CX, 0FFh
  1110.     xor AX, AX
  1111.     mov [rijndaelSbox], 0
  1112.    
  1113.     ;prepare sub box: multiplicative inverse
  1114.     it3:
  1115.     inc AX
  1116.    
  1117.     ;x^-1 = g^-log(x)
  1118.     mov BX, AX
  1119.     mov BL, [GLT_LOG + BX]
  1120.     not BL ;bl = -bl (mod 255)
  1121.     mov BL, [GLT_EXP + BX]
  1122.     mov DL, BL
  1123.     mov BX, AX
  1124.     mov [rijndaelSbox + BX], DL
  1125.    
  1126.     loop it3
  1127.    
  1128.     ;sbox[i] = 0x63 ^
  1129.     ;          sbox[i] ^
  1130.     ;          ROTL8(sbox[i], 1) ^
  1131.     ;          ROTL8(sbox[i], 2) ^
  1132.     ;          ROTL8(sbox[i], 3) ^
  1133.     ;          ROTL8(sbox[i], 4);
  1134.    
  1135.     mov CX, 100h
  1136.     xor BX, BX
  1137.    
  1138.     ;prepare sub box: affine transformation
  1139.     it4:
  1140.         mov AL, [rijndaelSbox + BX]
  1141.         mov DL, AL
  1142.         rol DL, 1
  1143.         xor AL, DL
  1144.         rol DL, 1
  1145.         xor AL, DL
  1146.         rol DL, 1
  1147.         xor AL, DL
  1148.         rol DL, 1
  1149.         xor AL, DL
  1150.         xor AL, 63h
  1151.         mov [rijndaelSbox + BX], AL
  1152.        
  1153.         inc BX
  1154.     loop it4
  1155.    
  1156.     ;---- prepare SHA3 constants ----
  1157.     ;prepare shift values (pretty much triangular numbers modulo 32)
  1158.     mov CX, 24
  1159.     xor AX, AX
  1160.     xor DX, DX
  1161.     xor BX, BX
  1162.    
  1163.     ;prepare keccak shift values
  1164.     it5:
  1165.         inc DL
  1166.         add AL, DL
  1167.         and AL, 11111b ;mod 32
  1168.         mov [keccakRotateTable + BX], AL
  1169.         inc BX
  1170.     loop it5
  1171.    
  1172.     ;prepare PI transformation values
  1173.     ;DL, DH: current X and Y values of the vector
  1174.     mov DL, 1
  1175.     mov DH, 0
  1176.     mov CX, 24
  1177.     xor BX, BX
  1178.    
  1179.     ; repeat matrix multiplication:
  1180.     ; |DL| = |0 1| * |DL|
  1181.     ; |DH|   |2 3|   |DH|
  1182.    
  1183.     ;prepare keccak transformation values
  1184.     it6:
  1185.         ;AL += 2 * DL
  1186.         xor AL, AL
  1187.         shl DL, 1
  1188.         add AL, DL
  1189.        
  1190.         ;AL += 3 * DH
  1191.         mov AH, DH
  1192.         add AL, AH
  1193.         shl AH, 1
  1194.         add AL, AH
  1195.        
  1196.         ;AL = AL % 5
  1197.         mov DL, 5
  1198.         xor AH, AH
  1199.         div DL
  1200.        
  1201.         ;finally, reorder DL and DH to get the transformed vector
  1202.         mov DL, DH
  1203.         mov DH, AH
  1204.        
  1205.         ;AL = DL + 5 * DH (array flattening)
  1206.         mov AX, DX
  1207.         add AL, AH
  1208.         shl AH, 2
  1209.         add AL, AH
  1210.        
  1211.         shl AL, 2 ;multiply by 4 because each value takes 4 bytes
  1212.         mov [keccakTransformationTable + BX], AL
  1213.         inc BX
  1214.     loop it6
  1215.    
  1216.     xor CX, CX
  1217.     mov BH, 1
  1218.    
  1219.     ;prepare keccak round constants
  1220.     it7:
  1221.         xor CL, CL
  1222.         xor AX, AX
  1223.         xor DX, DX
  1224.        
  1225.         it8:
  1226.             test BH, 1
  1227.             jz it9
  1228.                 push BX
  1229.                
  1230.                 mov BX, 1
  1231.                
  1232.                 cmp CL, 16
  1233.                 jae it10
  1234.                     shl BX, CL
  1235.                     or AX, BX
  1236.                     jmp it11
  1237.                 it10:
  1238.                     sub CX, 16
  1239.                     shl BX, CL
  1240.                     or DX, BX
  1241.                     add CX, 16
  1242.                 it11:
  1243.                
  1244.                 pop BX
  1245.             it9:
  1246.            
  1247.             shl BH, 1
  1248.             jnc it12
  1249.                 xor BH, 01110001b
  1250.             it12:
  1251.         shl CL, 1
  1252.         inc CL
  1253.         cmp CL, 32
  1254.         jb it8
  1255.        
  1256.         shl BH, 1
  1257.         jnc it13
  1258.             xor BH, 01110001b
  1259.         it13:
  1260.         push BX
  1261.        
  1262.         xor BX, BX
  1263.         mov BL, CH
  1264.         mov [word ptr keccakRoundConstants + BX + 0], AX
  1265.         mov [word ptr keccakRoundConstants + BX + 2], DX
  1266.        
  1267.         pop BX
  1268.     add CH, 4
  1269.     cmp CH, 88
  1270.     jb it7
  1271.    
  1272.     pop DX
  1273.     pop CX
  1274.     pop BX
  1275.     pop AX
  1276.    
  1277.     ret
  1278. endp
  1279.  
  1280. ;remove some bytes from the end of the file
  1281. proc truncateFile
  1282.     ;BX = file handle
  1283.     ;DX = number of bytes to remove
  1284.     push AX
  1285.     push BX
  1286.     push CX
  1287.     push DX
  1288.    
  1289.     mov AL, 2
  1290.     mov CX, 0FFFFh
  1291.     not DX
  1292.     mov AH, 42h
  1293.     int 21h
  1294.    
  1295.     mov CX, 0
  1296.     mov AH, 40h
  1297.     int 21h
  1298.    
  1299.     pop DX
  1300.     pop CX
  1301.     pop BX
  1302.     pop AX
  1303.    
  1304.     ret
  1305. endp
  1306.  
  1307. ;##########################################################################################
  1308. ;##########################################################################################
  1309. ;##########################################################################################
  1310.  
  1311. ;copy a block of memory to another location
  1312. proc memcpy
  1313.     ;AX = source address
  1314.     ;BX = destination address
  1315.     ;CX = count
  1316.    
  1317.     push AX
  1318.     push BX
  1319.     push CX
  1320.     push DX
  1321.    
  1322.     mcl:
  1323.     xchg AX, BX
  1324.     mov DL, [BX]
  1325.     inc BX
  1326.     xchg AX, BX
  1327.     mov [BX], DL
  1328.     inc BX
  1329.     loop mcl
  1330.    
  1331.     pop DX
  1332.     pop CX
  1333.     pop BX
  1334.     pop AX
  1335.    
  1336.     ret
  1337. endp
  1338.  
  1339. ;---wait for a key press snippet---
  1340. ;   mov ah, 0
  1341. ;   int 16h ;wait for a keypress
  1342.  
  1343.  
  1344. ;################################################################################
  1345. ;################################################################################
  1346. ;################################################################################
  1347. ;################################  PROGRAM START  #############w##################
  1348. ;################################################################################
  1349. ;################################################################################
  1350. ;################################################################################
  1351.  
  1352. start:
  1353.     mov AX, @data
  1354.     mov DS, AX
  1355.     ;code  
  1356.    
  1357.     ;######################### SETUP #########################
  1358.     ;hide cursor
  1359.     mov CX, 2607h
  1360.     call setCursorShape
  1361.    
  1362.     call initializeTables
  1363.    
  1364.     getChoice:
  1365.    
  1366.     mov BH, mainScreenColor
  1367.     call clearScreen
  1368.        
  1369.     mov DX, offset startMsg
  1370.     call printString
  1371.    
  1372.     ;############################## INPUT CHOICE #############################
  1373.    
  1374.     ;---- read untill a valid choice is read ----
  1375.    
  1376.     GFVI:
  1377.     ;read char
  1378.     mov AH, 00h
  1379.     int 16h
  1380.    
  1381.     sub AL, 31h
  1382.     cmp AL, choiceCount
  1383.     jae GFVI
  1384.    
  1385.     add AL, 31h
  1386.     mov BL, selectionColor
  1387.     call writeChar
  1388.    
  1389.     ;---- read untill enter is pressed ----
  1390.     GI:
  1391.     mov DL, AL
  1392.     GII:
  1393.    
  1394.     ;read char
  1395.     mov AH, 00h
  1396.     int 16h
  1397.    
  1398.     cmp AL, 0Dh ;enter key
  1399.     je EIL
  1400.    
  1401.     sub AL, 31h
  1402.     cmp AL, choiceCount
  1403.     jae GII
  1404.    
  1405.     add AL, 31h
  1406.     mov BL, selectionColor
  1407.     call writeChar
  1408.    
  1409.     jmp GI
  1410.     EIL: ;exit input loop
  1411.    
  1412.     ;---- finally print the choice in a different color/style ----
  1413.    
  1414.     mov CX, 2607h
  1415.     call setCursorShape
  1416.    
  1417.     mov AL, DL
  1418.     mov BL, finalSelectionColor
  1419.     call writeChar
  1420.    
  1421.     sub DL, 31h ;convert DL from ASCII to 0 indexed
  1422.    
  1423.     ;;pause for a second
  1424.     ;mov AX, 500
  1425.     ;call sleep
  1426.    
  1427.     ;####################### DO SOMETHING ACCORDING TO INPUT ################################
  1428.     ;---- switch the choice ----
  1429.    
  1430.     cmp DL, 0
  1431.     je sc0
  1432.     cmp DL, 1
  1433.     je sc1
  1434.     cmp DL, 2
  1435.     je sc2
  1436.    
  1437.     sc0:
  1438.     jmp fileEncrypt
  1439.     sc1:
  1440.     jmp fileDecrypt
  1441.     sc2:
  1442.     jmp exitSequence
  1443.    
  1444.     ;---- if the choice is invalid, display error and exit the program
  1445.     mov DX, offset invalidChoiceMsg
  1446.     call printString
  1447.     jmp exit
  1448.    
  1449.     ;#####################################################################################
  1450.     ;################################# FILE ENCRYPTION ###################################
  1451.     ;#####################################################################################
  1452.     fileEncrypt:
  1453.         mov DX, offset newlineMsg
  1454.         call printString
  1455.        
  1456.         mov DX, offset pathRequestMsg
  1457.         call printString
  1458.        
  1459.         mov DX, offset encryptedMsg
  1460.         call printString
  1461.        
  1462.         mov DX, offset newlineMsg
  1463.         call printString
  1464.        
  1465.         ;#################### get the file path ####################
  1466.         mov AH, 0Ah
  1467.         mov DX, offset pathSrc
  1468.         int 21h
  1469.        
  1470.         ;replace the CR EOS with null (ASCIIZ)
  1471.         xor BX, BX
  1472.         mov BL, [pathSrc + 1]
  1473.         mov [pathSrc + BX + 2], 0
  1474.        
  1475.         ;---- open the file ----
  1476.         mov AH, 3Dh
  1477.         mov AL, 0
  1478.         mov DX, offset pathSrc
  1479.         add DX, 2
  1480.         int 21h
  1481.        
  1482.         jnc fileSucceed1
  1483.             call printDosError
  1484.             mov AH, 0
  1485.             int 16h ;wait for a keypress
  1486.             jmp getChoice
  1487.         fileSucceed1:
  1488.        
  1489.         mov [srcHandle], AX
  1490.        
  1491.         ;#################### get the file destination path ####################
  1492.         mov DX, offset newlineMsg
  1493.         call printString
  1494.        
  1495.         mov DX, offset pathRequestEncMsg
  1496.         call printString
  1497.        
  1498.         mov DX, offset newlineMsg
  1499.         call printString
  1500.        
  1501.         mov AH, 0Ah
  1502.         mov DX, offset pathDst
  1503.         int 21h
  1504.        
  1505.         ;replace the CR EOS with null (ASCIIZ)
  1506.         xor BX, BX
  1507.         mov BL, [pathDst + 1]
  1508.         mov [pathDst + BX + 2], 0
  1509.        
  1510.         ;---- create the file ----
  1511.         mov AH, 3Ch
  1512.         mov AL, 1
  1513.         mov DX, offset pathDst
  1514.         add DX, 2
  1515.         xor CX, CX
  1516.         int 21h
  1517.        
  1518.         jnc fileSucceed2
  1519.             call printDosError
  1520.             mov AH, 0
  1521.             int 16h ;wait for a keypress
  1522.             ;close the first file
  1523.             mov AH, 3Eh
  1524.             mov BX, [word ptr srcHandle]
  1525.             int 21h
  1526.             jmp getChoice
  1527.         fileSucceed2:
  1528.        
  1529.         mov [dstHandle], AX
  1530.        
  1531.         ;###################### get password ######################
  1532.         mov DX, offset newlineMsg
  1533.         call printString
  1534.        
  1535.         mov DX, offset passwordEncMsg
  1536.         call printString
  1537.        
  1538.         mov DX, offset newlineMsg
  1539.         call printString
  1540.        
  1541.         mov AH, 0Ah
  1542.         mov DX, offset passwordBuffer
  1543.         int 21h
  1544.        
  1545.         ;###################### hash password ######################
  1546.         xor AX, AX
  1547.         mov AL, [passwordBuffer + 1]
  1548.         mov BX, offset passwordBuffer
  1549.         add BX, 2
  1550.         call getKeccakHash
  1551.        
  1552.         mov AX, offset keccakResult
  1553.         mov BX, offset rijndaelKey
  1554.         mov CX, 16
  1555.         call memcpy
  1556.        
  1557.         mov AX, offset keccakResult
  1558.         add AX, 16
  1559.         mov BX, offset IV
  1560.         call memcpy
  1561.        
  1562.         ;encrypt <s>with visuals</s>
  1563.         call rijndaelGenerateKey
  1564.        
  1565.         mov AX, offset IV
  1566.         mov BX, offset rijndaelState
  1567.         mov CX, 16
  1568.         call memcpy
  1569.        
  1570.         ;encrypt/decrypt using OFB
  1571.         ;in OFB encryption and decryption are exactly the same
  1572.        
  1573.         encryptLoop:
  1574.             ;get the next block of the OTP by encrypting the previous block
  1575.             call rijndaelEncryptBlock
  1576.            
  1577.             ;read a block of 16 bytes from the plaintext
  1578.             mov AH, 3Fh
  1579.             mov BX, [srcHandle]
  1580.             mov CX, 16
  1581.             mov DX, offset currentBlockBuffer
  1582.             int 21h
  1583.            
  1584.             ;check how many bytes were successfully read
  1585.             cmp AX, 16
  1586.            
  1587.             jne encryptLoopExit
  1588.            
  1589.             xor BX, BX
  1590.            
  1591.             ;xor the block of plaintext with the OTP
  1592.             encryptXorBlock:
  1593.                 mov DL, [rijndaelState + BX]
  1594.                 xor [currentBlockBuffer + BX], DL
  1595.                 inc BX
  1596.             cmp BX, 16
  1597.             jb encryptXorBlock
  1598.            
  1599.             ;write to file
  1600.             mov AH, 40h
  1601.             mov BX, [dstHandle]
  1602.             mov CX, 16
  1603.             mov DX, offset currentBlockBuffer
  1604.             int 21h
  1605.         jmp encryptLoop
  1606.        
  1607.         encryptLoopExit:
  1608.         ;encrypt the last partial block
  1609.        
  1610.         xor BX, BX
  1611.        
  1612.         ;xor the block of plaintext with the OTP
  1613.         encryptXorPartial:
  1614.             mov DL, [rijndaelState + BX]
  1615.             xor [currentBlockBuffer + BX], DL
  1616.             inc BX
  1617.         cmp BX, AX
  1618.         jb encryptXorPartial
  1619.        
  1620.         mov CX, AX
  1621.         mov AH, 40h
  1622.         mov BX, [dstHandle]
  1623.         mov DX, offset currentBlockBuffer
  1624.         int 21h
  1625.        
  1626.         mov DX, offset newlineMsg
  1627.         call printString
  1628.        
  1629.         mov DX, offset successMsg
  1630.         call printString
  1631.        
  1632.         mov DX, offset encryptedMsg
  1633.         call printString
  1634.        
  1635.         mov AH, 3Eh
  1636.         mov BX, [word ptr srcHandle]
  1637.         int 21h
  1638.        
  1639.         mov AH, 3Eh
  1640.         mov BX, [word ptr dstHandle]
  1641.         int 21h
  1642.        
  1643.         mov AH, 0
  1644.         int 16h ;wait for a keypress
  1645.     jmp workDone
  1646.    
  1647.     ;#####################################################################################
  1648.     ;################################# FILE DECRYPTION ###################################
  1649.     ;#####################################################################################
  1650.     fileDecrypt:
  1651.         mov DX, offset newlineMsg
  1652.         call printString
  1653.        
  1654.         mov DX, offset pathRequestMsg
  1655.         call printString
  1656.        
  1657.         mov DX, offset decryptedMsg
  1658.         call printString
  1659.        
  1660.         mov DX, offset newlineMsg
  1661.         call printString
  1662.        
  1663.         ;#################### get the file path ####################
  1664.         mov AH, 0Ah
  1665.         mov DX, offset pathSrc
  1666.         int 21h
  1667.        
  1668.         ;replace the CR EOS with null (ASCIIZ)
  1669.         xor BX, BX
  1670.         mov BL, [pathSrc + 1]
  1671.         mov [pathSrc + BX + 2], 0
  1672.        
  1673.         ;---- open the file ----
  1674.         mov AH, 3Dh
  1675.         mov AL, 0
  1676.         mov DX, offset pathSrc
  1677.         add DX, 2
  1678.         int 21h
  1679.        
  1680.         jnc fileSucceed3
  1681.             call printDosError
  1682.             mov AH, 0
  1683.             int 16h ;wait for a keypress
  1684.             jmp getChoice
  1685.         fileSucceed3:
  1686.        
  1687.         mov [srcHandle], AX
  1688.        
  1689.         ;#################### get the file destination path ####################
  1690.         mov DX, offset newlineMsg
  1691.         call printString
  1692.        
  1693.         mov DX, offset pathRequestDecMsg
  1694.         call printString
  1695.        
  1696.         mov DX, offset newlineMsg
  1697.         call printString
  1698.        
  1699.         mov AH, 0Ah
  1700.         mov DX, offset pathDst
  1701.         int 21h
  1702.        
  1703.         ;replace the CR EOS with null (ASCIIZ)
  1704.         xor BX, BX
  1705.         mov BL, [pathDst + 1]
  1706.         mov [pathDst + BX + 2], 0
  1707.        
  1708.         ;---- create the file ----
  1709.         mov AH, 3Ch
  1710.         mov AL, 1
  1711.         mov DX, offset pathDst
  1712.         add DX, 2
  1713.         xor CX, CX
  1714.         int 21h
  1715.        
  1716.         jnc fileSucceed4
  1717.             call printDosError
  1718.             mov AH, 0
  1719.             int 16h ;wait for a keypress
  1720.             ;close the first file
  1721.             mov AH, 3Eh
  1722.             mov BX, [word ptr srcHandle]
  1723.             int 21h
  1724.             jmp getChoice
  1725.         fileSucceed4:
  1726.        
  1727.         mov [dstHandle], AX
  1728.        
  1729.         ;###################### get password ######################
  1730.         mov DX, offset newlineMsg
  1731.         call printString
  1732.        
  1733.         mov DX, offset passwordDecMsg
  1734.         call printString
  1735.        
  1736.         mov DX, offset newlineMsg
  1737.         call printString
  1738.        
  1739.         mov AH, 0Ah
  1740.         mov DX, offset passwordBuffer
  1741.         int 21h
  1742.        
  1743.         ;###################### hash password ######################
  1744.         xor AX, AX
  1745.         mov AL, [passwordBuffer + 1]
  1746.         mov BX, offset passwordBuffer
  1747.         add BX, 2
  1748.         call getKeccakHash
  1749.        
  1750.         mov AX, offset keccakResult
  1751.         mov BX, offset rijndaelKey
  1752.         mov CX, 16
  1753.         call memcpy
  1754.        
  1755.         mov AX, offset keccakResult
  1756.         add AX, 16
  1757.         mov BX, offset IV
  1758.         call memcpy
  1759.        
  1760.         ;encrypt <s>with visuals</s>
  1761.         call rijndaelGenerateKey
  1762.        
  1763.         mov AX, offset IV
  1764.         mov BX, offset rijndaelState
  1765.         mov CX, 16
  1766.         call memcpy
  1767.        
  1768.         ;encrypt/decrypt using OFB
  1769.         ;in OFB encryption and decryption are exactly the same
  1770.        
  1771.         decryptLoop:
  1772.             ;get the next block of the OTP by encrypting the previous block
  1773.             call rijndaelEncryptBlock
  1774.            
  1775.             ;read a block of 16 bytes from the plaintext
  1776.             mov AH, 3Fh
  1777.             mov BX, [srcHandle]
  1778.             mov CX, 16
  1779.             mov DX, offset currentBlockBuffer
  1780.             int 21h
  1781.            
  1782.             ;check how many bytes were successfully read
  1783.             cmp AX, 16
  1784.            
  1785.             jne decryptLoopExit
  1786.            
  1787.             xor BX, BX
  1788.            
  1789.             ;xor the block of plaintext with the OTP
  1790.             decryptXorBlock:
  1791.                 mov DL, [rijndaelState + BX]
  1792.                 xor [currentBlockBuffer + BX], DL
  1793.                 inc BX
  1794.             cmp BX, 16
  1795.             jb decryptXorBlock
  1796.            
  1797.             ;write to file
  1798.             mov AH, 40h
  1799.             mov BX, [dstHandle]
  1800.             mov CX, 16
  1801.             mov DX, offset currentBlockBuffer
  1802.             int 21h
  1803.         jmp decryptLoop
  1804.        
  1805.         decryptLoopExit:
  1806.         ;encrypt the last partial block
  1807.        
  1808.         xor BX, BX
  1809.        
  1810.         ;xor the block of plaintext with the OTP
  1811.         decryptXorPartial:
  1812.             mov DL, [rijndaelState + BX]
  1813.             xor [currentBlockBuffer + BX], DL
  1814.             inc BX
  1815.         cmp BX, AX
  1816.         jb decryptXorPartial
  1817.        
  1818.         mov CX, AX
  1819.         mov AH, 40h
  1820.         mov BX, [dstHandle]
  1821.         mov DX, offset currentBlockBuffer
  1822.         int 21h
  1823.        
  1824.         mov DX, offset newlineMsg
  1825.         call printString
  1826.        
  1827.         mov DX, offset successMsg
  1828.         call printString
  1829.        
  1830.         mov DX, offset decryptedMsg
  1831.         call printString
  1832.        
  1833.         mov AH, 3Eh
  1834.         mov BX, [word ptr srcHandle]
  1835.         int 21h
  1836.        
  1837.         mov AH, 3Eh
  1838.         mov BX, [word ptr dstHandle]
  1839.         int 21h
  1840.        
  1841.         mov AH, 0
  1842.         int 16h ;wait for a keypress
  1843.     jmp workDone
  1844.    
  1845.     workDone:
  1846.     jmp start
  1847.    
  1848.     exitSequence:
  1849.    
  1850.     ;jmp exit
  1851.    
  1852. ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1853. ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1854. ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1855. ;@@@@@@@@@@@@@@@@@@@@@  PROGRAM EXIT  @@@@@@@@@@@@@@@@@@@@@@@@@
  1856. ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1857. ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1858. ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1859. exit:
  1860.     ;pause the program when its done
  1861.    
  1862.     mov BH, 07h
  1863.     call clearScreen
  1864.     mov DX, offset endMsg
  1865.     call printString
  1866.    
  1867.     mov AH, 0
  1868.     int 16h ;wait for a keypress
  1869.    
  1870.     mov BH, 07h
  1871.     call clearScreen
  1872.    
  1873.     mov CX, 0607h
  1874.     call setCursorShape
  1875.    
  1876.     mov AX, 4c00h
  1877.     int 21h
  1878. END start
  1879.  
  1880. ;https://www-s.acm.illinois.edu/sigops/roll_your_own/hardware/kb.html
  1881. ;http://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
  1882. ;http://www.moserware.com/2009/09/stick-figure-guide-to-advanced.html
  1883. ;http://swag.outpostbbs.net/FILES/0084.PAS.html
  1884. ;http://en.wikipedia.org/wiki/MS-DOS_API
  1885. ;http://stanislavs.org/helppc/int_21.html
  1886. ;https://courses.engr.illinois.edu/ece390/books/artofasm/CH13/CH13-6.html#HEADING6-321
  1887.  
  1888. ;http://stanislavs.org/helppc/int_21-40.html
  1889. ;http://stanislavs.org/helppc/int_21-3f.html
  1890. ;http://stanislavs.org/helppc/int_21-3e.html
  1891. ;http://stanislavs.org/helppc/int_21-3d.html
  1892. ;http://stanislavs.org/helppc/int_21.html
  1893.  
  1894. ;YKULVAARLCK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement