**110020015劉祐瑋 Hardware Security PA2- Hardware Trojan Insertion** Hackmd link: https://hackmd.io/lrtCuxCKR2-fLt1K_AMiUg?view 完成度:100% - [x] Implement an AES-128 cryptographic engine - [x] Implement the hardware trojan you designed in this system - [x] Sample hardware trojan - [x] Reimplement the hardware trojan - [x] Design a novel hardware trojan and implement: (bonus) 目錄: [TOC] # Backgroug Hardware Trojans are covert modifications in integrated circuits, posing security risks in modern chip design by potentially compromising function, confidentiality, or performance. Involving various entities like SoC integrators and foundries, the complex supply chain heightens the risk of these Trojans, which are hard to detect and remove. Hardware Trojans consist of a trigger, which monitors signals and activates under certain conditions, and a payload that can alter behavior, degrade performance, leak information, or cause service denial. They can be either combinational or sequential, based on the trigger mechanism. # AES Specification We first design a simple AES system, which will be used to implant various Trojans in the next section. The following figure is a schematic diagram of AES. ![image](https://hackmd.io/_uploads/HJZYIMLGA.png) The encryption process involves 10 rounds of SubBytes, ShiftRows, MixColumns, and AddRoundKey, with each round using the key to transform the data block. The final round does not include the MixColumns operation. Eventually, the data block undergoes multiple rounds of operations and is output as encrypted data. I will describe the function of each block in detail below. ## AES_128 ### 輸入與輸出介面 | Signal Name | I/O | Width | Simple Description| | -------- | -------- | -------- |----------| | clk | I | 1 | 本系統為同步於時脈正緣之同步設計。 | | state | I | 128 | 要加密的信號 | | key | I | 128 | key | | out | I | 128 | 加密後的結果| |rst| I|1|當0時reset| ### code 簡單介紹 ![image](https://hackmd.io/_uploads/SyIg9MLGR.png =25%x) 我們這邊如上面描述的一開始會先如圖一的部分做XOR(上圖紅色框框),接者data s0會經有9個one round module 得到 data s8 ,最後data s8 會進入final round產生出data out(正式加密後的答案)。除此之外每一round會對應到不同的key(k0b,k1b,k2b...都是經由expand_key module所產生的結果) ## expand_key_128 module ### 功能說明: AES(高階加密標準)的 expand_key_128 模組主要負責在 AES 加密過程中的密鑰擴展。在 AES 中,一個較短的密鑰會被擴展成多個輪密鑰,這些輪密鑰在 AES 加密算法的每一輪中使用。以下為expand_key過程的說明: - Initial Key:AES 從一個 128 位的密鑰(16 位元組)開始,這個密鑰作為第一輪加密的初始密鑰。 - Key Expansion:expand_key_128 模組將這個 128 位的密鑰擴展成一個密鑰調度陣列,用於每一輪的加密。密鑰擴展過程使用字節替換、行移位和輪常數加法的組合來生成每一輪的新的 128 位區塊。 - S-Box Substitution:擴展過程的一部分涉及使用 AES S 盒進行字節替換,通過使密鑰與擴展密鑰之間的關係非線性,增強了安全性。 - Round Constants:在擴展過程中生成的每個新密鑰都與一個輪常數(Rcon)進行異或運算。 - 輸出:expand_key_128 模組的輸出是一系列 128 位的密鑰,每一輪加密(加上一個額外的初輪)各用一個。對於 AES-128,它有 10 輪加密,因此總共需要 11 個輪密鑰。 ### I/O interface: | Signal Name | I/O | Width | Simple Description| | -------- | -------- | -------- |----------| | clk | I | 1 | clk | | in | I | 128 | 要進行expand的key | |out_1|O|128|用來下一round expand key所需要的key| |out_2|O|128|用來這一 round加密的key| |rcon|I|8|Round Constants| ### code 簡單介紹: 這一部分不在本次實做範圍內就不做過多介紹。 ## One_round module ### 功能說明: In the AES (Advanced Encryption Standard) encryption process, the "one round module" refers to a complete encryption cycle within the procedure. AES-128 involves 10 such rounds, each incorporating the following four main steps: - SubBytes: This step provides the encryption's non-linear transformation capability. It involves substituting each byte of the state with the corresponding byte from the S-box. You need to perform this substitution operation on each byte of the state in every round. We provide module S in table.v, which implements the functionality of S-box transformation. Please use this function to complete the SubBytes step. - ShiftRows: This step involves cyclically shifting each row of the state to the left. After ShiftRows, each column in the matrix is composed of elements from different rows in the input matrix. - MixColumns: In this step, each column's four bytes are combined through a linear transformation. This step operates on the columns of the state by performing matrix multiplication to mix the data. We provide module xS in table.v, which simplifies the matrix multiplication for MixColumns. Please use this function to complete the MixColumns step. - AddRoundKey: In this step, the round key is XORed with the original matrix. In each encryption iteration, a round key is generated from the main key, matching the size of the original matrix. This round key is then XORed with each corresponding byte in the original matrix. ### I/O interface: | Signal Name | I/O | Width | Simple Description| | -------- | -------- | -------- |----------| | clk | I | 1 | clk| | state_in | I | 128 | 每個round要加密的data | |key_in|I|128|每個round加密所需要的key| |state_out|O|128|每個round加密後的data| ### code 簡介: ![image](https://hackmd.io/_uploads/S1JfJ4Iz0.png =30%x) state_in會先進入Subbyte產生state1。 state1再進入ShiftRows 產生state2。 state2再進入MixColumns 產生state3。 最後state3與key進入addRoundkey就會產生state_out。 #### sub-module: SubByte 因為本次作業有提供實現Subyte的 module S,我們並不需要特別實做這部分,我們只需要利用此module來實現。那因為module S 的input data只有 8bit所以如下圖所示,我們需要把128 bit的data拆成16個8bit data做運算。 ![image](https://hackmd.io/_uploads/S17mg4IM0.png =30%x) #### sub-module: ShiftRows 我們需要實現Shift row,而根據不同的位置會有不同的偏移量如下圖所示。 ![image](https://hackmd.io/_uploads/BJY3b4UfA.png =50%x) 根據每個bit所對應到的位置(如下圖),就可以利用圖中的 coding 方式來完成設計。 ![image](https://hackmd.io/_uploads/rJ4oMEUf0.png =25%x) ![image](https://hackmd.io/_uploads/BJF8GNLf0.png =30%x) #### sub-module: MixColumns 因為本次作業有提供實現MixColumns的module xS,我們並不需要特別實做這部分,我們只需要利用此module來實現。但因為module xS 的input data只有 8bit所以如下圖所示,我們需要把128 bit的data拆成16個8bit data做運算。 ![image](https://hackmd.io/_uploads/H1DdW4LMR.png =30%x) #### sub-module: AddRound key 這部分就只是把state與key做XOR而已,coding 方式如下圖: ![image](https://hackmd.io/_uploads/r1s7XVLfR.png =30%x) ## Final round module 這部分與one_round module類似,只是少了Mixcolumn的module,所以我就不做過多的說明。 ![image](https://hackmd.io/_uploads/HylyNNLzR.png =30%x) ## Testbench and The Result 這部分主要利用助教所提供的tb來做測試。這裡我會先解釋tb內容然後再來說明結果。 在介紹之前,我在tb中加入了dumpfile可以產生vcd檔可以用來觀察波型。 ![image](https://hackmd.io/_uploads/SkNo8EUzC.png =30%x) 除此之外,我寫了兩個script檔來跑vivado xsim。 run_xsim 執行simulation、run_clean清除執行結果。 ![image](https://hackmd.io/_uploads/SyJYDVLGC.png) 圖:run_xsim 內容 ![image](https://hackmd.io/_uploads/B1gcvVIGR.png) 圖:run_clean 內容 只需在terminal 端打下以下指令就可以執行: ``` ./run_xsim source run_clean ``` tb 介紹: - rst : 系統開始前會先reset。 ![image](https://hackmd.io/_uploads/Skk8dVIM0.png) - counter: 這裡會有兩個counter,第一個counter代表執行的時間,從0數到31代表完整的做完一次AES,第二個counter1代表我要執行的case次數,且會根據不同的counter1給定不同的test pattern ![image](https://hackmd.io/_uploads/BysJKVIfA.png =30%x) 圖:每個cycle counter+1而每做完一次完整的AES就counter1就+1。 ![image](https://hackmd.io/_uploads/BkIeY48z0.png =30%x) 圖:根據不同的counter1給定不同的test pattern。 ![image](https://hackmd.io/_uploads/HkE-FEUMR.png =30%x) 圖:利用counter1決定我要測試幾筆data,這裡是測試3筆。 - get result:利用counter數到31時讀取當下的data。 ![image](https://hackmd.io/_uploads/rJo0KV8zA.png =30%x) Result 結果: - terminal 端: 根據下圖結果符合助教所提供的test pattern。 ![image](https://hackmd.io/_uploads/S1e-JHLMA.png) - waveform-gtkwave: - 可以發現每當counter數到31,就會輸入下一個test pattern。 ![image](https://hackmd.io/_uploads/ry0L-SIzR.png) - test bench會讀取當counter等於31的data值 ![image](https://hackmd.io/_uploads/SJtbGS8fC.png) - 我們可以觀察每一個round的output也同時可以知道他需要花多少時間完成10 round,由下圖可以知道one_round是每2cycle出來,而final_round是1個cycle,完成一次AES的lantency為20 cycles (所以我們其實可以不用等到 counter數到31再去讀。) ![image](https://hackmd.io/_uploads/r1naQrLMC.png) - 除此之外我們也可以觀察到每一round的state跟對應的key,經由.vcd檔,如上圖就是state在不同round的output。 # Sample hardware trojan ## Trojan Specification 我們需要設計如下圖的Trojan,他的trigger 訊號是當input state=128'b1時,payload MUX會直接選擇Key輸出,反之則正常輸出加密state。 ![image](https://hackmd.io/_uploads/HyHvcSLz0.png) ## Implement 我們可以直接在aes_128 module中加入MUX,來實現。 ![image](https://hackmd.io/_uploads/Bk2jcSLfR.png) ## Result 我們利用上面的tb來測試(跟改其中一組test pattern的state為128'b1),可以發現如果state=128'b1時,Trojan 會被啟動,並且直接洩漏key。 ![image](https://hackmd.io/_uploads/By0BsH8M0.png) # Reimplement the hardware trojan 根據 A trojan framework in AES core to evade state-of-the-art HT detection schemes這篇論文做trojan。 ## Trojan Specification 這篇論文提到我們可以在第9個one_round以及final_round之間加一個trojan,因為final沒有mixcolumn所以可以經由改變進入Subbyte的值來讓output洩漏部分的key。如下圖我們可以知道當今天如果讓state==8'h52,after Substitution時就會變00(這部分可以由module s 得知。),這時如果跟key做XOR就會洩漏key。 ![image](https://hackmd.io/_uploads/SJJ7VULGR.png) 圖:論文示意圖 ![image](https://hackmd.io/_uploads/H19BLLLfR.png) 圖:code in module s 那要如何讓XY變成8'h52呢,可以經由下圖的trigger跟payload。When i = a and j = b(< i, j > is position of the current processing element, < a, b > is the vulnerable position generated by the 5 bit LFSR ), the adversary forcefully changes the value of (XY)16 to (52)16 before the Substitution operation. ![image](https://hackmd.io/_uploads/S1CyrUIMC.png) 圖:trigger ans payload ## Implement 首先,我們要在r9與final_round之間加入Trojan。 ![image](https://hackmd.io/_uploads/r1s8wULf0.png =30%x) Trojan設計: - LFSR:這是一個簡單的LFSR,我們會利用他的least significant bit[3:0]來當the vulnerable position。 ![image](https://hackmd.io/_uploads/SJHnDL8fR.png =40%x) - Counter:因為我們是combination circuit,我們是一口氣計算16個element,所以我利用counter來模擬每個cycle對應到操作不同的位置。 ![image](https://hackmd.io/_uploads/Hkp6_IUfC.png =40%x) - Triger condition:我們要對操作位置與the vulnerable position 做xor。 ![image](https://hackmd.io/_uploads/r1cmwYOMA.png) - Payloaf設計:當ABCD 為4'b1111時當下的的操作位置的data要改為8'h52,反之則正常。 ![image](https://hackmd.io/_uploads/B1T7K88MC.png =70%x) ## Result 首先我修改了tb,這裡會有兩個module,一個AES是原先的設計,AES_top是加了Trojan的設計。 ![image](https://hackmd.io/_uploads/HkDsd58GR.png) 並且在讀取data時,會比較兩者的output如果不一樣就是被detected到了。 ![image](https://hackmd.io/_uploads/ByJ-SdLfA.png =30%x) ![image](https://hackmd.io/_uploads/ry0dL_UzR.png =30%x) 左圖:tb右圖:可以發現只有影響其中的16bit,符合我們的Spec。 而且可以發現這個trojan跟input的state或key沒有關係,而是跟time有關,我們可以參考waveform得知這件事情。 ![image](https://hackmd.io/_uploads/HyHbPuIMC.png) 圖:可以發現不同的cycle時,可能會造成out1與out不同,所以是看取出來的cycle當下是否會被detect到。 雖然每個cycle會造成不同的output,看似很容易就detected到,但我們在讀取時是固定的cycle count來讀取,所以機率就如論文所敘的會是1/16,這裡我模擬了64次AES其中有4次被偵測到,這也印證了論文中所提到的1/16的機率。 ![image](https://hackmd.io/_uploads/rkMl8dLfC.png =30%x) ![image](https://hackmd.io/_uploads/SJYJIO8zA.png =30%x) 左圖:tb。右圖:result on terminal 甚至如果我改cycle count也還會是一樣的結果。 ![image](https://hackmd.io/_uploads/HyV4u_UzR.png =30%x) ![image](https://hackmd.io/_uploads/Hk7HOdUzR.png =30%x) 左圖:tb。右圖:result on terminal # novel hardware trojan ## Trojan Specification 這裡我設置了一個Seq trojan當今天FSM中的tri_state==Tri時會觸發MUX選擇key直接當output。而 input state作為條件來判斷next_tri_state。下圖為FSM以及整個架構圖。 ![image](https://hackmd.io/_uploads/rkqZNcLzC.png) ## Implement MUX: 當trigger為1'b1時會直接輸出key。 ![image](https://hackmd.io/_uploads/r1fYKuIMC.png =30%x) state以及triger設計: ![image](https://hackmd.io/_uploads/Hyi5FOUzC.png =50%x) next_state設計: 當input state符合特定data時就會進入下一個state。 ![image](https://hackmd.io/_uploads/HyF3Fu8GA.png =50%x) 圖:這裡並不是完整的code,只截圖部份用來說明。 ## Result 這裡tb跟上面一樣,利用正常AES與加入trojan的AES進行比較,我這裡test 8筆data,除此之外我故意給的test pattern符合我的trigger才可以觸發。 ![image](https://hackmd.io/_uploads/BkcK9d8M0.png =50%x) terminal:結果 ![image](https://hackmd.io/_uploads/BJk65dIfR.png =30%x) 也可以從waveform發現當今天tri_state順利進入到Tri,就會拉高trigger導致out直接變成key。 ![image](https://hackmd.io/_uploads/r1zSjO8fR.png) # 結論 以下總結了本次實驗的主要成果和觀察: - AES-128 加密引擎實現: 成功實現了一個基本的 AES-128 加密系統,包括所有標準的加密操作如 SubBytes、ShiftRows、MixColumns 和 AddRoundKey。這為後續的木馬植入提供了一個穩固的基礎。 - 硬件木馬設計與實現: - 基本木馬:第一個木馬示例展示了當輸入狀態為128'b1 時,直接將密鑰作為輸出的簡單木馬。此木馬雖然基本,但清楚地指出了密鑰洩漏的風險。 - 重製木馬:根據專門文獻提出的概念,我們在 AES 的最後兩個階段之間插入了一個更複雜的木馬,該木馬能在特定條件下修改數據,從而洩露部分密鑰資訊。這證明了即使在加密流程的後期,也能有效地植入木馬。 - 創新木馬設計:設計了一個序列型木馬,通過內部狀態機的特定狀態觸發,改變輸出多工器的行為來直接輸出密鑰。這種設計展示了即使在密集的邏輯設計中,也可能隱藏深層的安全威脅。 - 測試與分析: 透過綜合使用助教提供的tb檢測基準和自行設計的腳本,我們對所有的木馬設計進行了徹底的功能和性能測試。測試結果證明了木馬的潛在影響,並揭示了其在特定條件下的行為模式。 - 本次作業困難點: 我認為重現論文內容較有挑戰性,因為原先論文是利用每次的操作位置來做XOR,但因為我們是利用comb circuit所以一次是做所有位置,所以我最後決定利用counter來模擬不同時間時不同的操作位置。