-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
85 lines (64 loc) · 1.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package main
import (
"log"
"os"
"runtime"
"time"
"github.com/usbarmory/GoTEE/applet"
"github.com/usbarmory/GoTEE/syscall"
"github.com/usbarmory/GoTEE-example/mem"
"github.com/usbarmory/GoTEE-example/util"
)
func init() {
log.SetFlags(log.Ltime)
log.SetOutput(os.Stdout)
// yield to monitor (w/ err != nil) on runtime panic
runtime.Exit = applet.Crash
}
func testRNG(n int) {
buf := make([]byte, n)
syscall.GetRandom(buf, uint(n))
log.Printf("applet obtained %d random bytes from monitor: %x", n, buf)
}
func testRPC() {
res := ""
req := "hello"
log.Printf("applet requests echo via RPC: %s", req)
err := syscall.Call("RPC.Echo", req, &res)
if err != nil {
log.Printf("applet received RPC error: %v", err)
} else {
log.Printf("applet received echo via RPC: %s", res)
}
}
func main() {
log.Printf("%s/%s (%s) • TEE user applet", runtime.GOOS, runtime.GOARCH, runtime.Version())
// test syscall interface
testRNG(16)
// test RPC interface
testRPC()
log.Printf("applet will sleep for 5 seconds")
ledStatus := util.LEDStatus{
Name: "blue",
On: true,
}
// test concurrent execution of applet and supervisor/monitor
for i := 0; i < 5; i++ {
syscall.Call("RPC.LED", ledStatus, nil)
ledStatus.On = !ledStatus.On
time.Sleep(1 * time.Second)
log.Printf("applet says %d mississippi", i+1)
}
// test memory protection
mem.TestAccess("applet")
// this should be unreachable
// test exception handling
mem.TestDataAbort("applet")
// terminate applet
applet.Exit()
}