;***** Final Project - Credit card transaction system with RSA encryption ******** ; ; Tony Cuadra, Ilyas Elkin ; ;************************************************ .nolist .include "c:\avrtools\appnotes\4414def.inc" .list .def char =r0 .def savSREG =r1 ;save the status register .def RSAd =r11 .def RSAm =r12 .def RSAs =r13 .def RSAe =r14 .def bitcnt =R16 ;bit counter .def temp =R17 ;temporary storage register .def Txbyte =R18 ;Data to be transmitted .def Rxbyte =R19 ;Received data .def mode =r20 ;mode of the machine (getting #=0001, getting$=0010, processing=0100) .def temp =r21 ;temporary register .def timeout =r22 ;Timeout value in mSec passed to subroutine .def keyindex=r23 ;index for the key table .def tblindex=r24 ;index for the data table .def count =r25 ;count how many entries we have received .def TXflash =r26 ;text to be sent is in flash if <>0 .def first =r27 ;flag that is set to 1 if we are receiving the account num .def RXchar =r28 ;char received by the hardware UART .equ keymax =3*3 .equ tblmax =15*10 ;******************************* ; Data segment .dseg table: .byte 15*10 ;enough room for 10 entries ;6 byte account num + 0-term + 6 byte transaction amt + 0-term ;************************************** ;print and read string macros .macro PrintFlashStr ;use: PrintFlashStr flashaddress ldi TXflash, 1 ldi ZL, low(@0<<1) ldi ZH, high(@0<<1) rcall putString .endmacro .macro PrintRamStr ;use: PrintRamStr ramaddress ldi TXflash, 0 ldi ZL, low(@0) ldi ZH, high(@0) rcall putString .endmacro .macro PrintRamStr2 ;use: PrintRamStr2 ramaddress, offset_register ldi TXflash, 0 ldi ZL, low(@0) ldi ZH, high(@0) add ZL, @1 adc ZH, TXflash rcall putString .endmacro .macro LED ;use: LED register_to_output push temp mov temp, @0 com temp out PORTB, temp pop temp .endmacro .macro LED2 ;use: LED constant push temp ldi temp, @0 com temp out PORTB, temp pop temp .endmacro .macro delaymac ;use: delaymac time (mSec) ldi timeout, @0 rcall delay2 .endmacro ;******************************* ; Initialization .cseg .org $0000 rjmp RESET ;reset entry vector reti reti reti reti reti reti reti reti rjmp Monitor reti rjmp Monitor reti ;define fixed strings to be transmitted from flash- zero terminated prompt: .db "(c) clear table, (d) display table, (k) change key > ",0x00 heading:.db "Account Amount",0x00 spacer: .db " $",0x00 newline:.db 0x0d, 0x0a, 0x00 ;carrage return/line feed Receiving: .db "Receiving Transaction: ", 0x00 TableCleared: .db "Data table cleared", 0x00 KeyChanged: .db "RSA key changed", 0x00 ;define RSA keys: keys: .db 241, 83, 107, 251, 127, 63, 221, 23, 167 RESET: ldi Temp, LOW(RAMEND) ;setup stack pointer out SPL, Temp ldi Temp, HIGH(RAMEND) out SPH, Temp ;setup UART -- enable RX, TX pins without ISRs ldi temp, 0b00011000 out UCR, temp ;set baud rate to 9600 ldi temp, 25 out UBRR, temp ;enable receive ISR ONLY for keyboard monitor sbi UCR, RXCIE ;set up the PORTs ser temp ;set PORTB to be out DDRB,temp ;all outputs (for LEDs) ;software UART cbi PORTD,RxD cbi DDRD,RxD sbi PORTD,TxD sbi DDRD,TxD ;initial conditions clr tblindex clr keyindex rcall newkey printFlashStr newline printFlashStr prompt sei spin: rcall getchar cli ;disable interrupts so we can receive data cpi tblindex, tblmax brne notfull printFlashStr newline printFlashStr newline rcall tblflush ;output the table contents to the terminal clr tblindex ;clear the table notfull: printFlashStr newline printFlashStr newline printFlashStr Receiving delaymac 10 mov Txbyte, RSAm rcall putchar delaymac 10 mov Txbyte, RSAe rcall putchar ser first ;set first when we are receiving the account num ldi count, 6 ldi ZL, LOW(table) ldi ZH, HIGH(table) clr temp add ZL, tblindex adc ZH, temp getentry: rcall getchar mov RSAs, Rxbyte rcall encrypt ldi temp, '0' add RSAs, temp st Z, RSAs mov char, RSAs rcall putc adiw ZL, 1 cpi count, 3 breq decimal get_1: dec count brne getentry ldi count, 6 clr temp st Z, temp ;write terminating 0 adiw ZL, 1 tst first ;if first time through (got account num) breq notfirst clr first push ZL push ZH printFlashStr spacer pop ZH pop ZL rjmp getentry ;go through again to get transaction amount notfirst: subi tblindex, -15 ;point to next entry in the table printFlashStr newline printFlashStr newline printFlashStr prompt sei ;enable interrupts (done receiving data) rjmp spin decimal: tst first brne get_1 ldi temp, '.' mov char, temp rcall putc st Z, char adiw ZL, 1 rjmp get_1 ;*************************** ;output the entire contents of the table to the terminal tblflush: printFlashStr heading printFlashStr newline clr count tblflush_1: cp count, tblindex breq tblflush_2 printRamStr2 table, count subi count, -7 printFlashStr spacer printRamStr2 table, count subi count, -8 printFlashStr newline rjmp tblflush_1 tblflush_2: printFlashStr newline ret ;*************************** ;change RSA key newkey: clr temp ldi ZL, LOW(keys*2) ldi ZH, HIGH(keys*2) add ZL, keyindex adc ZH, temp lpm mov RSAm, r0 adiw ZL, 1 lpm mov RSAe, r0 adiw ZL, 1 lpm mov RSAd, r0 subi keyindex, -3 cpi keyindex, keymax brne newkey_1 clr keyindex newkey_1: ret ;*************************** ;handle input from hardware UART keypressed: cpi RXchar, 'd' breq key_disp cpi RXchar, 'D' breq key_disp cpi RXchar, 'c' breq key_clr cpi RXchar, 'C' breq key_clr cpi RXchar, 'k' breq key_newkey cpi RXchar, 'K' breq key_newkey ret key_disp: mov char, RXchar rcall putc printFlashStr newline printFlashStr newline rcall tblflush printFlashStr prompt ret key_clr: mov char, RXchar rcall putc clr tblindex printFlashStr newline printFlashStr newline printFlashStr TableCleared printFlashStr newline printFlashStr newline printFlashStr prompt ret key_newkey: mov char, RXchar rcall putc rcall newkey printFlashStr newline printFlashStr newline printFlashStr KeyChanged printFlashStr newline printFlashStr newline printFlashStr prompt ret ;********************************************* ;string soubroutines ;********************************************* ;routine to output one char ;enter with char in char putc: sbis USR, UDRE ;wait until clear then send one char rjmp putC out UDR, char ret ;routine to input one char ;exit with char in char getc: sbis USR, RXC ;wait until ready then get one char rjmp getc in char, UDR ret ;routine to send a string putString: tst TXflash ;is it flash? breq _txram lpm ;char ;if so get a char rjmp _tstend _txram: ld char, Z ;if mem get a char _tstend:tst char ;is it string end marker? breq _TXend rcall putc ;emit a char adiw ZL, 1 ;set up next pointer rjmp putString _TXend: ret delay2: push temp delay2_1: ldi temp, 250 delay2_2: nop nop nop nop nop nop nop nop nop nop nop nop nop dec temp brne delay2_2 dec timeout brne delay2_1 pop temp ret .include ".\uart.inc" .include ".\keymon3.inc" .include ".\rsa.inc"