-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkey_test.go
409 lines (344 loc) · 10.3 KB
/
key_test.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
package main
// Test suite for the PoC application with real PGP keys. All the tests run
// locally, the networking logic is not tested here.
import (
"encoding/binary"
"fmt"
"math/rand"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/nikirill/go-crypto/openpgp"
"github.com/nikirill/go-crypto/openpgp/packet"
"github.com/si-co/vpir-code/lib/client"
"github.com/si-co/vpir-code/lib/database"
"github.com/si-co/vpir-code/lib/field"
"github.com/si-co/vpir-code/lib/monitor"
"github.com/si-co/vpir-code/lib/pgp"
"github.com/si-co/vpir-code/lib/query"
"github.com/si-co/vpir-code/lib/server"
"github.com/si-co/vpir-code/lib/utils"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/blake2b"
)
var db *database.DB
func initRealDB() {
// math randomness used only to select random entries
// in the database
rand.Seed(time.Now().UnixNano())
// init global db
var err error
db, err = getDB()
if err != nil {
panic(err)
}
// GC after DB creation
runtime.GC()
}
func TestRealRetrieveKey(t *testing.T) {
// math randomness used only to select random entries
// in the database
rand.Seed(time.Now().UnixNano())
// get file paths for key dump
filePaths := getDBFilePaths()
// generate db from sks key dump
db, err := database.GenerateRealKeyMerkle(filePaths, true)
require.NoError(t, err)
numBlocks := db.NumColumns * db.NumRows
// read in the real pgp key values
realKeys, err := pgp.LoadAndParseKeys(filePaths)
require.NoError(t, err)
// client and servers
prg := utils.RandomPRG()
c := client.NewPIR(prg, &db.Info)
servers := []server.Server{server.NewPIRTwo(db), server.NewPIRTwo(db)}
retrieveRealKey(t, c, servers, realKeys, numBlocks)
}
func TestRealRetrieveKeyPIR(t *testing.T) {
// math randomness used only for testing purposes
rand.Seed(time.Now().UnixNano())
// get file paths for key dump
filePaths := getDBFilePaths()
// generate db from sks key dump
db, err := database.GenerateRealKeyBytes(filePaths, true)
require.NoError(t, err)
numBlocks := db.NumColumns * db.NumRows
// read in the real pgp key values
realKeys, err := pgp.LoadAndParseKeys(filePaths)
require.NoError(t, err)
// client and servers
prg := utils.RandomPRG()
c := client.NewPIR(prg, &db.Info)
servers := []server.Server{server.NewPIRTwo(db), server.NewPIRTwo(db)}
retrieveRealKey(t, c, servers, realKeys, numBlocks)
}
func TestRealCountEmail(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := emailMatch(db)
retrieveComplex(t, db, q, match, "TestRealCountEntireEmail")
}
func TestRealCountEmailPIR(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := emailMatch(db)
retrieveComplexPIR(t, db, q, match, "TestRealCountEntireEmailPIR")
}
func TestRealCountStartsWithEmail(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := startsWithMatch(db)
retrieveComplex(t, db, q, match, "TestRealCountStartsWithEmail")
}
func TestRealCountStartsWithEmailPIR(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := startsWithMatch(db)
retrieveComplexPIR(t, db, q, match, "TestCountStartsWithEmailPIR")
}
func TestRealCountEndsWithEmail(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := endsWithMatch(db)
retrieveComplex(t, db, q, match, "TestRealCountEndsWithEmail")
}
func TestRealCountEndsWithEmailPIR(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := endsWithMatch(db)
retrieveComplexPIR(t, db, q, match, "TestRealCountEndsWithEmailPIR")
}
func TestRealCountPublicKeyAlgorithm(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := pkaMatch(db)
retrieveComplex(t, db, q, match, "TestRealCountPublicKeyAlgorithm")
}
func TestRealCountPublicKeyAlgorithmPIR(t *testing.T) {
if db == nil {
initRealDB()
}
match, q := pkaMatch(db)
retrieveComplex(t, db, q, match, "TestRealCountPublicKeyAlgorithmPIR")
}
func retrieveRealKey(t *testing.T, c client.Client, servers []server.Server, realKeys []*openpgp.Entity, numBlocks int) {
// number of keys to retrieve for the test
numKeys := 1
start := time.Now()
for i := 0; i < numKeys; i++ {
// get random key
j := rand.Intn(len(realKeys))
//fmt.Println(pgp.PrimaryEmail(realKeys[i]))
result := retrieveBlockGivenID(t, c, servers, pgp.PrimaryEmail(realKeys[j]), numBlocks)
result = database.UnPadBlock(result)
// Get a key from the block with the id of the search
retrievedKey, err := pgp.RecoverKeyFromBlock(result, pgp.PrimaryEmail(realKeys[j]))
require.NoError(t, err)
require.Equal(t, pgp.PrimaryEmail(realKeys[j]), pgp.PrimaryEmail(retrievedKey))
require.Equal(t, realKeys[j].PrimaryKey.Fingerprint, retrievedKey.PrimaryKey.Fingerprint)
}
fmt.Printf("Total time to retrieve %d real keys: %v\n", numKeys, time.Since(start))
}
func retrieveBlockGivenID(t *testing.T, c client.Client, ss []server.Server, id string, dbLenBlocks int) []byte {
// compute hash key for id
hashKey := database.HashToIndex(id, dbLenBlocks)
in := make([]byte, 4)
binary.BigEndian.PutUint32(in, uint32(hashKey))
// query given hash key
queries, err := c.QueryBytes(in, len(ss))
require.NoError(t, err)
// get servers answers
answers := make([][]byte, len(ss))
for i := range ss {
answers[i], err = ss[i].AnswerBytes(queries[i])
require.NoError(t, err)
}
// reconstruct block
result, err := c.ReconstructBytes(answers)
require.NoError(t, err)
// return result bytes
switch result.(type) {
case []uint32:
return field.VectorToBytes(result.([]uint32))
default:
return result.([]byte)
}
}
func retrieveComplexPIR(t *testing.T, db *database.DB, q *query.ClientFSS, match interface{}, testName string) {
c := client.NewPredicatePIR(utils.RandomPRG(), &db.Info)
s0 := server.NewPredicatePIR(db, 0)
s1 := server.NewPredicatePIR(db, 1)
totalTimer := monitor.NewMonitor()
// compute the input query
in, err := q.Encode()
require.NoError(t, err)
fssKeys, err := c.QueryBytes(in, 2)
require.NoError(t, err)
a0, err := s0.AnswerBytes(fssKeys[0])
require.NoError(t, err)
a1, err := s1.AnswerBytes(fssKeys[1])
require.NoError(t, err)
answers := [][]byte{a0, a1}
res, err := c.ReconstructBytes(answers)
require.NoError(t, err)
totalTime := totalTimer.Record()
fmt.Printf("Total CPU time %s: %.1fms\n", testName, totalTime)
// verify result
count := localResult(db, q.Info, match)
require.Equal(t, count, res.(uint32))
}
func retrieveComplex(t *testing.T, db *database.DB, q *query.ClientFSS, match interface{}, testName string) {
c := client.NewPredicateAPIR(utils.RandomPRG(), &db.Info)
s0 := server.NewPredicateAPIR(db, 0)
s1 := server.NewPredicateAPIR(db, 1)
totalTimer := monitor.NewMonitor()
// compute the input of the query
in, err := q.Encode()
require.NoError(t, err)
fssKeys, err := c.QueryBytes(in, 2)
require.NoError(t, err)
a0, err := s0.AnswerBytes(fssKeys[0])
require.NoError(t, err)
a1, err := s1.AnswerBytes(fssKeys[1])
require.NoError(t, err)
answers := [][]byte{a0, a1}
res, err := c.ReconstructBytes(answers)
require.NoError(t, err)
totalTime := totalTimer.Record()
fmt.Printf("Total CPU time %s: %.1fms\n", testName, totalTime)
// verify result
count := localResult(db, q.Info, match)
require.Equal(t, count, res.(uint32))
}
func emailMatch(db *database.DB) (string, *query.ClientFSS) {
match := db.KeysInfo[rand.Intn(db.NumColumns)].UserId.Email
h := blake2b.Sum256([]byte(match))
in := utils.ByteToBits(h[:16])
q := &query.ClientFSS{
Info: &query.Info{Target: query.UserId},
Input: in,
}
return match, q
}
func startsWithMatch(db *database.DB) (string, *query.ClientFSS) {
email := db.KeysInfo[rand.Intn(db.NumColumns)].UserId.Email
for len(email) < 5 {
email = db.KeysInfo[rand.Intn(db.NumColumns)].UserId.Email
}
match := email[:5]
in := utils.ByteToBits([]byte(match))
q := &query.ClientFSS{
Info: &query.Info{Target: query.UserId, FromStart: len(match)},
Input: in,
}
return match, q
}
func endsWithMatch(db *database.DB) (string, *query.ClientFSS) {
email := db.KeysInfo[rand.Intn(db.NumColumns)].UserId.Email
for len(email) < 5 {
email = db.KeysInfo[rand.Intn(db.NumColumns)].UserId.Email
}
match := email[len(email)-5:]
in := utils.ByteToBits([]byte(match))
q := &query.ClientFSS{
Info: &query.Info{Target: query.UserId, FromEnd: len(match)},
Input: in,
}
return match, q
}
func pkaMatch(db *database.DB) (packet.PublicKeyAlgorithm, *query.ClientFSS) {
match := packet.PubKeyAlgoRSA
in := utils.ByteToBits([]byte{byte(match)})
q := &query.ClientFSS{
Info: &query.Info{Target: query.PubKeyAlgo},
Input: in,
}
return match, q
}
func localResult(db *database.DB, q *query.Info, match interface{}) uint32 {
diffYears := uint32(0)
count := uint32(0)
for _, k := range db.KeysInfo {
if !q.And {
switch q.Target {
case query.UserId:
toMatch := ""
if q.FromStart != 0 {
email := k.UserId.Email
if len(email) < q.FromStart {
continue
}
toMatch = k.UserId.Email[:q.FromStart]
} else if q.FromEnd != 0 {
email := k.UserId.Email
if len(email) < q.FromEnd {
continue
}
toMatch = email[len(email)-q.FromEnd:]
} else {
toMatch = k.UserId.Email
}
if toMatch == match {
count++
}
case query.PubKeyAlgo:
if k.PubKeyAlgo == match {
count++
}
case query.CreationTime:
if k.CreationTime.Equal(match.(time.Time)) {
count++
}
default:
panic("unknown query type")
}
} else if q.And && !q.Avg {
email := k.UserId.Email
if len(email) < q.FromEnd {
continue
}
toMatchEmail := email[len(email)-q.FromEnd:]
if k.CreationTime.Year() == (match.([]interface{}))[0].(time.Time).Year() &&
toMatchEmail == (match.([]interface{}))[1].(string) {
count++
}
} else if q.And && q.Avg {
email := k.UserId.Email
if len(email) < q.FromEnd {
continue
}
toMatchEmail := email[len(email)-q.FromEnd:]
nowYear := time.Now().Year()
if toMatchEmail == match.(string) {
diffYears += uint32(nowYear - k.CreationTime.Year())
count++
}
} else {
panic("query not implemented")
}
}
if q.And && q.Avg {
count = diffYears / count
}
return count
}
func getDB() (*database.DB, error) {
// get file paths for key dump
filePaths := getDBFilePaths()
// generate db from sks key dump
return database.GenerateRealKeyDB(filePaths)
}
func getDBFilePaths() []string {
sksDir := filepath.Join("data", pgp.SksParsedFolder)
// always use sks-000 for tests
filePath := filepath.Join(sksDir, "sks-000.pgp")
return []string{filePath}
}