sha1prog.rmh

来自「和picoblaze完全兼容的mcu ip core」· RMH 代码 · 共 1,559 行 · 第 1/5 页

RMH
1,559 行
字号
// #611: ;// #612: ;// #613: ; Preparing the table.// #614: ;// #615: ; The table is stored in the external 'Wt' buffer and must first be initialised with the// #616: ; 16 'M' words (32-bit words each requiring 4 bytes). This is achieved by shifting in// #617: ; each word in sequence. Storing each word most significant byte first is a natural// #618: ; fit with the reading of the page data from the DS2432 and the way each 'M' word// #619: ; is organised. Notice how this causes least significant bytes to be swapped with most// #620: ; significant bytes!// #621: ;// #622: ;          [31:24]      [23:16]      [15:8]       [7:0]// #623: ;// #624: ;   M0 = [secret0    , secret1    , secret2    , secret3    ]// #625: ;   M1 = [page_data0 , page_data1 , page_data2 , page_data3 ]// #626: ;   M2 = [page_data4 , page_data5 , page_data6 , page_data7 ]// #627: ;   M3 = [page_data8 , page_data9 , page_data10, page_data11]// #628: ;   M4 = [page_data12, page_data13, page_data14, page_data15]// #629: ;   M5 = [page_data16, page_data17, page_data18, page_data19]// #630: ;   M6 = [page_data20, page_data21, page_data22, page_data23]// #631: ;   M7 = [page_data24, page_data25, page_data26, page_data27]// #632: ;   M8 = [page_data28, page_data29, page_data30, page_data31]// #633: ;   M9 = [   FF      ,    FF      ,    FF      ,    FF      ]// #634: ;  M10 = [   40      ,    33      , serial_num0, serial_num1]// #635: ;  M11 = [serial_num2, serial_num3, serial_num4, serial_num5]// #636: ;  M12 = [secret4    , secret5    , secret6    , secret7    ]// #637: ;  M13 = [scratchpad4, scratchpad5, scratchpad6,    80      ]// #638: ;  M14 = [   00      ,    00      ,    00      ,    00      ]// #639: ;  M15 = [   00      ,    00      ,    01      ,    B8      ]// #640: ;// #641: ; In M10, the '33' is the family code and the '40' is made up of a constant bit// #642: ; pattern '0100' and then bits [8:5] of the page address. This gives 4 possible values// #643: ; for this byte during a Read Authenticated Page Command, but this routine is currently// #644: ; fixed to work with page 0 only.// #645: ;        40 - page 0// #646: ;        41 - page 1// #647: ;        42 - page 2// #648: ;        43 - page 3// #649: ;// #650: ; M13 contains the 3 byte challenge from the scratch pad memory. This assumes that a// #651: ; read scratchpad command has previously been used and the bytes held in the DS2432// #652: ; scratchpad match those held in the PicoBlaze memory.// #653: ;// #654: ;// #655: ; The 160-bit Message Authentication Code (MAC) is computed from the table using the SHA-1// #656: ; algorithm. This algorithm actually results in 5 variables 'A', 'B', 'C', 'D' and 'E'// #657: ; which are 32-bit values each formed of 4 bytes. The MAC is the concatenation of// #658: ; the variables. To match the same order in which the Read Authenticated Page Command// #659: ; sends the MAC, the variables must be read in the order 'E', 'D', 'C', 'B' and 'A' and// #660: ; with the least significant byte of each variable first.// #661: ;// #662: ;// #663: ;// #664: ;// #665: ;// @09d #666: [read_auth_page_command]00001 // @09d #666: LOAD(s0,secret0) ;store M0 (secret 0, 1, 2 and 3) in Wt buffer.2c010 // @09e #667: OUTPUT(s0,W_word_write_port)00023 // @09f #668: LOAD(s0,secret1)2c010 // @0a0 #669: OUTPUT(s0,W_word_write_port)00045 // @0a1 #670: LOAD(s0,secret2)2c010 // @0a2 #671: OUTPUT(s0,W_word_write_port)00067 // @0a3 #672: LOAD(s0,secret3)2c010 // @0a4 #673: OUTPUT(s0,W_word_write_port)// #674: ;// #675: ;Start of DS2432 command// #676: ;3020b // @0a5 #677: CALL(clear_CRC16) ;prepare CRC registers [sE,sD]003a5 // @0a6 #678: LOAD(s3,A5) ;read authenticated page command3024e // @0a7 #679: CALL(write_byte_slow) ;transmit command3020e // @0a8 #680: CALL(compute_CRC16) ;compute CRC for value in 's3'00500 // @0a9 #681: LOAD(s5,0) ;set address for page 000400 // @0aa #682: LOAD(s4,0) ;  [TA2,TA1]=0000 hex01340 // @0ab #683: LOAD(s3,s4) ;transmit TA13024e // @0ac #684: CALL(write_byte_slow)3020e // @0ad #685: CALL(compute_CRC16) ;compute CRC for value in 's3'01350 // @0ae #686: LOAD(s3,s5) ;transmit TA23024e // @0af #687: CALL(write_byte_slow)3020e // @0b0 #688: CALL(compute_CRC16) ;compute CRC for value in 's3'// #689: ;// #690: ;Read 32-bytes of data associated with page 0// #691: ;Store these as M1 through to M8// #692: ;// @0b1 #693: [rapc_line_loop]302ec // @0b1 #693: CALL(send_CR)01050 // @0b2 #694: LOAD(s0,s5) ;display 16-bit address302c2 // @0b3 #695: CALL(send_hex_byte)01040 // @0b4 #696: LOAD(s0,s4)302c2 // @0b5 #697: CALL(send_hex_byte)302ef // @0b6 #698: CALL(send_space)302ef // @0b7 #699: CALL(send_space)// @0b8 #700: [rapc_data_loop]302ef // @0b8 #700: CALL(send_space)3026f // @0b9 #701: CALL(read_byte_slow) ;read data into s33020e // @0ba #702: CALL(compute_CRC16) ;compute CRC for value in 's3'2c310 // @0bb #703: OUTPUT(s3,W_word_write_port) ;store as 'M' word01030 // @0bc #704: LOAD(s0,s3) ;display byte302c2 // @0bd #705: CALL(send_hex_byte)18401 // @0be #706: ADD(s4,1) ;increment address1a500 // @0bf #707: ADDCY(s5,0)12407 // @0c0 #708: TEST(s4,7) ;test for 8-byte boundary354b8 // @0c1 #709: JUMP(NZ,rapc_data_loop)14420 // @0c2 #710: COMPARE(s4,32) ;test for last address354b1 // @0c3 #711: JUMP(NZ,rapc_line_loop)302ec // @0c4 #712: CALL(send_CR)// #713: ;// #714: ;Read one byte that should be value FF hex// #715: ;3026f // @0c5 #716: CALL(read_byte_slow) ;read data into s33020e // @0c6 #717: CALL(compute_CRC16) ;compute CRC for value in 's3'01030 // @0c7 #718: LOAD(s0,s3) ;display byte302c2 // @0c8 #719: CALL(send_hex_byte)302ec // @0c9 #720: CALL(send_CR)3021c // @0ca #721: CALL(read_send_test_CRC16) ;read, display and test CRC value// #722: ;// #723: ;Complete table by stroring M9 through to M15// #724: ;000ff // @0cb #725: LOAD(s0,FF) ;W9 = FF FF FF FF00104 // @0cc #726: LOAD(s1,4)// @0cd #727: [store_W9]2c010 // @0cd #727: OUTPUT(s0,W_word_write_port)1c101 // @0ce #728: SUB(s1,1)354cd // @0cf #729: JUMP(NZ,store_W9)// #730: ;00040 // @0d0 #731: LOAD(s0,64) ;W10 begins with 40 for page 02c010 // @0d1 #732: OUTPUT(s0,W_word_write_port)// #733: ;// #734: ;W10 ends with family code and serial number 0 and 1.// #735: ;W11 is formed of serial number 2, 3, 4 and 5.// #736: ;All of this information is in PicoBlaze memory having been read by the// #737: ;read ROM command.// #738: ;00100 // @0d2 #739: LOAD(s1,family_code) ;pointer to memory00207 // @0d3 #740: LOAD(s2,7) ;7 bytes to read and store// @0d4 #741: [next_M10_M11]07010 // @0d4 #741: FETCH(s0,s1)2c010 // @0d5 #742: OUTPUT(s0,W_word_write_port)18101 // @0d6 #743: ADD(s1,1) ;increment pointer1c201 // @0d7 #744: SUB(s2,1)354d4 // @0d8 #745: JUMP(NZ,next_M10_M11)// #746: ;00089 // @0d9 #747: LOAD(s0,secret4) ;store M12 (secret 4, 5, 6 and 7) in Wt buffer2c010 // @0da #748: OUTPUT(s0,W_word_write_port)000ab // @0db #749: LOAD(s0,secret5)2c010 // @0dc #750: OUTPUT(s0,W_word_write_port)000cd // @0dd #751: LOAD(s0,secret6)2c010 // @0de #752: OUTPUT(s0,W_word_write_port)000ef // @0df #753: LOAD(s0,secret7)2c010 // @0e0 #754: OUTPUT(s0,W_word_write_port)// #755: ;06020 // @0e1 #756: FETCH(s0,scratchpad4) ;M13 uses scratchpad 4, 5, and 6 and '80' hex2c010 // @0e2 #757: OUTPUT(s0,W_word_write_port)06021 // @0e3 #758: FETCH(s0,scratchpad5)2c010 // @0e4 #759: OUTPUT(s0,W_word_write_port)06022 // @0e5 #760: FETCH(s0,scratchpad6)2c010 // @0e6 #761: OUTPUT(s0,W_word_write_port)00080 // @0e7 #762: LOAD(s0,128)2c010 // @0e8 #763: OUTPUT(s0,W_word_write_port)// #764: ;00000 // @0e9 #765: LOAD(s0,0) ;W14 = 00 00 00 00   W15 = 00 00 01 B800106 // @0ea #766: LOAD(s1,6)// @0eb #767: [store_W14_W15]2c010 // @0eb #767: OUTPUT(s0,W_word_write_port)1c101 // @0ec #768: SUB(s1,1)354eb // @0ed #769: JUMP(NZ,store_W14_W15)00001 // @0ee #770: LOAD(s0,1)2c010 // @0ef #771: OUTPUT(s0,W_word_write_port)000b8 // @0f0 #772: LOAD(s0,B8)2c010 // @0f1 #773: OUTPUT(s0,W_word_write_port)// #774: ;// #775: ;Compute the SHA-1 algorithm at the same time that the DS2432 is also computing (2ms).// #776: ;30115 // @0f2 #777: CALL(compute_sha1)// #778: ;// #779: ;The 160-bit Message Authentication Code is read from the DS2432 as 20 bytes// #780: ;and compared with the concatenation of variables E, D, C, B and A in that order// #781: ;with each variable received from the DS2432 least significant byte first.// #782: ;Each received byte is also used to form a 16-bit CRC value which is tested to// #783: ;reveal any communication errors.// #784: ;// #785: ;303e7 // @0f3 #786: CALL(send_mac) ;display 'mac='3020b // @0f4 #787: CALL(clear_CRC16) ;prepare CRC registers [sE,sD]00c00 // @0f5 #788: LOAD(sC,0) ;Clear byte match counter00b18 // @0f6 #789: LOAD(sB,var_E0) ;start match with LS-Byte of variable 'E'// @0f7 #790: [mac_match_var]00a04 // @0f7 #790: LOAD(sA,4) ;4 bytes to match in each variable// @0f8 #791: [mac_match_byte]079b0 // @0f8 #791: FETCH(s9,sB) ;read variable byte from local SHA-13026f // @0f9 #792: CALL(read_byte_slow) ;read DS2432 byte into s33020e // @0fa #793: CALL(compute_CRC16) ;compute CRC for value in 's3'15390 // @0fb #794: COMPARE(s3,s9) ;compare MAC values354fe // @0fc #795: JUMP(NZ,display_mac_byte) ;count matching bytes18c01 // @0fd #796: ADD(sC,1) ;decrement match counter// @0fe #797: [display_mac_byte]01030 // @0fe #797: LOAD(s0,s3) ;display byte302c2 // @0ff #798: CALL(send_hex_byte)302ef // @100 #799: CALL(send_space)1ca01 // @101 #800: SUB(sA,1) ;counts bytes per variable35105 // @102 #801: JUMP(Z,next_mac_var)18b01 // @103 #802: ADD(sB,1)340f8 // @104 #803: JUMP(mac_match_byte)// @105 #804: [next_mac_var]14b0b // @105 #804: COMPARE(sB,var_A3) ;test for last byte35109 // @106 #805: JUMP(Z,report_mac)1cb07 // @107 #806: SUB(sB,7) ;point to next variable340f7 // @108 #807: JUMP(mac_match_var)// #808: ;// #809: ;MAC has passed if all 20 bytes matched// #810: ;// @109 #811: [report_mac]302ec // @109 #811: CALL(send_CR)14c14 // @10a #812: COMPARE(sC,20) ;20 bytes should have matched3550e // @10b #813: JUMP(NZ,mac_fail)303ad // @10c #814: CALL(send_Pass)3410f // @10d #815: JUMP(read_mac_CRC)// @10e #816: [mac_fail]303b4 // @10e #816: CALL(send_Fail)// #817: ;// #818: ;Next two bytes received are the 16-bit CRC// #819: ;Read 16-bit CRC into [s5,s4] and send value to UART// #820: ;// @10f #821: [read_mac_CRC]3021c // @10f #821: CALL(read_send_test_CRC16) ;read, display and test CRC value// #822: ;// #823: ;Read one byte that should be value AA hex.// #824: ;  Would actually read AA hex continuously until master reset// #825: ;3026f // @110 #826: CALL(read_byte_slow) ;read data into s301030 // @111 #827: LOAD(s0,s3) ;display byte302c2 // @112 #828: CALL(send_hex_byte)302ec // @113 #829: CALL(send_CR)// #830: ;34003 // @114 #831: JUMP(warm_start)// #832: ;// #833: ;// #834: ;**************************************************************************************// #835: ; Compute SHA-1 Algorithm.// #836: ;**************************************************************************************// #837: ;// #838: ; Computes the SHA-1 algorithm based on the initial table of values (M0 through to M15)// #839: ; which are stored in the external Wt buffer.// #840: ;// #841: ; The SHA-1 algorithms uses 5 variables called 'A', 'B', 'C', 'D' and 'E'. Each variable// #842: ; is 32-bits and stored as 4 bytes in PicoBlaze scratch pad memory. The locations must// #843: ; be defined using constants 'var_A0' thought to 'var_E3' in ascending locations.// #844: ;// #845: ; Constants must also be used to define access to the external Wt buffer.// #846: ;// #847: ; During this process, register 'sE' is used to count iterations from 0 to 79 (4F hex).// #848: ; Other registers are consistently grouped as follows to support 32-bit operations.// #849: ;// #850: ; Register set [s5,s4,s3,s2] is used as a temporary 32-bit word// #851: ; Register set [s9,s8,s7,s6] is used as a temporary 32-bit word// #852: ; Register set [sD,sC,sB,sA] is used as a temporary 32-bit word// #853: ;// #854: ;// #855: ; Initialise the 32-bit variables// #856: ;// #857: ;// @115 #858: [compute_sha1]00001 // @115 #858: LOAD(s0,1) ;A=674523012e008 // @116 #859: STORE(s0,var_A0)00023 // @117 #860: LOAD(s0,35)2e009 // @118 #861: STORE(s0,var_A1)00045 // @119 #862: LOAD(s0,69)2e00a // @11a #863: STORE(s0,var_A2)00067 // @11b #864: LOAD(s0,103)2e00b // @11c #865: STORE(s0,var_A3)00089 // @11d #866: LOAD(s0,137) ;B=EFCDAB892e00c // @11e #867: STORE(s0,var_B0)000ab // @11f #868: LOAD(s0,AB)2e00d // @120 #869: STORE(s0,var_B1)000cd // @121 #870: LOAD(s0,CD)2e00e // @122 #871: STORE(s0,var_B2)000ef // @123 #872: LOAD(s0,EF)2e00f // @124 #873: STORE(s0,var_B3)000fe // @125 #874: LOAD(s0,FE) ;C=98BADCFE2e010 // @126 #875: STORE(s0,var_C0)000dc // @127 #876: LOAD(s0,DC)2e011 // @128 #877: STORE(s0,var_C1)000ba // @129 #878: LOAD(s0,BA)2e012 // @12a #879: STORE(s0,var_C2)00098 // @12b #880: LOAD(s0,152)2e013 // @12c #881: STORE(s0,var_C3)00076 // @12d #882: LOAD(s0,118) ;D=103254762e014 // @12e #883: STORE(s0,var_D0)00054 // @12f #884: LOAD(s0,84)2e015 // @130 #885: STORE(s0,var_D1)00032 // @131 #886: LOAD(s0,50)2e016 // @132 #887: STORE(s0,var_D2)00010 // @133 #888: LOAD(s0,16)2e017 // @134 #889: STORE(s0,var_D3)000f0 // @135 #890: LOAD(s0,F0) ;E=C3D2E1F02e018 // @136 #891: STORE(s0,var_E0)000e1 // @137 #892: LOAD(s0,E1)2e019 // @138 #893: STORE(s0,var_E1)000d2 // @139 #894: LOAD(s0,D2)2e01a // @13a #895: STORE(s0,var_E2)000c3 // @13b #896: LOAD(s0,C3)2e01b // @13c #897: STORE(s0,var_E3)// #898: ;// #899: ;00e00 // @13d #900: LOAD(sE,0) ;reset iteration counter// #901: ;// #902: ;// #903: ;Compute ft(B,C,D) in register set [s5,s4,s3,s2] and then add constant Kt.// #904: ;// #905: ;Iterations 0 to 19 - process type 1// #906: ;   ft = (B and C) or ((not B) and D)// #907: ;  Then the constant Kt=5A827999 will be added// #908: ;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?