decrypt

package
v0.0.0-...-f135b4b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package decrypt implements the protocoll of the service to start a poll and decrypt a list of votes.

The service as to be initialized with decrypt.New(crypto_backend, storage_backend, [options...]).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Crypto

type Crypto interface {
	// CreatePollKey creates a new keypair for a poll.
	CreatePollKey() ([]byte, error)

	// PublicPollKey returns the public poll key and the signature for a given key.
	PublicPollKey(key []byte) (pubKey []byte, pubKeySig []byte, err error)

	// Decrypt returned the plaintext from value using the key.
	Decrypt(key []byte, value []byte) ([]byte, error)

	// Sign returns the signature for the given data.
	Sign(value []byte) []byte

	// PublicMainKey returns the public main key.
	PublicMainKey() []byte
}

Crypto implements all required cryptographic functions.

type Decrypt

type Decrypt struct {
	// contains filtered or unexported fields
}

Decrypt holds the internal state of the decrypt component.

func New

func New(crypto Crypto, store Store, options ...Option) *Decrypt

New returns the initialized decrypt component.

func (*Decrypt) Clear

func (d *Decrypt) Clear(ctx context.Context, pollID string) error

Clear stops a poll by removing the generated cryptographic key.

func (*Decrypt) PublicMainKey

func (d *Decrypt) PublicMainKey(ctx context.Context) []byte

PublicMainKey returns the public main key.

func (*Decrypt) Start

func (d *Decrypt) Start(ctx context.Context, pollID string) (pubKey []byte, pubKeySig []byte, err error)

Start starts the poll. Returns a public poll key.

It generates a cryptographic key, saves the poll meta data and returns the public key. It also returns a signature of the public key created with the main key.

If the method is called multiple times with the same pollID, it returns the same public key. This is at least true until Clear() is called.

func (*Decrypt) Stop

func (d *Decrypt) Stop(ctx context.Context, pollID string, voteList [][]byte) (decryptedContent, signature []byte, err error)

Stop takes a list of ecrypted votes, decryptes them and returns them in a random order together with a signature.

If the function is called multiple times with the same pollID and voteList, it returns the same output. But if fails if it is called with different votes.

TODO: This implementation is wrong. Not the output has to be hashed and saved, but the input.

type Option

type Option = func(*Decrypt)

Option for decrypt.New().

func WithListToContent

func WithListToContent(f func(id string, decrypted [][]byte) ([]byte, error)) Option

WithListToContent takes a function that is used to create the content returned from the Stop() call.

The function taks an id and the randomized list of decrypted votes and createa the output format.

func WithMaxVotes

func WithMaxVotes(maxVotes int) Option

WithMaxVotes sets the number of maximum votes, that are supported.

func WithRandomSource

func WithRandomSource(r io.Reader) Option

WithRandomSource sets the random source. Uses crypt/rand.Reader as default.

Also sets the decryptWorkers to 1

Should only be used for testing.

type Store

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

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

	// ValidateSignature makes sure, that no other signature is saved for a
	// poll. Saves the signature for future calls.
	//
	// Has to return `errorcode.Invalid` if the hash differs from a privious
	// call.
	//
	// Has to return `errorcode.NotExist` when the id does not exist.
	ValidateSignature(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
}

Store saves the data, that have to be persistent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL