-
Notifications
You must be signed in to change notification settings - Fork 4
/
threshsign.go
163 lines (137 loc) · 4.17 KB
/
threshsign.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package threshsign
/*
#include "enclave_threshsign/untrusted/Threshsign.h"
#cgo LDFLAGS: -L${SRCDIR}/enclave_threshsign -lthreshsign
#cgo CFLAGS: -I/opt/sgxsdk/include
*/
import "C"
import (
"crypto/sha256"
"fmt"
"unsafe"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
"github.com/pkg/errors"
)
const enclaveFileName = "threshsignenclave.signed.so"
type ThreshsignEnclave struct {
filepath string
threshold uint32
total uint32
cPtr *C.threshsign_t
}
func NewThreshsignEnclave(filepath string, threshold, total uint32) *ThreshsignEnclave {
return &ThreshsignEnclave{
filepath: filepath,
threshold: threshold,
total: total,
}
}
func (s *ThreshsignEnclave) Init(index int, skey string, pkeys []string, numCounters uint32) {
cPkeys := make([]*C.char, 0, len(pkeys))
for _, k := range pkeys {
cPkeys = append(cPkeys, C.CString(k))
}
cPkeyCombined := make([]byte, 512)
cPkeyCombinedLen := len(cPkeyCombined)
fmt.Println("Initializing SGX Enclave") //, pkt_bytes, len(pkt_bytes))
cFilepath := C.CString(fmt.Sprintf("%s/%s", s.filepath, enclaveFileName))
code := C.threshsign_setup(cFilepath,
C.unsigned(len(s.filepath)),
C.unsigned(s.threshold),
C.unsigned(s.total),
C.uint32_t(index),
C.CString(skey),
C.unsigned(len(skey)),
(**C.char)(unsafe.Pointer(&cPkeys[0])),
C.unsigned(len(pkeys)),
(*C.char)(unsafe.Pointer(&cPkeyCombined[0])),
(*C.unsigned)(unsafe.Pointer(&cPkeyCombinedLen)),
C.uint32_t(numCounters),
&s.cPtr)
if code != 0 {
panic(errors.Errorf("unable to initialize enclave. return code: %v", code))
}
for _, k := range cPkeys {
C.free(unsafe.Pointer(k))
}
C.free(unsafe.Pointer(cFilepath))
fmt.Println("Init done")
}
func (s *ThreshsignEnclave) Sign(message []byte, ctrId uint32, newValue uint64) []byte {
signature := make([]byte, 256)
sigLen := len(signature)
hash := sha256.Sum256(message)
ret := C.threshsign_sign(
s.cPtr,
(*C.char)(unsafe.Pointer(&hash[0])),
C.unsigned(len(hash)),
C.uint32_t(ctrId),
C.uint64_t(newValue),
(*C.char)(unsafe.Pointer(&signature[0])),
(*C.unsigned)(unsafe.Pointer(&sigLen)))
if ret != 0 {
panic("Unable to sign")
}
return signature[:sigLen]
}
func (s *ThreshsignEnclave) SignUsingEnclave(message []byte, ctrId uint32, newValue uint64) []byte {
signature := make([]byte, 256)
sigLen := len(signature)
hash := sha256.Sum256(message)
ret := C.threshsign_sign_using_enclave(
s.cPtr,
(*C.char)(unsafe.Pointer(&hash[0])),
C.unsigned(len(hash)),
C.uint32_t(ctrId),
C.uint64_t(newValue),
(*C.char)(unsafe.Pointer(&signature[0])),
(*C.unsigned)(unsafe.Pointer(&sigLen)))
if ret != 0 {
panic(fmt.Sprintf("Unable to sign using enclave: %x: %s", ret, signature[:sigLen]))
}
return signature[:sigLen]
}
func (s *ThreshsignEnclave) AggregateSigAndVerify(message []byte, ctrId uint32, newValue uint64, sigs map[peerpb.PeerID][]byte) []byte {
signature := make([]byte, 256)
sigLen := len(signature)
hash := sha256.Sum256(message)
cSigshares := make([]byte, 0, len(sigs))
cSigshareLens := make([]C.unsigned, 0, len(sigs))
cSigindex := make([]C.int, 0, len(sigs))
for idx, sigshare := range sigs {
cSigshares = append(cSigshares, sigshare...)
cSigshareLens = append(cSigshareLens, C.unsigned(len(sigshare)))
cSigindex = append(cSigindex, C.int(idx+1))
}
code := C.threshsign_aggregate_and_verify(
s.cPtr,
(*C.char)(unsafe.Pointer(&hash[0])),
C.unsigned(len(hash)),
C.uint32_t(ctrId),
C.uint64_t(newValue),
C.unsigned(len(sigs)),
(*C.char)(unsafe.Pointer(&cSigshares[0])),
(*C.unsigned)(&cSigshareLens[0]),
(*C.int)(&cSigindex[0]),
(*C.char)(unsafe.Pointer(&signature[0])),
(*C.unsigned)(unsafe.Pointer(&sigLen)))
if code != 0 {
panic(errors.Errorf("unable to aggregatesigandverify. return code: %v", code))
}
return signature[:sigLen]
}
func (s *ThreshsignEnclave) Verify(message []byte, ctrId uint32, newValue uint64, sig []byte) bool {
hash := sha256.Sum256(message)
ret := C.threshsign_verify(
s.cPtr,
(*C.char)(unsafe.Pointer(&hash[0])),
C.unsigned(len(hash)),
C.uint32_t(ctrId),
C.uint64_t(newValue),
(*C.char)(unsafe.Pointer(&sig[0])),
C.unsigned(len(sig)))
if ret != 0 {
panic("Unable to verify signature")
}
return true
}