Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,5 @@ The service uses the following enironment variables:
## TODOs:

* Fix the Stop method to hash the input instead of the output.
* Fix more timing attacks.
* Write a postgres storage backend.
* Write errors messages as output.
* Use the main key to encrypt the stored data (poll keys and poll hashes)
18 changes: 9 additions & 9 deletions decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (d *Decrypt) Start(ctx context.Context, pollID string) (pubKey []byte, pubK
}

// TODO: Load Key and CreatePoll Key have probably be atomic.
pollKey, err := d.store.LoadKey(pollID)
pollKey, err := d.store.LoadKey(ctx, pollID)
if err != nil {
if !errors.Is(err, errorcode.NotExist) {
return nil, nil, fmt.Errorf("loading poll key: %w", err)
Expand All @@ -83,7 +83,7 @@ func (d *Decrypt) Start(ctx context.Context, pollID string) (pubKey []byte, pubK
}

pollKey = key
if err := d.store.SaveKey(pollID, key); err != nil {
if err := d.store.SaveKey(ctx, pollID, key); err != nil {
return nil, nil, fmt.Errorf("saving poll key: %w", err)
}
}
Expand All @@ -107,7 +107,7 @@ func (d *Decrypt) Start(ctx context.Context, pollID string) (pubKey []byte, pubK
//
// TODO: This implementation is wrong. Not the output has to be hashed and saved, but the input.
func (d *Decrypt) Stop(ctx context.Context, pollID string, voteList [][]byte) (decryptedContent, signature []byte, err error) {
pollKey, err := d.store.LoadKey(pollID)
pollKey, err := d.store.LoadKey(ctx, pollID)
if err != nil {
return nil, nil, fmt.Errorf("loading poll key: %w", err)
}
Expand All @@ -131,7 +131,7 @@ func (d *Decrypt) Stop(ctx context.Context, pollID string, voteList [][]byte) (d
// This has to be the last step of this function to protect agains timing
// attacks. All other steps have to be run, even when the calll is doomed to
// fail in this step
if err := d.store.ValidateSignature(pollID, signature); err != nil {
if err := d.store.ValidateSignature(ctx, pollID, signature); err != nil {
if errors.Is(err, errorcode.Invalid) {
return nil, nil, fmt.Errorf("stop was called with different parameters before")
}
Expand All @@ -143,7 +143,7 @@ func (d *Decrypt) Stop(ctx context.Context, pollID string, voteList [][]byte) (d

// Clear stops a poll by removing the generated cryptographic key.
func (d *Decrypt) Clear(ctx context.Context, pollID string) error {
if err := d.store.ClearPoll(pollID); err != nil {
if err := d.store.ClearPoll(ctx, pollID); err != nil {
return fmt.Errorf("clearing poll from store: %w", err)
}
return nil
Expand Down Expand Up @@ -261,12 +261,12 @@ type Store interface {
// SaveKey stores the private key.
//
// Has to return an error `errorcode.Exist` if the key is already known.
SaveKey(id string, key []byte) error
SaveKey(ctx context.Context, id string, key []byte) error

// LoadKey returns the private key from the store.
//
// If the poll is unknown return `errorcode.NotExist`
LoadKey(id string) (key []byte, err error)
LoadKey(ctx context.Context, id string) (key []byte, err error)

// ValidateSignature makes sure, that no other signature is saved for a
// poll. Saves the signature for future calls.
Expand All @@ -275,12 +275,12 @@ type Store interface {
// call.
//
// Has to return `errorcode.NotExist` when the id does not exist.
ValidateSignature(id string, hash []byte) error
ValidateSignature(ctx context.Context, id string, hash []byte) error

// ClearPoll removes all data for the poll.
//
// Does not return an error if poll does not exist.
ClearPoll(id string) error
ClearPoll(ctx context.Context, id string) error
}

// jsonListToContent creates one byte slice from a list of votes in json format.
Expand Down
9 changes: 5 additions & 4 deletions decrypt/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package decrypt_test

import (
"bytes"
"context"
"fmt"
"sync"

Expand Down Expand Up @@ -53,7 +54,7 @@ func NewStoreMock() *StoreMock {
}
}

func (s *StoreMock) SaveKey(id string, key []byte) error {
func (s *StoreMock) SaveKey(_ context.Context, id string, key []byte) error {
s.mu.Lock()
defer s.mu.Unlock()

Expand All @@ -68,7 +69,7 @@ func (s *StoreMock) SaveKey(id string, key []byte) error {
// LoadKey returns the private key from the store.
//
// If the poll is unknown return (nil, nil)
func (s *StoreMock) LoadKey(id string) ([]byte, error) {
func (s *StoreMock) LoadKey(_ context.Context, id string) ([]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()

Expand All @@ -83,7 +84,7 @@ func (s *StoreMock) LoadKey(id string) ([]byte, error) {
// poll. Saves the signature for future calls.
//
// Has to return an error if the id is unknown in the store.
func (s *StoreMock) ValidateSignature(id string, signature []byte) error {
func (s *StoreMock) ValidateSignature(_ context.Context, id string, signature []byte) error {
s.mu.Lock()
defer s.mu.Unlock()

Expand All @@ -106,7 +107,7 @@ func (s *StoreMock) ValidateSignature(id string, signature []byte) error {
}

// Clear removes all data for the poll.
func (s *StoreMock) ClearPoll(id string) error {
func (s *StoreMock) ClearPoll(_ context.Context, id string) error {
s.mu.Lock()
defer s.mu.Unlock()

Expand Down
34 changes: 33 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,46 @@ go 1.22
require (
github.com/alecthomas/kong v0.9.0
github.com/golang/protobuf v1.5.4
github.com/jackc/pgx/v5 v5.6.0
github.com/ory/dockertest/v3 v3.10.0
golang.org/x/crypto v0.25.0
golang.org/x/sys v0.22.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
golang.org/x/net v0.25.0 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/continuity v0.4.3 // indirect
github.com/docker/cli v27.0.3+incompatible // indirect
github.com/docker/docker v27.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/runc v1.1.13 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading
Loading