matrixolm

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: BSD-2-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package matrixolm implements the Olm and Megolm primitives Matrix end-to-end encryption needs, built on the standard library.

It follows the same construction upstream Apprise uses, which in turn follows the Megolm specification and libolm. Matching the construction is what makes the bytes on the wire match: real Matrix clients have to decrypt what we send, so the format is not ours to choose.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalJSON

func CanonicalJSON(value any) ([]byte, error)

CanonicalJSON returns the sorted-key, whitespace-free encoding Matrix signs.

func DecodeBase64

func DecodeBase64(value string) ([]byte, error)

DecodeBase64 accepts padded, unpadded and URL-safe base64, matching the leniency of other Matrix implementations.

func EncodeBase64

func EncodeBase64(data []byte) string

EncodeBase64 returns the unpadded base64 Matrix uses on the wire.

func MarshalEvent

func MarshalEvent(payload any) ([]byte, error)

MarshalEvent serializes an event payload the way upstream does before encrypting it: a space after every colon and comma, and member order taken from the value rather than sorted. Pass a struct, whose field order encoding/json preserves; a map would be re-ordered and produce a ciphertext upstream never would.

func VerifySignature

func VerifySignature(publicKeyBase64 string, message []byte, signatureBase64 string) bool

VerifySignature checks an Ed25519 signature written the way Matrix writes them: unpadded base64 over a public key in the same encoding.

This is what stops a homeserver handing us a device key of its choosing to encrypt to, so a malformed key or signature has to read as "not verified" rather than being skipped over.

Types

type Account

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

Account is the device identity: a Curve25519 key that other devices encrypt to, and an Ed25519 key that signs everything this device publishes.

func NewAccount

func NewAccount() (*Account, error)

NewAccount generates a fresh device identity.

func NewAccountFromKeys

func NewAccountFromKeys(identityPriv, signingPriv []byte) (*Account, error)

NewAccountFromKeys restores an account from stored raw private keys, so a notifier does not register a new device on every send.

func (*Account) DeviceKeys

func (a *Account) DeviceKeys(userID, deviceID string) (map[string]any, error)

DeviceKeys builds the signed device key object for POST /keys/upload.

func (*Account) GenerateOneTimeKeys

func (a *Account) GenerateOneTimeKeys(userID, deviceID string, count int) (map[string]any, error)

GenerateOneTimeKeys tops the pool up to count keys and returns the signed one_time_keys object for POST /keys/upload.

func (*Account) IdentityKey

func (a *Account) IdentityKey() string

IdentityKey is the Curve25519 public key other devices encrypt to.

func (*Account) NewOutboundSession

func (a *Account) NewOutboundSession(theirIdentityKey, theirOneTimeKey string) (*OlmSession, error)

NewOutboundSession performs the triple Diffie-Hellman handshake against a recipient's published identity key and a claimed one-time key.

func (*Account) PrivateKeys

func (a *Account) PrivateKeys() (identity, signing []byte)

PrivateKeys returns the raw private key material for persistence.

func (*Account) Sign

func (a *Account) Sign(data []byte) string

Sign returns the unpadded base64 Ed25519 signature of data.

func (*Account) SigningKey

func (a *Account) SigningKey() string

SigningKey is the Ed25519 public key that verifies this device's signatures.

type MegolmSession

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

MegolmSession encrypts room events. The ratchet advances after every message, and recipients cannot decrypt anything sent before the session key they hold, so SessionKey must be captured before the first Encrypt.

func NewMegolmSession

func NewMegolmSession() (*MegolmSession, error)

NewMegolmSession starts a session with a random ratchet.

func NewMegolmSessionFromState

func NewMegolmSessionFromState(ratchet [4][]byte, counter uint32, signingPriv []byte) (*MegolmSession, error)

NewMegolmSessionFromState restores a session, which is what allows a sender to keep using one session across sends rather than re-sharing keys.

func (*MegolmSession) Encrypt

func (s *MegolmSession) Encrypt(plaintext []byte) (string, error)

Encrypt returns the base64 Megolm ciphertext for a room event payload: version byte, protobuf body, truncated HMAC, then an Ed25519 signature.

The plaintext is encrypted verbatim, so callers must serialize the event exactly as upstream does or the ciphertext will not match it byte for byte. Upstream uses Python's json.dumps defaults, which put a space after every colon and comma and preserve the order the payload was built in. See MarshalEvent.

func (*MegolmSession) SessionID

func (s *MegolmSession) SessionID() string

SessionID is the base64 Ed25519 public key identifying this session.

func (*MegolmSession) SessionKey

func (s *MegolmSession) SessionKey() string

SessionKey is what recipients need to decrypt, shared over Olm in an m.room_key event. The trailing signature lets them confirm it came from the device that published the matching Ed25519 key.

func (*MegolmSession) State

func (s *MegolmSession) State() (ratchet [4][]byte, counter uint32, signingPriv []byte)

State exports the session for persistence.

type OlmMessage

type OlmMessage struct {
	Type int    `json:"type"`
	Body string `json:"body"`
}

OlmMessage is the ciphertext entry of an m.olm.v1.curve25519-aes-sha2 event.

type OlmSession

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

OlmSession is a single-use outbound Olm session. It exists to hand one recipient device the Megolm room key, which is all a notifier ever needs to send, so it only produces pre-key (type 0) messages.

func (*OlmSession) Encrypt

func (s *OlmSession) Encrypt(plaintext []byte) (OlmMessage, error)

Encrypt produces a pre-key message carrying plaintext, advancing the chain ratchet once.

func (*OlmSession) TheirIdentityKey

func (s *OlmSession) TheirIdentityKey() string

TheirIdentityKey is the recipient device's Curve25519 key, which the event is addressed to.

Jump to

Keyboard shortcuts

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