Documentation
¶
Overview ¶
Package cryptoutil contains generic & stateless crypto helpers.
Index ¶
- Constants
- func AESCTRStream(key, iv []byte) (cipher.Stream, error)
- func AESGCMDecrypt(key, data []byte) ([]byte, error)
- func AESGCMEncrypt(key, data []byte) ([]byte, error)
- func ComputeLinkKey(publicKey, secret []byte) (*[KeySize]byte, error)
- func ConcatAndHashSha256(slices ...[]byte) *[sha256.Size]byte
- func CreatePushGroupReference(sender []byte, counter uint64, secret []byte) ([]byte, error)
- func DeriveKey(passphrase, salt []byte) ([]byte, []byte, error)
- func EdwardsToMontgomery(privKey crypto.PrivKey, pubKey crypto.PubKey) (*[32]byte, *[32]byte, error)
- func EdwardsToMontgomeryPriv(privKey crypto.PrivKey) (*[KeySize]byte, error)
- func EdwardsToMontgomeryPub(pubKey crypto.PubKey) (*[KeySize]byte, error)
- func GenerateNonce() (*[NonceSize]byte, error)
- func GenerateNonceSize(size int) ([]byte, error)
- func GetGroupForAccount(priv, signing crypto.PrivKey) (*protocoltypes.Group, error)
- func GetGroupForContact(contactPairSK crypto.PrivKey) (*protocoltypes.Group, error)
- func GetGroupPushSecret(m GroupWithSecret) ([]byte, error)
- func GetKeysForGroupOfContact(contactPairSK crypto.PrivKey) (crypto.PrivKey, crypto.PrivKey, error)
- func GetLinkKeyArray(m GroupWithLinkKey) (*[KeySize]byte, error)
- func GetSharedSecret(m GroupWithLinkKey) *[KeySize]byte
- func KeySliceToArray(keySlice []byte) (*[KeySize]byte, error)
- func NewDeviceSecret() (*protocoltypes.DeviceSecret, error)
- func NonceSliceToArray(nonceSlice []byte) (*[NonceSize]byte, error)
- func OpenEnvelopeHeaders(data []byte, g *protocoltypes.Group) (*protocoltypes.MessageEnvelope, *protocoltypes.MessageHeaders, error)
- func SealEnvelope(payload []byte, ds *protocoltypes.DeviceSecret, deviceSK crypto.PrivKey, ...) ([]byte, error)
- func SealPayload(payload []byte, ds *protocoltypes.DeviceSecret, deviceSK crypto.PrivKey, ...) ([]byte, []byte, error)
- func SeedFromEd25519PrivateKey(key crypto.PrivKey) ([]byte, error)
- type DecryptInfo
- type DeviceKeystore
- type DeviceKeystoreOpts
- type GroupDatastore
- func (gd *GroupDatastore) Delete(ctx context.Context, pk crypto.PubKey) error
- func (gd *GroupDatastore) Get(ctx context.Context, key crypto.PubKey) (*protocoltypes.Group, error)
- func (gd *GroupDatastore) Has(ctx context.Context, key crypto.PubKey) (bool, error)
- func (gd *GroupDatastore) Put(ctx context.Context, g *protocoltypes.Group) error
- func (gd *GroupDatastore) PutForContactPK(ctx context.Context, pk crypto.PubKey, deviceKeystore DeviceKeystore) error
- type GroupDatastoreReadOnly
- type GroupWithLinkKey
- type GroupWithSecret
- type MemberDevice
- type MessageKeystore
- func (m *MessageKeystore) DeriveDeviceSecret(ctx context.Context, g *protocoltypes.Group, devicePK crypto.PubKey) error
- func (m *MessageKeystore) GetByPushGroupReference(ctx context.Context, ref []byte) ([]byte, error)
- func (m *MessageKeystore) GetDeviceChainKey(ctx context.Context, groupPK, pk crypto.PubKey) (*protocoltypes.DeviceSecret, error)
- func (m *MessageKeystore) GetDeviceSecret(ctx context.Context, g *protocoltypes.Group, acc DeviceKeystore) (*protocoltypes.DeviceSecret, error)
- func (m *MessageKeystore) GetKeyForCID(ctx context.Context, id cid.Cid) (*[32]byte, error)
- func (m *MessageKeystore) GetPrecomputedKeyExpectedCount() int
- func (m *MessageKeystore) HasSecretForRawDevicePK(ctx context.Context, groupPK, devicePK []byte) (has bool)
- func (m *MessageKeystore) OpenEnvelopePayload(ctx context.Context, env *protocoltypes.MessageEnvelope, ...) (*protocoltypes.EncryptedMessage, error)
- func (m *MessageKeystore) OpenOutOfStoreMessage(ctx context.Context, envelope *protocoltypes.OutOfStoreMessage, ...) ([]byte, bool, error)
- func (m *MessageKeystore) OpenPayload(ctx context.Context, id cid.Cid, groupPK crypto.PubKey, payload []byte, ...) ([]byte, *DecryptInfo, error)
- func (m *MessageKeystore) PostDecryptActions(ctx context.Context, di *DecryptInfo, g *protocoltypes.Group, ...) error
- func (m *MessageKeystore) RegisterChainKey(ctx context.Context, g *protocoltypes.Group, devicePK crypto.PubKey, ...) error
- func (m *MessageKeystore) SealEnvelope(ctx context.Context, g *protocoltypes.Group, deviceSK crypto.PrivKey, ...) ([]byte, error)
- func (m *MessageKeystore) UpdatePushGroupReferences(ctx context.Context, devicePK []byte, first uint64, group GroupWithSecret) error
- type OwnMemberDevice
Constants ¶
const ( KeySize = 32 // Key size required by box NonceSize = 24 // Nonce size required by box ScryptIterations = 1 << 15 ScryptR = 8 ScryptP = 1 ScryptKeyLen = 32 )
const NamespaceGroupDatastore = "account_groups_datastore"
const PushSecretNamespace = "push_secret_ref" // nolint:gosec
Variables ¶
This section is empty.
Functions ¶
func AESCTRStream ¶
AESCTRStream returns a CTR stream that can be used to produce ciphertext without padding.
func AESGCMDecrypt ¶
AESGCMDecrypt uses AES+GCM to decrypt plaintext data.
func AESGCMEncrypt ¶
AESGCMEncrypt use AES+GCM to encrypt plaintext data.
The generated output will be longer than the original plaintext input.
func DeriveKey ¶
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 ¶
EdwardsToMontgomeryPriv converts ed25519 priv key to X25519 priv key.
func EdwardsToMontgomeryPub ¶
EdwardsToMontgomeryPub converts ed25519 pub key to X25519 pub key.
func GenerateNonce ¶
func GenerateNonceSize ¶
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 GetLinkKeyArray ¶
func GetLinkKeyArray(m GroupWithLinkKey) (*[KeySize]byte, error)
func GetSharedSecret ¶
func GetSharedSecret(m GroupWithLinkKey) *[KeySize]byte
func NewDeviceSecret ¶
func NewDeviceSecret() (*protocoltypes.DeviceSecret, error)
func OpenEnvelopeHeaders ¶
func OpenEnvelopeHeaders(data []byte, g *protocoltypes.Group) (*protocoltypes.MessageEnvelope, *protocoltypes.MessageHeaders, 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)
Types ¶
type DecryptInfo ¶
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 GroupDatastore ¶
type GroupDatastore struct {
// contains filtered or unexported fields
}
func NewGroupDatastore ¶
func NewGroupDatastore(ds datastore.Datastore) (*GroupDatastore, error)
func (*GroupDatastore) Get ¶
func (gd *GroupDatastore) Get(ctx context.Context, key crypto.PubKey) (*protocoltypes.Group, error)
func (*GroupDatastore) Put ¶
func (gd *GroupDatastore) Put(ctx context.Context, g *protocoltypes.Group) error
func (*GroupDatastore) PutForContactPK ¶
func (gd *GroupDatastore) PutForContactPK(ctx context.Context, pk crypto.PubKey, deviceKeystore DeviceKeystore) error
type GroupDatastoreReadOnly ¶
type GroupWithLinkKey ¶
type GroupWithLinkKey interface { GroupWithSecret GetLinkKey() []byte }
type GroupWithSecret ¶
type MemberDevice ¶
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 ¶
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 ¶
func (m *MessageKeystore) GetDeviceSecret(ctx context.Context, g *protocoltypes.Group, acc DeviceKeystore) (*protocoltypes.DeviceSecret, error)
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 ¶
func (m *MessageKeystore) OpenEnvelopePayload( ctx context.Context, env *protocoltypes.MessageEnvelope, headers *protocoltypes.MessageHeaders, g *protocoltypes.Group, ownPK crypto.PubKey, id cid.Cid, ) (*protocoltypes.EncryptedMessage, error)
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