📄 sha1prog.psm
字号:
display_mac_byte: LOAD s0, s3 ;display byte
CALL send_hex_byte
CALL send_space
SUB sA, 01 ;counts bytes per variable
JUMP Z, next_mac_var
ADD sB, 01
JUMP mac_match_byte
next_mac_var: COMPARE sB, var_A3 ;test for last byte
JUMP Z, report_mac
SUB sB, 07 ;point to next variable
JUMP mac_match_var
;
;MAC has passed if all 20 bytes matched
;
report_mac: CALL send_CR
COMPARE sC, 14 ;20 bytes should have matched
JUMP NZ, mac_fail
CALL send_Pass
JUMP read_mac_CRC
mac_fail: CALL send_Fail
;
;Next two bytes received are the 16-bit CRC
;Read 16-bit CRC into [s5,s4] and send value to UART
;
read_mac_CRC: CALL read_send_test_CRC16 ;read, display and test CRC value
;
;Read one byte that should be value AA hex.
; Would actually read AA hex continuously until master reset
;
CALL read_byte_slow ;read data into s3
LOAD s0, s3 ;display byte
CALL send_hex_byte
CALL send_CR
;
JUMP warm_start
;
;
;**************************************************************************************
; Compute SHA-1 Algorithm.
;**************************************************************************************
;
; Computes the SHA-1 algorithm based on the initial table of values (M0 through to M15)
; which are stored in the external Wt buffer.
;
; The SHA-1 algorithms uses 5 variables called 'A', 'B', 'C', 'D' and 'E'. Each variable
; is 32-bits and stored as 4 bytes in PicoBlaze scratch pad memory. The locations must
; be defined using constants 'var_A0' thought to 'var_E3' in ascending locations.
;
; Constants must also be used to define access to the external Wt buffer.
;
; During this process, register 'sE' is used to count iterations from 0 to 79 (4F hex).
; Other registers are consistently grouped as follows to support 32-bit operations.
;
; Register set [s5,s4,s3,s2] is used as a temporary 32-bit word
; Register set [s9,s8,s7,s6] is used as a temporary 32-bit word
; Register set [sD,sC,sB,sA] is used as a temporary 32-bit word
;
;
; Initialise the 32-bit variables
;
;
compute_sha1: LOAD s0, 01 ;A=67452301
STORE s0, var_A0
LOAD s0, 23
STORE s0, var_A1
LOAD s0, 45
STORE s0, var_A2
LOAD s0, 67
STORE s0, var_A3
LOAD s0, 89 ;B=EFCDAB89
STORE s0, var_B0
LOAD s0, AB
STORE s0, var_B1
LOAD s0, CD
STORE s0, var_B2
LOAD s0, EF
STORE s0, var_B3
LOAD s0, FE ;C=98BADCFE
STORE s0, var_C0
LOAD s0, DC
STORE s0, var_C1
LOAD s0, BA
STORE s0, var_C2
LOAD s0, 98
STORE s0, var_C3
LOAD s0, 76 ;D=10325476
STORE s0, var_D0
LOAD s0, 54
STORE s0, var_D1
LOAD s0, 32
STORE s0, var_D2
LOAD s0, 10
STORE s0, var_D3
LOAD s0, F0 ;E=C3D2E1F0
STORE s0, var_E0
LOAD s0, E1
STORE s0, var_E1
LOAD s0, D2
STORE s0, var_E2
LOAD s0, C3
STORE s0, var_E3
;
;
LOAD sE, 00 ;reset iteration counter
;
;
;Compute ft(B,C,D) in register set [s5,s4,s3,s2] and then add constant Kt.
;
;Iterations 0 to 19 - process type 1
; ft = (B and C) or ((not B) and D)
; Then the constant Kt=5A827999 will be added
;
;Iterations 20 to 39 and iterations 60 to 79 - process type 2
; ft = B xor C xor D
; Then the constant Kt=6ED9EBA1 will be added for iterations 20 to 39
; Then the constant Kt=CA62C1D6 will be added for iterations 60 to 79
;
;Iterations 40 to 59 - process type 3
; ft = (B and C) or (B and D) or (C and D)
; Then the constant Kt=8F1BBCDC will be added
;
next_sha1_iteration: FETCH s5, var_B3 ;B in [s5,s4,s3,s2]
FETCH s4, var_B2
FETCH s3, var_B1
FETCH s2, var_B0
CALL fetch_C ;C in [s9,s8,s7,s6]
FETCH sD, var_D3 ;D in [sD,sC,sB,sA]
FETCH sC, var_D2
FETCH sB, var_D1
FETCH sA, var_D0
;
;Determine process type
;
COMPARE sE, 14 ;set carry flag for iterations <20
JUMP C, ft_type1
COMPARE sE, 28 ;set carry flag for iterations <40
JUMP C, ft_type2
COMPARE sE, 3C ;set carry flag for iterations <60
JUMP C, ft_type3
;
; ft = B xor C xor D
;
; B xor C = B xor C
; [s5,s4,s3,s2] = [s5,s4,s3,s2] xor [s9,s8,s7,s6]
;
; B xor C xor D = (B xor C) xor D
; [s5,s4,s3,s2] = [s5,s4,s3,s2] xor [sD,sC,sB,sA]
;
;
ft_type2: XOR s5, s9 ;B xor C in [s5,s4,s3,s2]
XOR s4, s8
XOR s3, s7
XOR s2, s6
XOR s5, sD ;(B xor C) xor D in [s5,s4,s3,s2]
XOR s4, sC
XOR s3, sB
XOR s2, sA
COMPARE sE, 3C ;set carry flag for iterations <60
JUMP NC, Kt_CA62C1D6
ADD s2, A1 ;add Kt=6ED9EBA1
ADDCY s3, EB
ADDCY s4, D9
ADDCY s5, 6E
JUMP compute_TMP
Kt_CA62C1D6: ADD s2, D6 ;add Kt=CA62C1D6
ADDCY s3, C1
ADDCY s4, 62
ADDCY s5, CA
JUMP compute_TMP
;
; ft = (B and C) or ((not B) and D)
;
; B and C = C and B
; [s9,s8,s7,s6] = [s9,s8,s7,s6] and [s5,s4,s3,s2]
;
; not B = B xor FFFFFFFF
; [s5,s4,s3,s2] = [s5,s4,s3,s2] xor [FF,FF,FF,FF]
;
; (not B) and D = (not B) and D
; [s5,s4,s3,s2] = [s5,s4,s3,s2] and [sD,sC,sB,sA]
;
; ;(B and C) or ((not B) and D) = ((not B) and D) or (B and C)
; [s5,s4,s3,s2] = [s5,s4,s3,s2] or [s9,s8,s7,s6]
;
ft_type1: AND s9, s5 ;B and C in [s9,s8,s7,s6]
AND s8, s4
AND s7, s3
AND s6, s2
XOR s5, FF ;(not B) in [s5,s4,s3,s2]
XOR s4, FF
XOR s3, FF
XOR s2, FF
AND s5, sD ;((not B) and D) in [s5,s4,s3,s2]
AND s4, sC
AND s3, sB
AND s2, sA
OR s5, s9 ;(B and C) or ((not B) and D) in [s5,s4,s3,s2]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -