cryptoutil

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2023 License: Apache-2.0, MIT Imports: 35 Imported by: 8

Documentation

Overview

Package cryptoutil contains generic & stateless crypto helpers.

Index

Constants

View Source
const (
	KeySize          = 32 // Key size required by box
	NonceSize        = 24 // Nonce size required by box
	ScryptIterations = 1 << 15
	ScryptR          = 8
	ScryptP          = 1
	ScryptKeyLen     = 32
)
View Source
const NamespaceGroupDatastore = "account_groups_datastore"
View Source
const PushSecretNamespace = "push_secret_ref" // nolint:gosec

Variables

This section is empty.

Functions

func AESCTRStream

func AESCTRStream(key, iv []byte) (cipher.Stream, error)

AESCTRStream returns a CTR stream that can be used to produce ciphertext without padding.

func AESGCMDecrypt

func AESGCMDecrypt(key, data []byte) ([]byte, error)

AESGCMDecrypt uses AES+GCM to decrypt plaintext data.

func AESGCMEncrypt

func AESGCMEncrypt(key, data []byte) ([]byte, error)

AESGCMEncrypt use AES+GCM to encrypt plaintext data.

The generated output will be longer than the original plaintext input.

func ComputeLinkKey

func ComputeLinkKey(publicKey, secret []byte) (*[KeySize]byte, error)

func ConcatAndHashSha256

func ConcatAndHashSha256(slices ...[]byte) *[sha256.Size]byte

func CreatePushGroupReference

func CreatePushGroupReference(sender []byte, counter uint64, secret []byte) ([]byte, error)

func DeriveKey

func DeriveKey(passphrase, salt []byte) ([]byte, []byte, error)

DeriveKey takes a passphrase of any length and returns a key of fixed size.

If no salt is provided, a new one will be created and returned.

func EdwardsToMontgomery

func EdwardsToMontgomery(privKey crypto.PrivKey, pubKey crypto.PubKey) (*[32]byte, *[32]byte, error)

EdwardsToMontgomery converts ed25519 priv/pub keys to X25519 keys.

func EdwardsToMontgomeryPriv

func EdwardsToMontgomeryPriv(privKey crypto.PrivKey) (*[KeySize]byte, error)

EdwardsToMontgomeryPriv converts ed25519 priv key to X25519 priv key.

func EdwardsToMontgomeryPub

func EdwardsToMontgomeryPub(pubKey crypto.PubKey) (*[KeySize]byte, error)

EdwardsToMontgomeryPub converts ed25519 pub key to X25519 pub key.

func GenerateNonce

func GenerateNonce() (*[NonceSize]byte, error)

func GenerateNonceSize

func GenerateNonceSize(size int) ([]byte, error)

func GetGroupForAccount

func GetGroupForAccount(priv, signing crypto.PrivKey) (*protocoltypes.Group, error)

func GetGroupForContact

func GetGroupForContact(contactPairSK crypto.PrivKey) (*protocoltypes.Group, error)

func GetGroupPushSecret

func GetGroupPushSecret(m GroupWithSecret) ([]byte, error)

func GetKeysForGroupOfContact

func GetKeysForGroupOfContact(contactPairSK crypto.PrivKey) (crypto.PrivKey, crypto.PrivKey, error)

func GetLinkKeyArray

func GetLinkKeyArray(m GroupWithLinkKey) (*[KeySize]byte, error)

func GetSharedSecret

func GetSharedSecret(m GroupWithLinkKey) *[KeySize]byte

func KeySliceToArray

func KeySliceToArray(keySlice []byte) (*[KeySize]byte, error)

func NewDeviceSecret

func NewDeviceSecret() (*protocoltypes.DeviceSecret, error)

func NonceSliceToArray

func NonceSliceToArray(nonceSlice []byte) (*[NonceSize]byte, error)

func SealEnvelope

func SealEnvelope(payload []byte, ds *protocoltypes.DeviceSecret, deviceSK crypto.PrivKey, g *protocoltypes.Group) ([]byte, error)

func SealPayload

func SealPayload(payload []byte, ds *protocoltypes.DeviceSecret, deviceSK crypto.PrivKey, g *protocoltypes.Group) ([]byte, []byte, error)

func SeedFromEd25519PrivateKey

func SeedFromEd25519PrivateKey(key crypto.PrivKey) ([]byte, error)

Types

type DecryptInfo

type DecryptInfo struct {
	NewlyDecrypted bool
	MK             *[32]byte
	Cid            cid.Cid
}

type DeviceKeystore

type DeviceKeystore interface {
	AccountPrivKey() (crypto.PrivKey, error)
	AccountProofPrivKey() (crypto.PrivKey, error)
	DevicePrivKey() (crypto.PrivKey, error)
	ContactGroupPrivKey(pk crypto.PubKey) (crypto.PrivKey, error)
	MemberDeviceForGroup(g *protocoltypes.Group) (*OwnMemberDevice, error)
	RestoreAccountKeys(accountKey crypto.PrivKey, accountProofKey crypto.PrivKey) error
}

func NewDeviceKeystore

func NewDeviceKeystore(ks keystore.Keystore, opts *DeviceKeystoreOpts) DeviceKeystore

NewDeviceKeystore creates a new deviceKeystore instance, if the keystore does not hold an deviceKeystore key, one will be created when required

func NewWithExistingKeys

func NewWithExistingKeys(ks keystore.Keystore, sk crypto.PrivKey, proofSK crypto.PrivKey) (DeviceKeystore, error)

NewWithExistingKeys creates a new deviceKeystore instance and registers the supplied secret key, useful when migrating deviceKeystore to another device

type DeviceKeystoreOpts

type DeviceKeystoreOpts struct {
	Logger *zap.Logger
}

type GroupDatastore

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

func NewGroupDatastore

func NewGroupDatastore(ds datastore.Datastore) (*GroupDatastore, error)

func (*GroupDatastore) Delete

func (gd *GroupDatastore) Delete(ctx context.Context, pk crypto.PubKey) error

func (*GroupDatastore) Get

func (*GroupDatastore) Has

func (gd *GroupDatastore) Has(ctx context.Context, key crypto.PubKey) (bool, error)

func (*GroupDatastore) Put

func (*GroupDatastore) PutForContactPK

func (gd *GroupDatastore) PutForContactPK(ctx context.Context, pk crypto.PubKey, deviceKeystore DeviceKeystore) error

type GroupDatastoreReadOnly

type GroupDatastoreReadOnly interface {
	Has(ctx context.Context, key crypto.PubKey) (bool, error)
	Get(ctx context.Context, key crypto.PubKey) (*protocoltypes.Group, error)
}

type GroupWithLinkKey

type GroupWithLinkKey interface {
	GroupWithSecret
	GetLinkKey() []byte
}

type GroupWithSecret

type GroupWithSecret interface {
	GetPublicKey() []byte
	GetSecret() []byte
}

type MemberDevice

type MemberDevice struct {
	Member crypto.PubKey
	Device crypto.PubKey
}

MemberDevice is a remote Device part of a group

type MessageKeystore

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

MessageKeystore is a key-value store for storing values related to message opening. It has the following namespaces:

  • `chainKeyForDeviceOnGroup`: Storing the current state of a device chain key for a given group. It contains the secret used to derive the next value of the chain key and used to generate a message key for the message at `counter` value, then put in the `precomputedMessageKeys` namespace.
  • `precomputedMessageKeys`: Storing precomputed message keys for a given group, device and message counter. As the chain key stored has already been derived, these message keys need to be computed beforehand. The corresponding message can then be decrypted via a quick lookup.
  • `messageKeyForCIDs`: Containing the message key for a given message CID once the corresponding message has been decrypted.
  • `outOfStoreGroupHint`: Keys are a HMAC value associated to a group public key. It is used when receiving an out-of-store message (e.g. a push notification) to identify the group on which the message belongs, which can then be decrypted.

func NewInMemMessageKeystore

func NewInMemMessageKeystore(logger *zap.Logger) (*MessageKeystore, func())

nolint:deadcode,unused // NewInMemMessageKeystore instantiate a new MessageKeystore, useful for testing

func NewMessageKeystore

func NewMessageKeystore(s datastore.Datastore, logger *zap.Logger) *MessageKeystore

NewMessageKeystore instantiate a new MessageKeystore

func (*MessageKeystore) DeriveDeviceSecret

func (m *MessageKeystore) DeriveDeviceSecret(ctx context.Context, g *protocoltypes.Group, devicePK crypto.PubKey) error

DeriveDeviceSecret derives the next device secret from the current device secret and stores it in the cache. It also updates the device secret in the keystore.

func (*MessageKeystore) GetByPushGroupReference

func (m *MessageKeystore) GetByPushGroupReference(ctx context.Context, ref []byte) ([]byte, error)

GetByPushGroupReference returns the groupPK associated with the given push group reference.

func (*MessageKeystore) GetDeviceChainKey

func (m *MessageKeystore) GetDeviceChainKey(ctx context.Context, groupPK, pk crypto.PubKey) (*protocoltypes.DeviceSecret, error)

GetDeviceChainKey returns the device secret for the given group and device.

func (*MessageKeystore) GetDeviceSecret

GetDeviceSecret returns the device secret for the current device on a given group. If the chain key has not been created yet, it will be generated and registered.

func (*MessageKeystore) GetKeyForCID

func (m *MessageKeystore) GetKeyForCID(ctx context.Context, id cid.Cid) (*[32]byte, error)

GetKeyForCID retrieves the message key for the given message CID.

func (*MessageKeystore) GetPrecomputedKeyExpectedCount

func (m *MessageKeystore) GetPrecomputedKeyExpectedCount() int

GetPrecomputedKeyExpectedCount returns the number of precomputed keys that should be in the cache namespace of the keystore.

func (*MessageKeystore) HasSecretForRawDevicePK

func (m *MessageKeystore) HasSecretForRawDevicePK(ctx context.Context, groupPK, devicePK []byte) (has bool)

HasSecretForRawDevicePK returns true if the device secret is known for the given group and device.

func (*MessageKeystore) OpenEnvelopePayload

OpenEnvelopePayload opens the payload of a message envelope and returns the decrypted message in its EncryptedMessage form. It also performs post decryption actions such as updating message key cache.

func (*MessageKeystore) OpenOutOfStoreMessage

func (m *MessageKeystore) OpenOutOfStoreMessage(ctx context.Context, envelope *protocoltypes.OutOfStoreMessage, groupPublicKey []byte) ([]byte, bool, error)

OpenOutOfStoreMessage opens the given OutOfStoreMessage and returns the decrypted payload. The signature is verified against the given devicePK. It derives the next message key and stores it in the cache, but it doesn't update the device secret.

func (*MessageKeystore) OpenPayload

func (m *MessageKeystore) OpenPayload(ctx context.Context, id cid.Cid, groupPK crypto.PubKey, payload []byte, headers *protocoltypes.MessageHeaders) ([]byte, *DecryptInfo, error)

OpenPayload opens the payload of a message envelope and returns the decrypted message. It retrieves the message key from the keystore or the cache to decrypt the message.

func (*MessageKeystore) PostDecryptActions

func (m *MessageKeystore) PostDecryptActions(ctx context.Context, di *DecryptInfo, g *protocoltypes.Group, ownPK crypto.PubKey, headers *protocoltypes.MessageHeaders) error

PostDecryptActions is called after a message has been decrypted. It saves the message key from the cache namespace to find it quickly on subsequent read operations. It derives the chain key in the cache namespace.

func (*MessageKeystore) RegisterChainKey

func (m *MessageKeystore) RegisterChainKey(ctx context.Context, g *protocoltypes.Group, devicePK crypto.PubKey, ds *protocoltypes.DeviceSecret, isOwnPK bool) error

RegisterChainKey registers a device secret for the given group and device. If the device secret is not from the current device, the function will precompute and store in the cache namespace the next message keys. It is the exported version of registerChainKey.

func (*MessageKeystore) SealEnvelope

func (m *MessageKeystore) SealEnvelope(ctx context.Context, g *protocoltypes.Group, deviceSK crypto.PrivKey, payload []byte) ([]byte, error)

SealEnvelope encrypts the given payload and returns it as an envelope to be published on the group's store. It retrieves the device's chain key from the keystore to encrypt the payload using symmetric encryption. The payload is signed using the device's long term private key for the target group. It also updates the device secret and stores the next message key in the cache.

func (*MessageKeystore) UpdatePushGroupReferences

func (m *MessageKeystore) UpdatePushGroupReferences(ctx context.Context, devicePK []byte, first uint64, group GroupWithSecret) error

UpdatePushGroupReferences updates the push group references for the given devicePK and groupPK in the keystore. It creates the references for the given range [first + precomputePushRefsCount] and [first - precomputePushRefsCount] and deletes the references out of range.

type OwnMemberDevice

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

OwnMemberDevice is own local Device part of a group

func NewOwnMemberDevice

func NewOwnMemberDevice(member crypto.PrivKey, device crypto.PrivKey) *OwnMemberDevice

func (*OwnMemberDevice) PrivateDevice

func (d *OwnMemberDevice) PrivateDevice() crypto.PrivKey

func (*OwnMemberDevice) PrivateMember

func (d *OwnMemberDevice) PrivateMember() crypto.PrivKey

func (*OwnMemberDevice) Public

func (d *OwnMemberDevice) Public() *MemberDevice

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳