📄 sha1prog.psm
字号:
; contained in the page are read back from the DS2432 and these are used in
; the preparation of the table required for the for SHA-1 algorithm. Other values
; stored in the table are the secret, serial number of the DS2432, family code, some
; constants, 4-bits of the page address and a 3 byte 'challenge' currently set into
; the DS2432 scratchpad memory.
;
; NOTE - The read scratchpad command must be executed before this routine in order
; that the 3 byte 'challenge' of scratchpad memory is known to PicoBlaze.
;
; During this command, two 16-bit CRC values are generated which PicoBlaze also
; computes and tests. The first is formed of the command byte, address TA1 and TA2,
; all the bytes of the page read and an 'FF' byte. The second is formed of the 20
; bytes of the 160-but message authentication code (MAC).
;
;
; Preparing the table.
;
; The table is stored in the external 'Wt' buffer and must first be initialised with the
; 16 'M' words (32-bit words each requiring 4 bytes). This is achieved by shifting in
; each word in sequence. Storing each word most significant byte first is a natural
; fit with the reading of the page data from the DS2432 and the way each 'M' word
; is organised. Notice how this causes least significant bytes to be swapped with most
; significant bytes!
;
; [31:24] [23:16] [15:8] [7:0]
;
; M0 = [secret0 , secret1 , secret2 , secret3 ]
; M1 = [page_data0 , page_data1 , page_data2 , page_data3 ]
; M2 = [page_data4 , page_data5 , page_data6 , page_data7 ]
; M3 = [page_data8 , page_data9 , page_data10, page_data11]
; M4 = [page_data12, page_data13, page_data14, page_data15]
; M5 = [page_data16, page_data17, page_data18, page_data19]
; M6 = [page_data20, page_data21, page_data22, page_data23]
; M7 = [page_data24, page_data25, page_data26, page_data27]
; M8 = [page_data28, page_data29, page_data30, page_data31]
; M9 = [ FF , FF , FF , FF ]
; M10 = [ 40 , 33 , serial_num0, serial_num1]
; M11 = [serial_num2, serial_num3, serial_num4, serial_num5]
; M12 = [secret4 , secret5 , secret6 , secret7 ]
; M13 = [scratchpad4, scratchpad5, scratchpad6, 80 ]
; M14 = [ 00 , 00 , 00 , 00 ]
; M15 = [ 00 , 00 , 01 , B8 ]
;
; In M10, the '33' is the family code and the '40' is made up of a constant bit
; pattern '0100' and then bits [8:5] of the page address. This gives 4 possible values
; for this byte during a Read Authenticated Page Command, but this routine is currently
; fixed to work with page 0 only.
; 40 - page 0
; 41 - page 1
; 42 - page 2
; 43 - page 3
;
; M13 contains the 3 byte challenge from the scratch pad memory. This assumes that a
; read scratchpad command has previously been used and the bytes held in the DS2432
; scratchpad match those held in the PicoBlaze memory.
;
;
; The 160-bit Message Authentication Code (MAC) is computed from the table using the SHA-1
; algorithm. This algorithm actually results in 5 variables 'A', 'B', 'C', 'D' and 'E'
; which are 32-bit values each formed of 4 bytes. The MAC is the concatenation of
; the variables. To match the same order in which the Read Authenticated Page Command
; sends the MAC, the variables must be read in the order 'E', 'D', 'C', 'B' and 'A' and
; with the least significant byte of each variable first.
;
;
;
;
;
read_auth_page_command: LOAD s0, secret0 ;store M0 (secret 0, 1, 2 and 3) in Wt buffer.
OUTPUT s0, W_word_write_port
LOAD s0, secret1
OUTPUT s0, W_word_write_port
LOAD s0, secret2
OUTPUT s0, W_word_write_port
LOAD s0, secret3
OUTPUT s0, W_word_write_port
;
;Start of DS2432 command
;
CALL clear_CRC16 ;prepare CRC registers [sE,sD]
LOAD s3, A5 ;read authenticated page command
CALL write_byte_slow ;transmit command
CALL compute_CRC16 ;compute CRC for value in 's3'
LOAD s5, 00 ;set address for page 0
LOAD s4, 00 ; [TA2,TA1]=0000 hex
LOAD s3, s4 ;transmit TA1
CALL write_byte_slow
CALL compute_CRC16 ;compute CRC for value in 's3'
LOAD s3, s5 ;transmit TA2
CALL write_byte_slow
CALL compute_CRC16 ;compute CRC for value in 's3'
;
;Read 32-bytes of data associated with page 0
;Store these as M1 through to M8
;
rapc_line_loop: CALL send_CR
LOAD s0, s5 ;display 16-bit address
CALL send_hex_byte
LOAD s0, s4
CALL send_hex_byte
CALL send_space
CALL send_space
rapc_data_loop: CALL send_space
CALL read_byte_slow ;read data into s3
CALL compute_CRC16 ;compute CRC for value in 's3'
OUTPUT s3, W_word_write_port ;store as 'M' word
LOAD s0, s3 ;display byte
CALL send_hex_byte
ADD s4, 01 ;increment address
ADDCY s5, 00
TEST s4, 07 ;test for 8-byte boundary
JUMP NZ, rapc_data_loop
COMPARE s4, 20 ;test for last address
JUMP NZ, rapc_line_loop
CALL send_CR
;
;Read one byte that should be value FF hex
;
CALL read_byte_slow ;read data into s3
CALL compute_CRC16 ;compute CRC for value in 's3'
LOAD s0, s3 ;display byte
CALL send_hex_byte
CALL send_CR
CALL read_send_test_CRC16 ;read, display and test CRC value
;
;Complete table by stroring M9 through to M15
;
LOAD s0, FF ;W9 = FF FF FF FF
LOAD s1, 04
store_W9: OUTPUT s0, W_word_write_port
SUB s1, 01
JUMP NZ, store_W9
;
LOAD s0, 40 ;W10 begins with 40 for page 0
OUTPUT s0, W_word_write_port
;
;W10 ends with family code and serial number 0 and 1.
;W11 is formed of serial number 2, 3, 4 and 5.
;All of this information is in PicoBlaze memory having been read by the
;read ROM command.
;
LOAD s1, family_code ;pointer to memory
LOAD s2, 07 ;7 bytes to read and store
next_M10_M11: FETCH s0, (s1)
OUTPUT s0, W_word_write_port
ADD s1, 01 ;increment pointer
SUB s2, 01
JUMP NZ, next_M10_M11
;
LOAD s0, secret4 ;store M12 (secret 4, 5, 6 and 7) in Wt buffer
OUTPUT s0, W_word_write_port
LOAD s0, secret5
OUTPUT s0, W_word_write_port
LOAD s0, secret6
OUTPUT s0, W_word_write_port
LOAD s0, secret7
OUTPUT s0, W_word_write_port
;
FETCH s0, scratchpad4 ;M13 uses scratchpad 4, 5, and 6 and '80' hex
OUTPUT s0, W_word_write_port
FETCH s0, scratchpad5
OUTPUT s0, W_word_write_port
FETCH s0, scratchpad6
OUTPUT s0, W_word_write_port
LOAD s0, 80
OUTPUT s0, W_word_write_port
;
LOAD s0, 00 ;W14 = 00 00 00 00 W15 = 00 00 01 B8
LOAD s1, 06
store_W14_W15: OUTPUT s0, W_word_write_port
SUB s1, 01
JUMP NZ, store_W14_W15
LOAD s0, 01
OUTPUT s0, W_word_write_port
LOAD s0, B8
OUTPUT s0, W_word_write_port
;
;Compute the SHA-1 algorithm at the same time that the DS2432 is also computing (2ms).
;
CALL compute_sha1
;
;The 160-bit Message Authentication Code is read from the DS2432 as 20 bytes
;and compared with the concatenation of variables E, D, C, B and A in that order
;with each variable received from the DS2432 least significant byte first.
;Each received byte is also used to form a 16-bit CRC value which is tested to
;reveal any communication errors.
;
;
CALL send_mac ;display 'mac='
CALL clear_CRC16 ;prepare CRC registers [sE,sD]
LOAD sC, 00 ;Clear byte match counter
LOAD sB, var_E0 ;start match with LS-Byte of variable 'E'
mac_match_var: LOAD sA, 04 ;4 bytes to match in each variable
mac_match_byte: FETCH s9, (sB) ;read variable byte from local SHA-1
CALL read_byte_slow ;read DS2432 byte into s3
CALL compute_CRC16 ;compute CRC for value in 's3'
COMPARE s3, s9 ;compare MAC values
JUMP NZ, display_mac_byte ;count matching bytes
ADD sC, 01 ;decrement match counter
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -