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.

Is this serial data transfer appropriate for encryption?

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
p16_tprp

hi every1,

i wrote a pc application in Visual C which could send text file by block of 8 bytes.i short pin 2 and 3 of the serial port to test the GUI without any hardware.i was able to get back data by block of 8 bytes.any incomplete block is padded with a space character.

Later i tested the GUI with PIC16f877.I loaded the following code from microchip's application note AN774 to test serial comm using buffres:

P16_TWRP.ASM Poll to receive, Wait to transmit, No buffers, Eight bit data

the code for the .asm file is show below:

Code:
============================================================================= 
; Filename: p16_tprp.asm 
;============================================================================= 
; Author:  Mike Garbutt 
; Company: Microchip Technology Inc. 
; Revision: 1.00 
; Date:  August 6, 2002 
; Assembled using MPASMWIN V3.20 
;============================================================================= 
; Include Files: p16f877.inc V1.12 
;============================================================================= 
; PIC16XXX USART example code with transmit and receive polling. The 
; main routine calls calls subroutines to poll for received data and for 
; data to be transmitted. Received data is put into a buffer, called 
; RxBuffer. When a carriage return <CR> is received, the received data 
; in RxBuffer is copied into another buffer, TxBuffer. The data in 
; TxBuffer is then transmitted by the transmit polling routine. 
;============================================================================= 
 list p=16f877  ;list directive to define processor 
 #include <p16f877.inc> ;processor specific definitions 
 errorlevel -302  ;suppress "not in bank 0" message 
 __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_OFF & _LVP_OFF & _DEBUG_OFF & _CPD_OFF 
;---------------------------------------------------------------------------- 
;Constants 
SPBRG_VAL EQU .103  ;set baud rate 9600 for 16Mhz clock 
RX_BUF_LEN EQU .8  ;length of receive buffer 
TX_BUF_LEN EQU RX_BUF_LEN ;length of transmit buffer 
;---------------------------------------------------------------------------- 
;Bit Definitions 
ReceivedCR EQU 0  ;bit indicates <CR> character received 
;---------------------------------------------------------------------------- 
;Variables 
 CBLOCK 0x020 
 Flags   ;byte to store indicator flags 
 RxByteCount  ;number of bytes received 
 TxByteCount  ;number of bytes to transmit 
 TxPointer  ;pointer to data in buffer 
 ENDC 
 CBLOCK 0x0A0 
 TxBuffer:TX_BUF_LEN ;buffer for data to transmit 
 ENDC 
 CBLOCK 0x120 
 RxBuffer:RX_BUF_LEN ;buffer for data received 
 ENDC 
;---------------------------------------------------------------------------- 
;Macros to select the register bank 
;Many bank changes can be optimized when only one STATUS bit changes 
Bank0  MACRO   ;macro to select data RAM bank 0 
 bcf STATUS,RP0 
 bcf STATUS,RP1 
 ENDM 
Bank1  MACRO   ;macro to select data RAM bank 1 
 bsf STATUS,RP0 
 bcf STATUS,RP1 
 ENDM 
Bank2  MACRO   ;macro to select data RAM bank 2 
 bcf STATUS,RP0 
 bsf STATUS,RP1 
 ENDM 
Bank3  MACRO   ;macro to select data RAM bank 3 
 bsf STATUS,RP0 
 bsf STATUS,RP1 
 ENDM 
;---------------------------------------------------------------------------- 
;This code executes when a reset occurs. 
 ORG     0x0000  ;place code at reset vector 
ResetCode: clrf    PCLATH  ;select program memory page 0 
  goto    Main  ;go to beginning of program 
;---------------------------------------------------------------------------- 
;This code executes when an interrupt occurs. 
 ORG 0x0004  ;place code at interrupt vector 
InterruptCode: ;do interrupts here 
 retfie   ;return from interrupt 
;---------------------------------------------------------------------------- 
;Main routine calls the transmit and receive polling routines and checks for a 
;carriage return. It then calls a routine to copy the data to transmit back. 
Main:  call SetupSerial ;set up serial port and buffers 
 ;do other initialization here 
MainLoop: call TransmitSerial ;go transmit data if possible 
 call ReceiveSerial ;go get received data if possible 
 btfsc Flags,ReceivedCR ;check if <CR> received 
 call CopyRxToTx ;if so then go copy the data 
 ;do other stuff here 
 goto MainLoop ;go do main loop again 
;---------------------------------------------------------------------------- 
;Check if data received and if so, place in a buffer. Also check for <CR>. 
ReceiveSerial: Bank0   ;select bank 0 
 btfss PIR1,RCIF ;check if data 
 return   ;return if no data 
 btfsc RCSTA,OERR ;if overrun error occurred 
 goto ErrSerialOverr ; then go handle error 
 btfsc RCSTA,FERR ;if framing error occurred 
 goto ErrSerialFrame ; then go handle error 
 movlw RX_BUF_LEN ;get buffer length 
 xorwf RxByteCount,W ;and compare with byte count 
 btfsc STATUS,Z ;check if buffer is full 
 goto ErrRxBufOver ; go handle it if full 
ReceiveSer1:  
 BANKISEL RxBuffer ;bank bit for indirect addressing 
 movlw LOW RxBuffer  ;get start address of buffer 
 addwf RxByteCount,W ;add current byte number 
 movwf FSR  ;and place in FSR pointer 
 movf RCREG,W  ;get received data 
 movwf INDF  ;and place in buffer 
 xorlw 0x0d  ;compare with <CR>   
 btfsc STATUS,Z ;check if the same 
 bsf Flags,ReceivedCR ;indicate <CR> character received 
 incf RxByteCount,F ;increment count of bytes received 
 return 
;error because OERR overrun error bit is set 
;can do special error handling here - this code simply clears and continues 
ErrSerialOverr: bcf RCSTA,CREN ;reset the receiver logic 
 bsf RCSTA,CREN ;enable reception again 
 return 
;error because FERR framing error bit is set 
;can do special error handling here - this code simply clears and continues 
ErrSerialFrame: movf RCREG,W  ;discard received data that has error 
 return 
;error because Rx buffer is full and new data has been received 
;can do special error handling here - this code simply overwrites last data 
ErrRxBufOver: decf RxByteCount,F ;decrement byte count to overwrite last 
 goto ReceiveSer1 
;---------------------------------------------------------------------------- 
;Transmit data if there is data in the transmit buffer. 
TransmitSerial: Bank0   ;select bank 0 
 movf TxByteCount,F ;check if data to be transmitted 
 btfsc STATUS,Z 
 return   ;return if no data to be transmitted 
 btfss PIR1,TXIF ;check if transmitter busy 
 return   ;return if transmitter busy 
 BANKISEL TxBuffer ;bank bit for indirect addressing 
 movf TxPointer,W ;put pointer into WREG 
 movwf FSR  ;and then into FSR 
 movf INDF,W  ;get the data to be transmitted 
 movwf TXREG  ;and transmit 
 decf TxByteCount,F ;decrement number of bytes left 
 incf TxPointer,F ;increment pointer to next data 
 return 
;---------------------------------------------------------------------------- 
;Set up serial port and buffers. 
SetupSerial: Bank1   ;select bank 1 
 movlw 0xc0  ;set tris bits for TX and RX 
 iorwf TRISC,F 
 movlw SPBRG_VAL ;set baud rate 
 movwf SPBRG 
 movlw 0x24  ;enable transmission and high baud rate 
 movwf TXSTA 
 Bank0   ;select bank 0 
 movlw 0x90  ;enable serial port and reception 
 movwf RCSTA 
 clrf TxByteCount ;clear transmit buffer data count 
 clrf RxByteCount ;clear receive buffer data count 
 clrf Flags  ;clear all flags 
 return 
;---------------------------------------------------------------------------- 
;Copy data from receive buffer to transmit buffer to echo the line back 
CopyRxToTx: Bank0   ;select bank 0 
 movf TxByteCount,F ;check if still transmitting data 
 btfss STATUS,Z 
 goto ErrTxBufOver ;go handle error if still busy 
CopyRxTx1: 
 BANKISEL RxBuffer ;bank bit for indirect addressing 
 movlw LOW RxBuffer  ;get start address of buffer 
 addwf TxByteCount,W ;add current byte number 
 movwf FSR  ;and place in FSR pointer 
 movf INDF,W  ;get data from Rx buffer 
 movwf TxPointer ;and save for later (reusing register) 
 BANKISEL TxBuffer ;bank bit for indirect addressing 
 movlw LOW TxBuffer  ;get start address of buffer 
 addwf TxByteCount,W ;add current byte number 
 movwf FSR  ;and place in FSR pointer 
 movf TxPointer,W ;get saved data (not a pointer) 
 movwf INDF  ;and place in Tx buffer 
 incf TxByteCount,F ;increment count of bytes in TxBuffer 
 decfsz RxByteCount,F ;decrement counter and see if all done 
 goto CopyRxTx1 ;repeat if not done 
 movlw LOW TxBuffer ;take address of transmit buffer 
 movwf TxPointer ;and place in transmit pointer 
 bcf Flags,ReceivedCR ;clear indicator for <CR> received 
 return 
;error because still transmitting data and new data is available to transmit 
;can do special error handling here - this code simply discards the new data 
ErrTxBufOver: clrf RxByteCount ;reset received byte count 
 bcf Flags,ReceivedCR ;clear indicator for <CR> received 
 return 
;---------------------------------------------------------------------------- 
 END

i've managed to include a carriage return <cr> character after each block of 8 bytes in my PC application.i also set the buffer length at 8 bytes in the p16_tprp.asm for my PIC16f877 code.i tested the serial comm with text files which has 7,8,9,15,16 and 17 characters.

however,when i tested the code with 8 bytes as buffer length on da PIC side, i realise that the 8th bytes for the text file was replaced by the <cr> character.then when i changed the buffer length from 9 onwards on da PIC side, the data reception was working fine.

after that i edited my PC application code so that the <cr> character is send after 2 blocks of 8 bytes.this time i set the buffer length to 16, and when a text file more than 15 bytes is sent, the 16th byte is replaced by the <cr> character.then when i change the buffer length from 17 onwards on da PIC side, the data reception was working fine.

so my question is this data transfer appropriate for me to proceed with encrypting text files using the AES algoithm since the algorithm works with blocks of 16 bytes for encryption??

regards,
ganavel9783
 

16f877a fsr

Hi you can go ahead with encryption at application level. i.e. use some block cipher and encrypt at application level itself.

use AED, DES, 3DES like encryption algorithms
 

low rxbuffer

i've managed to include a carriage return <cr> character after each block of 8 bytes
i also set the buffer length at 8 bytes in the p16_tprp.asm

So you send 9 bytes (8 + <cr>).
Now look at this statement in your code :
Code:
ErrRxBufOver: decf RxByteCount,F ;decrement byte count to overwrite last received
              goto ReceiveSer1

Because you use a linear buffer of 8 bytes rather than a circular one, before receiving <CR>, PIC16F877 will keep the pointer to the last 8th byte in the receiving buffer regardless the number of received bytes.
Obvious until a <CR> will appear. And funny, <CR> is the 8th byte stored in the RxBuffer, which will then be coppied to TxBuffer.
Code:
 movf RCREG,W  ;get received data 
 movwf INDF  ;and place in buffer 
 xorlw 0x0d  ;compare with <CR>    
 btfsc STATUS,Z ;check if the same 
 bsf Flags,ReceivedCR ;indicate <CR> character received 
 incf RxByteCount,F ;increment count of bytes received

This way for a RxBuffer of 8 bytes and a string "ABCDEFGHIJKLMNO<CR>" sent by PC, you'll get back "ABCDEFG<CR>" and "HIJKLMNO" lost.

If you set the PIC16F877 receiving buffer length to 17 and take care not to overwrite it by sending subsequent 16 bytes (not until you get back the previous 16) then you can go ahead with encryption.

Do you want to build up a dongle or trying to mask your updated code loaded by PIC16F877 bootloader ?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top