-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serializing fence implementation and test
Co-authored-by: Seppe Vanderhallen <seppe.vanderhallen@hotmail.com>
- Loading branch information
1 parent
46534bf
commit d48439e
Showing
11 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package riscv.plugins | ||
|
||
import riscv._ | ||
import spinal.core._ | ||
|
||
class Fence extends Plugin[DynamicPipeline] with FenceService { | ||
|
||
object Data { | ||
object FENCE extends PipelineData(Bool()) | ||
} | ||
|
||
override def setup(): Unit = { | ||
val issuer = pipeline.service[IssueService] | ||
issuer.setDestinations(Opcodes.FENCE, pipeline.rsStages.toSet) | ||
|
||
pipeline.service[DecoderService].configure { config => | ||
config.addDefault( | ||
Map( | ||
Data.FENCE -> False | ||
) | ||
) | ||
|
||
config.addDecoding( | ||
Opcodes.FENCE, | ||
InstructionType.I, | ||
Map( | ||
Data.FENCE -> True | ||
) | ||
) | ||
} | ||
} | ||
|
||
override def isFence(stage: Stage): Bool = { | ||
stage.output(Data.FENCE) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.bin | ||
*_stripped.s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
%.elf: %.s | ||
riscv32-unknown-elf-gcc -mabi=ilp32 -march=rv32im_zicsr -c -o $@ $< | ||
riscv32-unknown-elf-gcc -mabi=ilp32 -march=rv32im_zicsr -ffreestanding -nostdlib -T ../tests.ld -o $@ $< | ||
|
||
%.ihex: %.elf | ||
riscv32-unknown-elf-objcopy -O ihex $< $@ | ||
|
||
%.bin: %.elf | ||
riscv32-unknown-elf-objcopy -O binary $< $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import vcdvcd | ||
import subprocess | ||
import os | ||
|
||
|
||
def evaluate(): | ||
vcd = vcdvcd.VCDVCD("sim.vcd", []) | ||
addresses = vcd["TOP.Core.pipeline.dbus_cmd_payload_address[31:0]"] | ||
|
||
violation = False | ||
|
||
for (_, val) in addresses.tv: | ||
addr = int(val, 2) | ||
if str(hex(addr)) == "0xdead0": | ||
violation = True | ||
|
||
return violation | ||
|
||
proteus_bin = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../sim/build/sim') | ||
|
||
test_cases = [ | ||
"secret-before-branch", | ||
"secret-after-branch", | ||
] | ||
|
||
for case in test_cases: | ||
print(f"TEST {case}:") | ||
# run test case with secure variant | ||
subprocess.call( | ||
["make", f"{case}.bin"]) | ||
subprocess.call([f"{proteus_bin}", f"{case}.bin"]) | ||
print("SECURE VARIANT: ", end='\t') | ||
print("🗲 Secret leaked!" if evaluate() else "✔ Secret did not leak!") | ||
|
||
# run test case with insecure variant: | ||
# 1. remove fence instructions from code | ||
with open(f"{case}.s") as source: | ||
lines = source.readlines() | ||
stripped = [ | ||
line for line in lines if not line.strip().startswith("fence") and not line.strip().startswith("sfence")] | ||
with open(f"{case}_stripped.s", 'w') as stripped_file: | ||
stripped_file.writelines(stripped) | ||
|
||
subprocess.call(["make", f"{case}_stripped.bin"]) | ||
subprocess.call([f"{proteus_bin}", f"{case}_stripped.bin"]) | ||
print("INSECURE VARIANT:", end='\t') | ||
print("🗲 Secret leaked!" if evaluate() else "✔ Secret did not leak!") | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.globl _start | ||
.data | ||
public_value: .word 0 | ||
.space 1024 # padding to avoid caching | ||
public_address: .word 0 | ||
.space 1024 # padding to avoid caching | ||
secret: .word 0xDEAD0 | ||
|
||
.text | ||
_start: | ||
|
||
setup: | ||
# public_address = &public_value; | ||
|
||
fence | ||
|
||
la t0, public_value | ||
la t1, public_address | ||
sw t0, (t1) | ||
|
||
la t6, secret | ||
|
||
condition: | ||
# t0 = *public_address | ||
# t0 = *t0 | ||
# if (t0 == 0) { goto finish; } | ||
|
||
la t0, public_address | ||
lw t0, (t0) | ||
lw t0, (t0) | ||
beqz t0, finish | ||
|
||
# ----- BEGIN TRANSIENT BLOCK ----- | ||
|
||
# t6 = *secret; | ||
lw t6, (t6) | ||
|
||
fence | ||
|
||
# leak(t6); | ||
lw zero, (t6) | ||
|
||
# ----- END TRANSIENT BLOCK ----- | ||
|
||
finish: | ||
lui ra,0x10000 | ||
li sp,4 | ||
sb sp,0(ra) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
.globl _start | ||
.data | ||
public_value: .word 0 | ||
.space 1024 # padding to avoid caching | ||
public_address: .word 0 | ||
.space 1024 # padding to avoid caching | ||
secret: .word 0xDEAD0 | ||
|
||
.text | ||
_start: | ||
|
||
setup: | ||
# public_address = &public_value; | ||
|
||
la t0, public_value | ||
la t1, public_address | ||
sw t0, (t1) | ||
|
||
nop | ||
nop | ||
|
||
# t6 = *secret // load secret architecturally | ||
lw t6, secret | ||
|
||
condition: | ||
# t0 = *public_address | ||
# t0 = *t0 | ||
# if (t0 == 0) { goto finish; } | ||
|
||
la t0, public_address | ||
lw t0, (t0) | ||
lw t0, (t0) | ||
beqz t0, finish | ||
|
||
# ----- BEGIN TRANSIENT BLOCK ----- | ||
|
||
fence | ||
|
||
# leak(t6); | ||
|
||
lw zero, (t6) | ||
|
||
# ----- END TRANSIENT BLOCK ----- | ||
|
||
finish: | ||
lui ra,0x10000 | ||
li sp,4 | ||
sb sp,0(ra) | ||
|