Initial Commit

This commit is contained in:
Sarah Jamie Lewis 2019-03-20 12:24:59 -07:00
incheckning fa9b659a15
5 ändrade filer med 191 tillägg och 0 borttagningar

9
LICENSE Normal file
Visa fil

@ -0,0 +1,9 @@
All code in this repository, unless otherwise indicated, is distributed under the following license:
Copyright 2019 Open Privacy Research Society
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

11
README.md Normal file
Visa fil

@ -0,0 +1,11 @@
# Vassago: Private Information Retrieval
Vassago is a client/server implementation of PIRMAP[1], an efficient single-server PIR focused on
retreiving large information in a metadata-resisant way.
Vassago buckets records by recency e.g. a request for bucket 0 will return the bucket with the most recently added items.
This library is a work in progress, unvetted, unaudited. Please do not use this unless you understand the risks of doing so.
* [1] Mayberry, Travis, Erik-Oliver Blass, and Agnes Hui Chan. "PIRMAP: Efficient private information retrieval for MapReduce." International Conference on Financial Cryptography and Data Security. Springer, Berlin, Heidelberg, 2013.

60
client.go Normal file
Visa fil

@ -0,0 +1,60 @@
package vassago
import (
"errors"
"git.openprivacy.ca/lib.surveillance.resistant.tech/trostle-parrish"
"math"
"math/big"
)
// Client implements a Vassago client and can construct PIR requests that a Vassago Server can respond to.
type Client struct {
pk trostle_parrish.TrostleParrish
numberOfServerBuckets int
}
// NewClient generates a new client with the appropriate encryption parameters for the number of buckets and the underlying data.
func NewClient(numberOfServerBuckets int, ciphertextLength int) Client {
var client Client
client.numberOfServerBuckets = numberOfServerBuckets
// We need homomorphism for up to 2^n * n additions
t := math.Ceil(math.Log2(math.Pow(2, float64(numberOfServerBuckets)) * float64(numberOfServerBuckets)))
client.pk = trostle_parrish.Generate(int(t), ciphertextLength)
return client
}
// GenerateRequest creates a request that can be sent to a VassagoServer to obtain the data from the requested bucket.
func (c Client) GenerateRequest(requestedBucket int) []trostle_parrish.Ciphertext {
requestVector := make([]trostle_parrish.Ciphertext, c.numberOfServerBuckets)
for i := 0; i < c.numberOfServerBuckets; i++ {
if requestedBucket != i {
requestVector[i] = c.pk.Encrypt(big.NewInt(0))
} else {
requestVector[i] = c.pk.Encrypt(big.NewInt(1))
}
}
return requestVector
}
// DecryptResponse takes in response from the VassagoServer to a request and outputs the decrypted bucket contents.
// An error is returned if this fails.
func (c Client) DecryptResponse(response []trostle_parrish.Ciphertext) ([]byte, error) {
plaintext := make([]byte, len(response))
for i, res := range response {
decrypted := c.pk.Decrypt(res)
// FIXME: Currently we assume we are encoding 1 byte per ciphertext, this could probably be made much more efficient.
if decrypted.Int64() >= 0 && decrypted.Int64() <= 255 {
if len(decrypted.Bytes()) > 0 {
plaintext[i] = decrypted.Bytes()[0]
} else {
plaintext[i] = 0x00
}
} else {
return plaintext, errors.New("could not sucessfully decrypt request, please check that the number of server buckets is correct")
}
}
return plaintext, nil
}

83
server.go Normal file
Visa fil

@ -0,0 +1,83 @@
package vassago
import (
"git.openprivacy.ca/lib.surveillance.resistant.tech/trostle-parrish"
"math/big"
"sync"
)
// Server implements the server portion of a Vassago system.
type Server struct {
numberOfBuckets uint
bucketBuffer []byte
currentBucketPointer uint
lengthOfBuckets uint
itemLength uint
}
// NewServer sets up a new server with a given number of buckets.
func NewServer(numberOfBuckets uint, lengthOfBuckets uint, itemLength uint) Server {
var server Server
server.bucketBuffer = make([]byte, numberOfBuckets*lengthOfBuckets*itemLength)
server.numberOfBuckets = numberOfBuckets
server.lengthOfBuckets = lengthOfBuckets
server.itemLength = itemLength
server.currentBucketPointer = 0
return server
}
// Add item to bucket appends an item to the most recent bucket
func (s *Server) AddItem(item []byte) {
copy(s.bucketBuffer[s.currentBucketPointer:], item[:])
s.currentBucketPointer += s.itemLength
if s.currentBucketPointer >= (s.numberOfBuckets*s.lengthOfBuckets*s.itemLength) {
s.currentBucketPointer = 0
}
}
// PrivateLookup takes in a request from a client and calculates the response
// Note this method executes an operation over every item in every bucket.
func (s Server) PrivateLookup(request []trostle_parrish.Ciphertext) (result []trostle_parrish.Ciphertext) {
maxBucketLength := s.lengthOfBuckets*s.itemLength
var wg sync.WaitGroup
result = make([]trostle_parrish.Ciphertext, maxBucketLength)
reduce := make([][]trostle_parrish.Ciphertext, maxBucketLength)
for i := range reduce {
reduce[i] = make([]trostle_parrish.Ciphertext, s.numberOfBuckets)
}
calc := func(bucket int, startIndex int) {
for i:=0; i< int(maxBucketLength); i++{
index := (startIndex + i) % int(s.numberOfBuckets*maxBucketLength)
m := s.bucketBuffer[index]
reduce[i][bucket] = big.NewInt(0).Mul(big.NewInt(int64(m)), request[bucket])
}
wg.Done()
}
// Spin up a go routine for each bucket.
for x := 0; x < int(s.numberOfBuckets); x++ {
wg.Add(1)
startIndexForX := int(s.currentBucketPointer) - (int(maxBucketLength)*(x+1))
if startIndexForX < 0 {
startIndexForX = int(s.numberOfBuckets*s.lengthOfBuckets*s.itemLength) + startIndexForX
}
go calc(x, startIndexForX)
}
// Wait for all the go routines to finish
wg.Wait()
// Add each result together.
for i := range reduce {
total := big.NewInt(0)
for j := 0; j < int(s.numberOfBuckets); j++ {
total = trostle_parrish.Add(total, reduce[i][j])
}
result[i] = total
}
return result
}

28
vassago_test.go Normal file
Visa fil

@ -0,0 +1,28 @@
package vassago
import (
"testing"
)
func TestVassago(t *testing.T) {
const NumberOfServerBuckets = 50
const NumberOfRecords = 2048*100
client := NewClient(NumberOfServerBuckets, 255)
server := NewServer(NumberOfServerBuckets, NumberOfRecords, 1)
for i:=0;i< NumberOfServerBuckets*NumberOfRecords; i++ {
server.AddItem([]byte{byte(i)})
}
request := client.GenerateRequest(3)
response := server.PrivateLookup(request)
result, err := client.DecryptResponse(response)
if err == nil {
t.Logf("Returned: (%d) %d", len(result), result)
} else {
t.Errorf("Did not get expected result: %x %v", result, err)
}
}