Untitled

 avatar
unknown
plain_text
9 months ago
8.3 kB
3
Indexable
org 100H


    
    

.DATA

i1 db 0aH,0dH, "Enter Input State: $"

i2 db 0aH,0dH, "Enter Cipher Key: $"   ;-ah,0dh for new line 

o1 db 0aH,0dH, "Output: $"



SBOX DB 63H, 07cH, 77H, 7bH, 0f2H, 6bH, 6fH, 0c5H, 30H, 01H, 67H, 2bH, 0feH, 0d7H, 0abH, 76H
     DB 0caH, 82H, 0c9H, 7dH, 0faH, 59H, 47H, 0f0H, 0adH, 0d4H, 0a2H, 0afH, 9cH, 0a4H, 72H, 0c0H
     DB 0b7H, 0fdH, 93H, 26H, 36H, 3fH, 0f7H, 0ccH, 34H, 0a5H, 0e5H, 0f1H, 71H, 0d8H, 31H, 15H
     DB 04H, 0c7H, 23H, 0c3H, 18H, 96H, 05H, 9aH, 07H, 12H, 80H, 0e2H, 0ebH, 27H, 0b2H, 75H
     DB 09H, 83H, 2cH, 1aH, 1bH, 6eH, 5aH, 0a0H, 52H, 3bH, 0d6H, 0b3H, 29H, 0e3H, 2fH, 84H
     DB 53H, 0d1H, 00H, 0edH, 20H, 0fcH, 0b1H, 5bH, 6aH, 0cbH, 0beH, 39H, 4aH, 4cH, 58H, 0cfH
     DB 0d0H, 0efH, 0aaH, 0fbH, 43H, 4dH, 33H, 85H, 45H, 0f9H, 02H, 7fH, 50H, 3cH, 9fH, 0a8H
     DB 51H, 0a3H, 40H, 8fH, 92H, 9dH, 38H, 0f5H, 0bcH, 0b6H, 0daH, 21H, 10H, 0ffH, 0f3H, 0d2H
     DB 0cdH, 0cH, 13H, 0ecH, 5fH, 97H, 44H, 17H, 0c4H, 0a7H, 7eH, 3dH, 64H, 5dH, 19H, 73H
     DB 60H, 81H, 4fH, 0dcH, 22H, 2aH, 90H, 88H, 46H, 0eeH, 0b8H, 14H, 0deH, 5eH, 0bH, 0dbH
     DB 0e0H, 32H, 3aH, 0aH, 49H, 06H, 24H, 5cH, 0c2H, 0d3H, 0acH, 62H, 91H, 95H, 0e4H, 79H
     DB 0e7H, 0c8H, 37H, 6dH, 8dH, 0d5H, 4eH, 0a9H, 6cH, 56H, 0f4H, 0eaH, 65H, 7aH, 0aeH, 08H
     DB 0baH, 78H, 25H, 2eH, 1cH, 0a6H, 0b4H, 0c6H, 0e8H, 0ddH, 74H, 1fH, 4bH, 0bdH, 8bH, 8aH
     DB 70H, 3eH, 0b5H, 66H, 48H, 03H, 0f6H, 0eH, 61H, 35H, 57H, 0b9H, 86H, 0c1H, 1dH, 9eH
     DB 0e1H, 0f8H, 98H, 11H, 69H, 0d9H, 8eH, 94H, 9bH, 1eH, 87H, 0e9H, 0ceH, 55H, 28H, 0dfH
     DB 8cH, 0a1H, 89H, 0dH, 0bfH, 0e6H, 42H, 68H, 41H, 99H, 2dH, 0fH, 0b0H, 54H, 0bbH, 16H

i     DB 19H, 0a0H, 9aH, 0e9H
      DB 3dH, 0f4H, 0c6H, 0f8H
      DB 0e3H, 0e2H, 8dH, 48H
      DB 0beH, 2bH, 2aH, 08H
      DB 24H
      
m      DB 2H, 3H, 1H, 1H
       DB 1H, 2H, 3H, 1H
       DB 1H, 1H, 2H, 3H
       DB 3H, 1H, 1H, 2H
       
    


rm    DB 19H, 0a0H, 9aH, 0e9H
      DB 3dH, 0f4H, 0c6H, 0f8H
      DB 0e3H, 0e2H, 8dH, 48H
      DB 0beH, 2bH, 2aH, 08H 


      
RoundKey DB 00H, 00H, 00H, 00H
         DB 00H, 00H, 00H, 00H
         DB 00H, 00H, 00H, 00H
         DB 00H, 00H, 00H, 00H


CipherKey DB 2bH, 28H, 0abH, 09H
          DB 7eH, 0aeH, 0f7H, 0cfH
          DB 15H, 0d2H, 15H, 4fH
          DB 16H, 0a6H, 88H, 3cH
          
Rcon    DB 01H, 02H, 04H, 08H, 10H, 20H, 40H, 80H, 1bH, 36H
        DB 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H
        DB 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H
        DB 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H, 00H
                   


counterRcon DW 0H    

Testing        DB 00H, 00H, 00H, 00H
         DB 00H, 00H, 00H, 00H
         DB 00H, 00H, 00H, 00H
         DB 00H, 00H, 00H, 00H

               
 
 
     
.CODE   
;Printing "Enter Input"
LEA DX,i1
MOV AH,09H
INT 21H  

;Taking Input from user till he presses ENTER
Mov DX, offset i
mov AH, 0ah
int 21h


;PPrinting "Enter Cipher Key"
LEA DX,i2
MOV AH,09H 
INT 21H

   
;Cipher Key Input (has to be 16 bytes)    
MOV CX,16
MOV SI,0 

CipherKeyInput:
MOV AH,01H
INT 21H
MOV CipherKey[SI],AL
INC SI
Loop CipherKeyInput




MOV CX,10
SOLVE:      
    PUSH CX
    MOV CX,16
    MOV SI,0H



;===================================SubBytes================================
SUBBYTES:    
    MOV AL,i[SI]
    MOV Bl,Al
    MOV AL,SBOX[BX]
    MOV [i+SI],AL
    INC SI
    Loop SUBBYTES
              


;===================================ShiftRows================================            
SHIFT_ROWS:
    ; Row 2
    MOV AL, [i+4]    
    MOV BL, [i+5]    
    MOV CL, [i+6]   
    MOV DL, [i+7]    

    MOV [i+4], BL   
    MOV [i+5], CL    
    MOV [i+6], DL    
    MOV [i+7], AL    

    ;Row 3
    MOV AL, [i+8]    
    MOV BL, [i+9]   
    MOV CL, [i+10]   
    MOV DL, [i+11]   

    MOV [i+8], CL    
    MOV [i+9], DL    
    MOV [i+10], AL   
    MOV [i+11], BL   

    ; Row 4 
    MOV AL, [i+12]   
    MOV BL, [i+13]   
    MOV CL, [i+14]   
    MOV DL, [i+15]  

    MOV [i+12], DL   
    MOV [i+13], AL   
    MOV [i+14], BL   
    MOV [i+15], CL   
    


;If it's the last round, no MIX Columns  
MOV AX,1
POP CX
SUB AX,CX
PUSH CX
JZ CipherDecryption
     
;===================================MixColumns================================ 
;For 2 Multiplication
overflow: Macro  
    LOCAL overf, done
    MOV BL,m[0]
    Mul BL
    JC overf
    JMP done:
    
overf:
    XOR AL,1BH

done:
ENDM

;for 3 Multiplication
Matri: Macro 
    overflow
    MOV BX,DI
    XOR AL,i[BX+SI]
    INC SI
    INC SI
    INC SI
    INC SI
        
ENDM

MOV CX,4
MOV SI,4
MOV DI,0
MixColumns:
    ;i[0][0]
    Mov AL,i[DI] 
    overflow
    MOV DL,AL 
    
    MOV AL,i[DI+4]
    
    Matri
    XOR DL,AL
    XOR DL,i[DI+8]
    XOR DL,i[DI+12]
    MOV rm[DI],DL
    
    
    
    ;i[1,0] 
      
    MOV DL,i[DI]
    Mov AL,i[DI+4] 
    overflow
    XOR DL,AL 
    MOV AL,i[DI+8]
    Matri
    XOR DL,AL
    XOR DL,i[DI+12]
    MOV rm[DI+4],DL
    
    ;i[2,0] 
    MOV DL,i[DI]
    XOR DL,i[DI+4]
    MOV AL,i[DI+8]
    overflow
    XOR DL,AL  
    MOV AL,i[DI+12]
    Matri
    XOR DL,AL
    MOV rm[DI+8],DL
    
    ;i[3,0]
    MOV SI,0 
    MOV AL,i[DI]
    Matri
    MOV DL,AL
    XOR DL,i[DI+4]
    XOR DL,i[DI+8]
    MOV AL,i[DI+12]
    overflow
    XOR DL,AL
    MOV rm[DI+12],DL 
    INC DI
    Loop MixColumns





;===================================Bonus================================

CipherDecryption:
    ;First Column
    MOV AL, CipherKey[3] 
    MOV BL, CipherKey[7]
    MOV CL, CipherKey[11]
    MOV DL, CipherKey[15]
    
    MOV RoundKey[0],BL
    MOV RoundKey[4],CL
    MOV RoundKey[8],DL
    MOV RoundKey[12],AL
    
    MOV CX,4
    MOV SI,0
    MOV DI,3
SB: 
    MOV AL,RoundKey[SI]
    MOV BL,AL
    MOV AL,SBOX[BX]
    MOV BL,AL
    MOV RoundKey[SI],BL
    INC SI
    INC SI
    INC SI
    INC SI
    INC DI
    INC DI
    INC DI
    INC DI
    INC DI
    Loop SB 
    
    ;XOR  FIRST COL
    MOV CX,4
    MOV SI,0
FCXOR:
    MOV AL,CipherKey[SI]
    MOV BL,AL
    XOR RoundKey[SI],BL
    INC SI
    INC SI
    INC SI
    INC SI
    Loop FCXOR
   
   
 
    ;XOR RCON
    MOV SI, counterRcon[0]
    MOV AL,Rcon[SI]
    MOV BL,AL
    XOR RoundKey[0],BL
    
    INC counterRcon
    ;First Row            
    MOV CX,3 
    MOV SI,1           
R1: 
    MOV AL,CipherKey[SI]
    MOV BL,AL
    XOR BL,RoundKey[SI-1]
    MOV RoundKey[SI],BL
    INC SI 
    Loop R1
    
    ;Second Row
    MOV CX,3
    MOV SI,5
    
R2: 
    MOV AL,CipherKey[SI]
    MOV BL,AL
    XOR BL,RoundKey[SI-1]
    MOV RoundKey[SI],BL
    INC SI 
    Loop R2
    
    ;Third Row
    MOV CX,3
    MOV SI,9 
    
R3: 
    MOV AL,CipherKey[SI]
    MOV BL,AL
    XOR BL,RoundKey[SI-1]
    MOV RoundKey[SI],BL
    INC SI 
    Loop R3
    
    ;Fourth Row
    MOV CX,3
    MOV SI,13
R4: 
    MOV AL,CipherKey[SI]
    MOV BL,AL
    XOR BL,RoundKey[SI-1]
    MOV RoundKey[SI],BL
    INC SI 
    Loop R4 
    

    MOV CX,16
    MOV SI,0
CopyFromKeytoCipher:
    MOV AL,RoundKey[SI]
    MOV BL,AL
    MOV CipherKey[SI],BL
    INC SI
    Loop CopyFromKeytoCipher
       
    


;===================================RoundKey================================
;If it's the last round, it copies whatever is in i to rm
MOV AX,1
POP CX
SUB AX,CX
PUSH CX
JNZ NotLastRound


MOV CX,16
MOV SI,0
Copy:
    MOV AL,i[SI]
    MOV rm[SI],AL
    INC SI
    Loop copy



NotLastRound: 
MOV CX,16
MOV SI,0  
AddRoundKey:
    MOV AL,RoundKey[SI]
    MOV BL,rm[SI]
    XOR AL,BL
    MOV rm[SI],AL 
    INC SI
    Loop  AddRoundKey

MOV CX,16
MOV SI,0 



CopyAgain:
    MOV AL,rm[SI]
    MOV i[SI],AL
    INC SI
    Loop CopyAgain




    
End:        
    POP CX
    Loop SOLVE




Output: 
                                     
LEA DX,o1
MOV AH,09H
INT 21H 

LEA DX,i
MOV AH,09H
INT 21H 

       
       

;====================================================================
  


Editor is loading...
Leave a Comment