Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help for encryption using AN821 for pic16f877

Status
Not open for further replies.

ganavel9783

Member level 1
Joined
Sep 6, 2005
Messages
38
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,257
microchip project an821

Hi guys,

I'm currently working on a project to do encryption of text files using PIC16877. There is an Application Note from Microchip.com to do encryption using AN821 (Advanced Encryption Standard using PIC16XXX). I have a serial circuit that i would like to use to send a text file from a PC to be encrypted by PIC16f877.Attached is the serial circuit i'm using to test the code.

The problem i'm facing now are---->>
1) how do i test the code whether it can encrypt a text file or not?
2) how do i send the key for the text to be encrypted?
2) should i send a text file using Hyperterminal to test the code??
3) is there any subroutines i need to add to the assembly source code given so that it can work to take data(text file) serially from PC??
4) what are the changes i should make to make the assembly code work in PIC16f877 since it given for PIC16c22a?

Here is a sample from the source code given by microchip from the following website:
h**p://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en012009

I pasted the main rountines only below here.I didn't include the subrountines for the 4 data tranformation using AES algorithm.Any help will be deeply appreciated.

*** Program: aes_rijn.asm ***
*** This routine takes 16 bytes of data and ***
*** first encrypts it then decrypts it using ***
*** the AES/Rijndael algorithm. ***



list P=PIC16C622A
#include p16c622A.inc
#include TABLES.inc ; Holds S_TABLE & Si_TABLE Data
; Starts at Location 0x0600

__CONFIG _CP_OFF & _WDT_ON & _BODEN_OFF & _PWRTE_ON & _RC_OSC

;****************** Constants ************************
INIT_RAM0 equ 0x20 ; Starting point of RAM in segment 0
ROUNDS equ 0x0A ; number of rounds necessary to cipher/decipher

;****************** Variables ************************
block equ 0x20 ; block vector ( 16 bytes*8 => 128 bits)
key equ 0x30 ; key vector ( 16 bytes*8 => 128 bits)

aux equ 0x40 ; general purpose var
aux1 equ 0x41 ;
aux2 equ 0x42
aux3 equ 0x43

round_counter equ 0x44 ; encryption/decryption rounds counter
rcon equ 0x45 ;

d0 equ 0x4A
d1 equ 0x4B
d2 equ 0x4C

ORG 0x0000
start:
goto main ;

ORG 0x0004

int_handler:

; ***************** END Interrupt Handler ***********************

ORG 0x0010

;********************** MAIN Routine ****************************
; This routine cipher and decipher a given text.
; The init_decryption_key routine was put here to show how to
; create a decryption key from the initial key
;
main:
call init_cpu ; start CPU registers
call set_test_block ; loads the plain text to be ciphered
call set_test_key ; load the key used to cipher
call encrypt ; cipher the code
call set_test_key ; load a second time the key used to cipher
call init_decryption_key ; create the initial decryption key
call decrypt ; decipher the block back to plain text
goto $

; ***************** INIT_CPU ************************************
; This procedure cleans the RAM in segment 0
; Input: None
; Output: None
; ***************************************************************
init_cpu:
movlw INIT_RAM0 ;points to the start of the RAM
movwf FSR
loop_cram0
clrf INDF ;clear RAM location pointed by FSR
incf FSR,f ;increment pointer
btfss FSR,7 ;while FSR < 0x80 loop
goto loop_cram0 ;
return


; ********************* SET_TEST_BLOCK **************************
; This procedure sets the test plain text to be ciphered
; Input:
; Outup: block[16]={B0,B1,...}
; Globals: block,aux,FSR
; OBS: Generates block vector for test;
; ***************************************************************
set_test_block:
movlw 0x0
movwf block+0x0
movlw 0x0
movwf block+0x1
movlw 0x0
movwf block+0x2
movlw 0x0
movwf block+0x3
movlw 0x0
movwf block+0x4
movlw 0x0
movwf block+0x5
movlw 0x0
movwf block+0x6
movlw 0x0
movwf block+0x7
movlw 0x0
movwf block+0x8
movlw 0x0
movwf block+0x9
movlw 0x0
movwf block+0xA
movlw 0x0
movwf block+0xB
movlw 0x0
movwf block+0xC
movlw 0x0
movwf block+0xD
movlw 0x0
movwf block+0xE
movlw 0x0
movwf block+0xF
return
; ********************* SET_TEST_KEY **************************
; This procedure sets the key used to cipher
; Input: None
; Outup: key[16]
; Globals: key[16],FSR,aux
; ***************************************************************
set_test_key:
movlw 0x0
movwf key+0x0
movlw 0x0
movwf key+0x1
movlw 0x0
movwf key+0x2
movlw 0x0
movwf key+0x3
movlw 0x0
movwf key+0x4
movlw 0x0
movwf key+0x5
movlw 0x0
movwf key+0x6
movlw 0x0
movwf key+0x7
movlw 0x0
movwf key+0x8
movlw 0x0
movwf key+0x9
movlw 0x0
movwf key+0xA
movlw 0x0
movwf key+0xB
movlw 0x0
movwf key+0xC
movlw 0x0
movwf key+0xD
movlw 0x0
movwf key+0xE
movlw 0x80
movwf key+0xF
return

---------------------------------------------------------------------
---->>>Subroutines for SubBytes, ShiftRows, MixColumns, AddRoundKey
---------------------------------------------------------------------


; ************************** DECRYPT ******************************
; This procedure decipher the plain text in block using key
; Inputs: The encrypted text in block[],
; The decipher_key in key[]; this key is NOT the
; original cipher key used to cipher the plain text,
; but the scheduled original.
; Outups: The plain text in block
; The original cipher key in key[]
; OBS: This implementation follows as possible the original
; structure and function names used by the reference
; implementation of AES
; *****************************************************************
decrypt:
movlw 0x36
movwf rcon ; RCON initialization
call key_addition ; initial key addition
movlw ROUNDS
movwf round_counter ;
goto dec_first_round
loop_decrypt:
call inv_mix_column
dec_first_round:
call substitution_Si ;
call dec_shift_row
call dec_key_schedule
call key_addition ;
decfsz round_counter,f
goto loop_decrypt
return

; ************************** ENCRYPT ******************************
; This procedure cipher the plain text in block using key
; Inputs: The plain_text in block[]
; The cipher_key in key[]
; Outups: The ciphered text in block
; The rounded (decipher) key in key[]
; OBS: This implementation follows as possible the original
; structure and function names used by the reference
; implementation of AES
; *****************************************************************
encrypt:
movlw 0x01
movwf rcon ; RCON initialization
call key_addition ; initial key addition
movlw ROUNDS ; start the round counter for the loop
movwf round_counter
loop_encrypt:
call substitution_S ;
call enc_shift_row
decf round_counter,w
btfsc STATUS,Z
goto last_round
call mix_column
last_round:
call enc_key_schedule
call key_addition ; key addition
decfsz round_counter,f
goto loop_encrypt
return

; ***************** INIT_DECRYPTION_KEY ***************************
; This procedure creates the initial deciphering key from the initial
; cyphering key by multiple application of enc_key_schedule
; Inputs: The plain_text in block[]
; The cipher_key in key[]
; Outups: The ciphered text in block
; The rounded (decipher) key in key[]
; OBS: This implementation follows as possible the original
; structure and function names used by the reference
; implementation of AES
; *****************************************************************

init_decryption_key:
movlw 0x01
movwf rcon
movlw 0xA
movwf round_counter
loop_init_decryption_key:
call enc_key_schedule
decfsz round_counter,f
goto loop_init_decryption_key
return

;0E DD 33 D3 C6 21 E5 46 45 5B D8 BA 14 18 BE C8

END
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top