-
Notifications
You must be signed in to change notification settings - Fork 22
/
rsa.go
421 lines (371 loc) · 8.58 KB
/
rsa.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// rsa.go
//
// Copyright (c) 2019-2023 Markku Rossi
//
// All rights reserved.
//
package ot
import (
"crypto/rand"
"crypto/rsa"
"fmt"
"math/big"
"github.com/markkurossi/crypto/pkcs1"
"github.com/markkurossi/mpc/ot/mpint"
)
// RandomData creates size bytes of random data.
func RandomData(size int) ([]byte, error) {
m := make([]byte, size)
_, err := rand.Read(m)
if err != nil {
return nil, err
}
return m, nil
}
// Sender implements OT sender.
type Sender struct {
key *rsa.PrivateKey
}
// NewSender creates a new OT sender for the bit.
func NewSender(keyBits int) (*Sender, error) {
key, err := rsa.GenerateKey(rand.Reader, keyBits)
if err != nil {
return nil, err
}
return &Sender{
key: key,
}, nil
}
// MessageSize returns the maximum OT message size.
func (s *Sender) MessageSize() int {
return s.key.PublicKey.Size()
}
// PublicKey returns the sender's public key.
func (s *Sender) PublicKey() *rsa.PublicKey {
return &s.key.PublicKey
}
// NewTransfer creates a new OT sender data transfer.
func (s *Sender) NewTransfer(m0, m1 []byte) (*SenderXfer, error) {
x0, err := RandomData(s.MessageSize())
if err != nil {
return nil, err
}
x1, err := RandomData(s.MessageSize())
if err != nil {
return nil, err
}
return &SenderXfer{
sender: s,
m0: m0,
m1: m1,
x0: x0,
x1: x1,
}, nil
}
// SenderXfer implements the OT sender data transfer.
type SenderXfer struct {
sender *Sender
m0 []byte
m1 []byte
x0 []byte
x1 []byte
k0 *big.Int
k1 *big.Int
}
// MessageSize returns the maximum OT message size.
func (s *SenderXfer) MessageSize() int {
return s.sender.MessageSize()
}
// RandomMessages creates random messages.
func (s *SenderXfer) RandomMessages() ([]byte, []byte) {
return s.x0, s.x1
}
// ReceiveV receives the V.
func (s *SenderXfer) ReceiveV(data []byte) {
v := mpint.FromBytes(data)
x0 := mpint.FromBytes(s.x0)
x1 := mpint.FromBytes(s.x1)
s.k0 = mpint.Exp(mpint.Sub(v, x0), s.sender.key.D, s.sender.key.PublicKey.N)
s.k1 = mpint.Exp(mpint.Sub(v, x1), s.sender.key.D, s.sender.key.PublicKey.N)
}
// Messages creates the transfer messages.
func (s *SenderXfer) Messages() ([]byte, []byte, error) {
m0, err := pkcs1.NewEncryptionBlock(pkcs1.BT1, s.MessageSize(), s.m0)
if err != nil {
return nil, nil, err
}
m0p := mpint.Add(mpint.FromBytes(m0), s.k0)
m1, err := pkcs1.NewEncryptionBlock(pkcs1.BT1, s.MessageSize(), s.m1)
if err != nil {
return nil, nil, err
}
m1p := mpint.Add(mpint.FromBytes(m1), s.k1)
return m0p.Bytes(), m1p.Bytes(), nil
}
// Receiver implements OT receivers.
type Receiver struct {
pub *rsa.PublicKey
}
// NewReceiver creates a new OT receiver.
func NewReceiver(pub *rsa.PublicKey) (*Receiver, error) {
return &Receiver{
pub: pub,
}, nil
}
// MessageSize returns the maximum OT message size.
func (r *Receiver) MessageSize() int {
return r.pub.Size()
}
// NewTransfer creates a new OT receiver data transfer for the bit.
func (r *Receiver) NewTransfer(bit uint) (*ReceiverXfer, error) {
return &ReceiverXfer{
receiver: r,
bit: bit,
}, nil
}
// ReceiverXfer implements the OT receiver data transfer.
type ReceiverXfer struct {
receiver *Receiver
bit uint
k *big.Int
v *big.Int
mb []byte
}
// ReceiveRandomMessages receives the random messages x0 and x1.
func (r *ReceiverXfer) ReceiveRandomMessages(x0, x1 []byte) error {
k, err := rand.Int(rand.Reader, r.receiver.pub.N)
if err != nil {
return err
}
r.k = k
var xb *big.Int
if r.bit == 0 {
xb = mpint.FromBytes(x0)
} else {
xb = mpint.FromBytes(x1)
}
e := big.NewInt(int64(r.receiver.pub.E))
r.v = mpint.Mod(
mpint.Add(xb, mpint.Exp(r.k, e, r.receiver.pub.N)), r.receiver.pub.N)
return nil
}
// V returns the V of the exchange.
func (r *ReceiverXfer) V() []byte {
return r.v.Bytes()
}
// ReceiveMessages processes the received m0p and m1p messages.
func (r *ReceiverXfer) ReceiveMessages(m0p, m1p []byte, err error) error {
if err != nil {
return err
}
var mbp *big.Int
if r.bit == 0 {
mbp = mpint.FromBytes(m0p)
} else {
mbp = mpint.FromBytes(m1p)
}
mbBytes := make([]byte, r.receiver.MessageSize())
mbIntBytes := mpint.Sub(mbp, r.k).Bytes()
ofs := len(mbBytes) - len(mbIntBytes)
copy(mbBytes[ofs:], mbIntBytes)
mb, err := pkcs1.ParseEncryptionBlock(mbBytes)
if err != nil {
return err
}
r.mb = mb
return nil
}
// Message returns the message and bit from the exchange.
func (r *ReceiverXfer) Message() (m []byte, bit uint) {
return r.mb, r.bit
}
// RSA implements RSA OT as the OT interface.
type RSA struct {
keyBits int
name string
io IO
priv *rsa.PrivateKey
pub *rsa.PublicKey
}
// NewRSA creates a new RSA OT implementing the OT interface. The
// argument specifies the RSA key size in bits.
func NewRSA(keyBits int) *RSA {
return &RSA{
keyBits: keyBits,
name: fmt.Sprintf("RSA-%v", keyBits),
}
}
func (r *RSA) messageSize() int {
return (r.keyBits + 7) / 8
}
// InitSender initializes the OT sender.
func (r *RSA) InitSender(io IO) error {
r.io = io
priv, err := rsa.GenerateKey(rand.Reader, r.keyBits)
if err != nil {
return err
}
r.priv = priv
r.pub = &priv.PublicKey
if err := SendString(io, r.name); err != nil {
return err
}
if err := io.SendData(r.pub.N.Bytes()); err != nil {
return err
}
if err := io.SendUint32(r.pub.E); err != nil {
return err
}
return io.Flush()
}
// InitReceiver initializes the OT receiver.
func (r *RSA) InitReceiver(io IO) error {
r.io = io
name, err := ReceiveString(io)
if err != nil {
return err
}
if name != r.name {
return fmt.Errorf("invalid algorithm %s, expected %s", name, r.name)
}
pubN, err := ReceiveBigInt(io)
if err != nil {
return err
}
pubE, err := io.ReceiveUint32()
if err != nil {
return err
}
r.pub = &rsa.PublicKey{
N: pubN,
E: pubE,
}
return nil
}
// Send sends the wire labels with OT.
func (r *RSA) Send(wires []Wire) error {
for i := 0; i < len(wires); i++ {
// Send random messages.
x0, err := RandomData(r.messageSize())
if err != nil {
return err
}
x1, err := RandomData(r.messageSize())
if err != nil {
return err
}
if err := r.io.SendData(x0); err != nil {
return err
}
if err := r.io.SendData(x1); err != nil {
return err
}
if err := r.io.Flush(); err != nil {
return err
}
// Receive V.
v, err := ReceiveBigInt(r.io)
if err != nil {
return err
}
x0i := mpint.FromBytes(x0)
x1i := mpint.FromBytes(x1)
k0 := mpint.Exp(mpint.Sub(v, x0i), r.priv.D, r.pub.N)
k1 := mpint.Exp(mpint.Sub(v, x1i), r.priv.D, r.pub.N)
// Create transfer messages.
var ld LabelData
wires[i].L0.GetData(&ld)
m0, err := pkcs1.NewEncryptionBlock(pkcs1.BT1, r.messageSize(), ld[:])
if err != nil {
return err
}
m0p := mpint.Add(mpint.FromBytes(m0), k0)
if err := r.io.SendData(m0p.Bytes()); err != nil {
return err
}
wires[i].L1.GetData(&ld)
m1, err := pkcs1.NewEncryptionBlock(pkcs1.BT1, r.messageSize(), ld[:])
if err != nil {
return err
}
m1p := mpint.Add(mpint.FromBytes(m1), k1)
if err := r.io.SendData(m1p.Bytes()); err != nil {
return err
}
if err := r.io.Flush(); err != nil {
return err
}
}
return nil
}
// Receive receives the wire labels with OT based on the flag values.
func (r *RSA) Receive(flags []bool, result []Label) error {
for i := 0; i < len(flags); i++ {
k, err := rand.Int(rand.Reader, r.pub.N)
if err != nil {
return err
}
// Receive random messages.
var xb *big.Int
if flags[i] {
_, err = r.io.ReceiveData()
if err != nil {
return err
}
xb, err = ReceiveBigInt(r.io)
if err != nil {
return err
}
} else {
xb, err = ReceiveBigInt(r.io)
if err != nil {
return err
}
_, err = r.io.ReceiveData()
if err != nil {
return err
}
}
// Create and send V.
e := big.NewInt(int64(r.pub.E))
v := mpint.Mod(mpint.Add(xb, mpint.Exp(k, e, r.pub.N)), r.pub.N)
if err := r.io.SendData(v.Bytes()); err != nil {
return err
}
if err := r.io.Flush(); err != nil {
return err
}
// Receive transfer messages.
var mbp *big.Int
if flags[i] {
_, err = ReceiveBigInt(r.io)
if err != nil {
return err
}
mbp, err = ReceiveBigInt(r.io)
if err != nil {
return err
}
} else {
mbp, err = ReceiveBigInt(r.io)
if err != nil {
return err
}
_, err = ReceiveBigInt(r.io)
if err != nil {
return err
}
}
mbBytes := make([]byte, r.messageSize())
mbIntBytes := mpint.Sub(mbp, k).Bytes()
ofs := len(mbBytes) - len(mbIntBytes)
copy(mbBytes[ofs:], mbIntBytes)
mb, err := pkcs1.ParseEncryptionBlock(mbBytes)
if err != nil {
return err
}
result[i].SetBytes(mb)
}
return nil
}