Skip to content

Commit 5c9499b

Browse files
committed
time lock example
1 parent 7c90221 commit 5c9499b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

timelock-nts/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Timelock example using cloudflare's time service
2+
#
3+
4+
Cloudflare provides a secure time oracle service.
5+
Roughly it lets you connect over TLS and it gives you the current time.
6+
7+
Read more about this service here:
8+
https://blog.cloudflare.com/secure-time/
9+
https://developers.cloudflare.com/time-services/nts/
10+
11+
So, this example functions pretty simply:
12+
- first it generates a public key
13+
- it also outputs a remote attestation, where the `report_data` includes the public key and the release time (5 minutes in the future)
14+
- after the release time is reached according to the oralce, it outputs the private key

timelock-nts/docker-compose.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
services:
2+
tapp:
3+
configs:
4+
- source: run.sh
5+
target: run.sh
6+
ports:
7+
- "8080:8080"
8+
volumes:
9+
- /var/run/tappd.sock:/var/run/tappd.sock
10+
build:
11+
dockerfile_inline: |
12+
FROM ubuntu:22.04
13+
RUN apt-get update
14+
RUN apt install -y curl openssl ntpsec-ntpdate
15+
command: bash -c "bash run.sh | tee -a /root/log.txt"
16+
17+
configs:
18+
run.sh:
19+
content: |
20+
#!/bin/bash
21+
key=$$(openssl genpkey -algorithm Ed25519)
22+
echo "Public Key:"; echo "$$key" | openssl pkey -pubout
23+
24+
# Serve the log file (TODO: dashboard not working?)
25+
(cd /root && python3 -m http.server 8080) &
26+
27+
# Get timestamp from cloudflare and add 5 minutes
28+
get_time() { ntpdate -4q time.cloudflare.com 2>/dev/null | head -1 | cut -d' ' -f1,2 | date +%s -f -; }
29+
deadline=$$(($$(get_time) + 300))
30+
deadline_str=$$(date -d @$${deadline})
31+
echo "Release: $$deadline_str"
32+
33+
# Fetch the quote
34+
get_quote() {
35+
PAYLOAD="{\"report_data\": \"$$(echo -n $$1 | od -A n -t x1 | tr -d ' \n')\"}"
36+
curl -X POST --unix-socket /var/run/tappd.sock -d "$$PAYLOAD" http://localhost/prpc/Tappd.TdxQuote?json
37+
}
38+
get_quote $$(echo $$key $$deadline_str | sha256sum)
39+
echo
40+
41+
# Loop until it's time to release the key
42+
while [ $$(get_time) -lt $$deadline ]; do
43+
echo "$$((deadline - $$(get_time)))s left"
44+
sleep 60
45+
done
46+
echo "Private Key:"; echo "$$key"

0 commit comments

Comments
 (0)