Full Version : RC4 Data Encryption (AVR ASM)
avr >>ASSMBLER ROUTINES >>RC4 Data Encryption (AVR ASM)


AVR_Admin- 05-09-2006
CODE

;----------------------------------------------------------------------------;
;              RC4 CipherSaber in a chip  V1.0 beta                        ;
;                     low level layer Protocol                              ;
;----------------------------------------------------------------------------;


;***************************************************************************
;
;                                 AT90S8515Px
; SIGNAL_NAME                    ______ ______                     SIGNAL_NAME
;      .                        |      -      |                         .
;      .     -->    (TO) PB0  --| 1        40 |--   VCC                 .
;      .     -->    (T1) PB1  --| 2        39 |--   PA0 (AD0)   <->     .
;      .     -->  (AIN0) PB2  --| 3        38 |--   PA1 (AD1)   <->     .
;      .     -->  (AIN1) PB3  --| 4        37 |--   PA2 (AD2)   <->     .
;      .     -->   (SS*) PB4  --| 5        36 |--   PA3 (AD3)   <->     .
;      .     <--  (MOSI) PB5  --| 6        35 |--   PA4 (AD4)   <->     .
;      .     -->  (MISO) PB6  --| 7        34 |--   PA5 (AD5)   <->     .
;      .     <--   (SCK) PB7  --| 8        33 |--   PA6 (AD6)   <->     .
;      .     -->      RESET*  ->| 9        32 |--   PA7 (AD7)   <->     .
;rs 232.     -->   (RXD) PD0  --| 10       31 |--   ICP         <--     .
;rs 232.     <--   (TXD) PD1  --| 11       30 |--   ALE         -->     .
;      .     -->  (INT0) PD2  --| 12       29 |--   OC1B        -->     .
;      .     -->  (INT1) PD3  --| 13       28 |--   PC7 (A15)   -->     .
;      .     <->         PD4  --| 14       27 |--   PC6 (A14)   -->     .
;      .     <--  (OC1A) PD5  --| 15       26 |--   PC5 (A13)   -->     .
;      .     <--   (WR*) PD6  --| 16       25 |--   PC4 (A12)   -->     .
;      .     <--   (RD*) PD7  --| 17       24 |--   PC3 (A11)   -->     .
;      .     -->       XTAL1  ->| 18       23 |--   PC2 (A10)   -->     .
;      .     <--       XTAL2  <-| 19       22 |--   PC1 (A9)    -->     .
;                        GND  --| 20       21 |--   PC0 (A8)    -->     .
;                               |_____________|
;
;                                   (DIP-40)
;
;***************************************************************************
;*Algorithm RC4 CipherSaber by Ron Rivest of RSA Security  
;***************************************************************************
;S-array is initialized whiy  s0=0 ,s1=1 ,s2=2 ,s3=3,  ,,,s255=255
;k-array is initialized whit  PassPhrase + 10 ramdom bytes (mod key_leng)
 
;***************************************************************************
;*    Definitions
;***************************************************************************
                   
.include "c:\atmel\inc\8515def.inc";Define chip particulars

;***** Global register variables

.def temp                =       R16                        
.def    key_length              =       r17
.def    Ath                     =       r18    ;adresse pointer
.def    Bth                     =       r19    ;adresse pointer
.def    Nth                     =       r20    ;adresse pointer
.def    a                       =       r21
.def    b                       =       r22
.def    n                       =       r23
.def    temp2                   =       r24
.def    loop_255                =       r25
 
.equ   S_array                 =       $60
.equ    k_array                 =       $160          

.cseg

; Interrupt vectors

.org 0
rjmp reset                  ;Reset Vector

;***************************************************************************
;*    reset
;***************************************************************************
       
; Main program entry point on reset

reset:
       ldi temp,low(RAMEND)
out SPL,temp                ;init Stack Pointer  
ldi temp,high(RAMEND)
out SPH,temp

       ldi YH,high(S_array)
       ldi YL,low(S_array)                ;init Y-pointer
       ldi     temp,0

;***************************************************************************
;*    initialization of S array  (key for Encryption)
;***************************************************************************
       
ini_s:        
       
       st      Y+,temp                        ;start of ini of S_array
       inc     temp
       brne    ini_s                          ;end of ini of S_arry
       
;***************************************************************************
;*   initialization of k array  (PassPhrase)
;***************************************************************************

       ldi     temp,0
ldi YH,high(k_array)
ldi YL,low(k_array);init Y-pointer

ini_K_loop:

ldi ZH,high(key*2)
ldi ZL,low(key*2);init Z-pointer
ldi key_length,56

ini_k:
                           
lpm    ;get constant
st Y+,r0  ;store in SRAM and increment Y-pointer
adiw ZL,1  ;increment Z-pointer
dec key_length
brne ini_k

;***************************************************************************
;*    Generation of Pseudo-Random Stream
;***************************************************************************
     
ramdom: ldi     loop_255,255
       ldi     key_length,15
       ldi     n,0  
       ldi     a,0
       ldi     b,0
       ldi     ath,0
       ldi     nth,0
       ldi     bth,0
s0:     ldi YH,high(k_array)
ldi YL,low(k_array)                ;init Y-pointer
ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer

s1:  
       cp      a,ath                          ;check if poiter is a desired position
       breq    s1_out                        ;if yes go load memory location
       inc     a                              ;if no ok not bad just add a    
       adiw    r28,1                          ;dont forget to add memory pointer
       rjmp    s1                            ;now is time to make a other ride
                                   
s1_out: ld      a,y      

k1:            
       cp      n,nth                          ;check if poiter is a desired position
       breq    k1_out                        ;if yes go load memory location
       inc     n                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    k1                            ;now is time to make a other ride
                                   
k1_out: ld      n,z  
       
       add     a,n                            ;(a+n)
       add     b,a                            ;b=(a+n+b)
       add     bth,a                          ;bth=(a+n+b)   b adress pointer
       
     
       ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer
ldi     N,0
       
s2:        
       cp      N,Nth                          ;check if poiter is a desired position
       breq    s2_out                        ;if yes go load memory location
       inc     N                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    s2                            ;now is time to make a other ride
                                   
s2_out: ld      temp,z        
       
       ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer
ldi     B,0
       
s3:            
       cp      B,Bth                          ;check if poiter is a desired position
       breq    s3_out                        ;if yes go load memory location
       inc     B                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    s3                            ;now is time to make a other ride
       
s3_out: ld      temp2,z
       st      z,temp                                      

       ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer
ldi     N,0    

s4:        
       cp      N,Nth                          ;check if poiter is a desired position
       breq    s4_out                        ;if yes go load memory location
       inc     N                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    s4                            ;now is time to make a other ride
       
s4_out: st      z,temp2                        
   
       ldi     a,0
       ldi     n,0
       inc     ath
       inc     nth
       cp      nth,loop_255
       breq    xor
       cp      ath,key_length
       breq    mod_key    
       rjmp    s0
   ;  rjmp    xor
       
mod_key:
     
       ldi     ath,0
       rjmp    s0    
       
;***************************************************************************
;*    now is time to scramble the data (the proff of your are guilty)
;***************************************************************************          

xor:    ldi     n,0  
   ;   ldi     a,0
       ldi     b,0
       ldi     ath,0
       ldi     nth,0
       ldi     bth,0
encryp:
     
ldi ZH,high(s_array)
ldi ZL,low(s_array)
ldi     A,0

inc     ath

e1:  
       cp      a,ath                          ;check if poiter is a desired position
       breq    e1_out                        ;if yes go load memory location
       inc     a                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    e1                            ;now is time to make a other ride
                                   
E1_out: ld      a,z      
 
       add     bth,a
       
       ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer
ldi     A,0
       
E2:        
       cp      A,Ath                          ;check if poiter is a desired position
       breq    E2_out                        ;if yes go load memory location
       inc     A                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    E2                            ;now is time to make a other ride
                                   
E2_out: ld      temp,z        
       
       ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer
ldi     B,0
       
E3:            
       cp      B,Bth                          ;check if poiter is a desired position
       breq    E3_out                        ;if yes go load memory location
       inc     B                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    E3                            ;now is time to make a other ride
       
E3_out: ld      temp2,z
       st      z,temp                                      

       ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer
ldi     A,0    

E4:        
       cp      A,Ath                          ;check if poiter is a desired position
       breq    E4_out                        ;if yes go load memory location
       inc     A                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    E4                            ;now is time to make a other ride
       
E4_out: st      z,temp2

       add     temp2,temp
       add     nth,temp2
       
       ldi ZH,high(s_array)
ldi ZL,low(s_array)                ;init Z-pointer
ldi     n,0          
E5:  
       cp      N,Nth                          ;check if poiter is a desired position
       breq    E5_out                        ;if yes go load memory location
       inc     N                              ;if no ok not bad just add a    
       adiw    r30,1                          ;dont forget to add memory pointer
       rjmp    E5                            ;now is time to make a other ride
                                   
E5_out: ld      N,z
       ldi     nth,0

byte_xor:
       
 ;    eor     n,data
       
         
end:    rjmp    encryp
     



key: .db "this is the test passphrase using for test",0

Link: http://vfx.freeweb.hu/avr/index.html


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.