-
Notifications
You must be signed in to change notification settings - Fork 146
/
simotlocal.go
238 lines (202 loc) · 5.59 KB
/
simotlocal.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package simot
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/subtle"
"errors"
"io"
"github.com/cloudflare/circl/group"
"golang.org/x/crypto/sha3"
)
const keyLength = 16
// AES GCM encryption, we don't need to pad because our input is fixed length
// Need to use authenticated encryption to defend against tampering on ciphertext
// Input: key, plaintext message
// Output: ciphertext
func aesEncGCM(key, plaintext []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonce := make([]byte, aesgcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
panic(err)
}
ciphertext := aesgcm.Seal(nonce, nonce, plaintext, nil)
return ciphertext
}
// AES GCM decryption
// Input: key, ciphertext message
// Output: plaintext
func aesDecGCM(key, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonceSize := aesgcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("ciphertext too short")
}
nonce, encryptedMessage := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := aesgcm.Open(nil, nonce, encryptedMessage, nil)
return plaintext, err
}
// Initialization
// Input: myGroup, the group we operate in
// Input: m0, m1 the 2 message of the sender
// Input: index, the index of this SimOT
// Output: A = [a]G, a the sender randomness
func (sender *Sender) InitSender(myGroup group.Group, m0, m1 []byte, index int) group.Element {
sender.a = myGroup.RandomNonZeroScalar(rand.Reader)
sender.k0 = make([]byte, keyLength)
sender.k1 = make([]byte, keyLength)
sender.m0 = m0
sender.m1 = m1
sender.index = index
sender.A = myGroup.NewElement()
sender.A.MulGen(sender.a)
sender.myGroup = myGroup
return sender.A.Copy()
}
// Round 1
// ---- sender should send A to receiver ----
// Input: myGroup, the group we operate in
// Input: choice, the receiver choice bit
// Input: index, the index of this SimOT
// Input: A, from sender
// Output: B = [b]G if c == 0, B = A+[b]G if c == 1 (Implementation in constant time). b, the receiver randomness
func (receiver *Receiver) Round1Receiver(myGroup group.Group, choice int, index int, A group.Element) group.Element {
receiver.b = myGroup.RandomNonZeroScalar(rand.Reader)
receiver.c = choice
receiver.kR = make([]byte, keyLength)
receiver.index = index
receiver.A = A
receiver.myGroup = myGroup
bG := myGroup.NewElement()
bG.MulGen(receiver.b)
AorI := myGroup.NewElement()
AorI.CMov(choice, A)
receiver.B = myGroup.NewElement()
receiver.B.Add(bG, AorI)
return receiver.B.Copy()
}
// Round 2
// ---- receiver should send B to sender ----
// Input: B from the receiver
// Output: e0, e1, encryption of m0 and m1 under key k0, k1
func (sender *Sender) Round2Sender(B group.Element) ([]byte, []byte) {
sender.B = B
aB := sender.myGroup.NewElement()
aB.Mul(sender.B, sender.a)
maA := sender.myGroup.NewElement()
maA.Mul(sender.A, sender.a)
maA.Neg(maA)
aBaA := sender.myGroup.NewElement()
aBaA.Add(aB, maA)
// Hash the whole transcript A|B|...
AByte, errByte := sender.A.MarshalBinary()
if errByte != nil {
panic(errByte)
}
BByte, errByte := sender.B.MarshalBinary()
if errByte != nil {
panic(errByte)
}
aBByte, errByte := aB.MarshalBinary()
if errByte != nil {
panic(errByte)
}
hashByte0 := append(AByte, BByte...)
hashByte0 = append(hashByte0, aBByte...)
s := sha3.NewShake128()
_, errWrite := s.Write(hashByte0)
if errWrite != nil {
panic(errWrite)
}
_, errRead := s.Read(sender.k0)
if errRead != nil {
panic(errRead)
}
aBaAByte, errByte := aBaA.MarshalBinary()
if errByte != nil {
panic(errByte)
}
hashByte1 := append(AByte, BByte...)
hashByte1 = append(hashByte1, aBaAByte...)
s = sha3.NewShake128()
_, errWrite = s.Write(hashByte1)
if errWrite != nil {
panic(errWrite)
}
_, errRead = s.Read(sender.k1)
if errRead != nil {
panic(errRead)
}
e0 := aesEncGCM(sender.k0, sender.m0)
sender.e0 = e0
e1 := aesEncGCM(sender.k1, sender.m1)
sender.e1 = e1
return sender.e0, sender.e1
}
// Round 3
// ---- sender should send e0, e1 to receiver ----
// Input: e0, e1: encryption of m0 and m1 from the sender
// Input: choice, choice bit of receiver
// Choose e0 or e1 based on choice bit in constant time
func (receiver *Receiver) Round3Receiver(e0, e1 []byte, choice int) error {
receiver.ec = make([]byte, len(e1))
// If c == 1, copy e1
subtle.ConstantTimeCopy(choice, receiver.ec, e1)
// If c == 0, copy e0
subtle.ConstantTimeCopy(1-choice, receiver.ec, e0)
AByte, errByte := receiver.A.MarshalBinary()
if errByte != nil {
panic(errByte)
}
BByte, errByte := receiver.B.MarshalBinary()
if errByte != nil {
panic(errByte)
}
bA := receiver.myGroup.NewElement()
bA.Mul(receiver.A, receiver.b)
bAByte, errByte := bA.MarshalBinary()
if errByte != nil {
panic(errByte)
}
// Hash the whole transcript so far
hashByte := append(AByte, BByte...)
hashByte = append(hashByte, bAByte...)
s := sha3.NewShake128()
_, errWrite := s.Write(hashByte)
if errWrite != nil {
panic(errWrite)
}
_, errRead := s.Read(receiver.kR) // kR, decryption key of mc
if errRead != nil {
panic(errRead)
}
mc, errDec := aesDecGCM(receiver.kR, receiver.ec)
if errDec != nil {
return errDec
}
receiver.mc = mc
return nil
}
func (receiver *Receiver) Returnmc() []byte {
return receiver.mc
}
func (sender *Sender) Returne0e1() ([]byte, []byte) {
return sender.e0, sender.e1
}
func (sender *Sender) Returnm0m1() ([]byte, []byte) {
return sender.m0, sender.m1
}