crypto/register: don't use init in *olm packages

This commit is contained in:
Tulir Asokan 2025-09-11 14:14:08 +03:00
commit c716f30959
12 changed files with 72 additions and 70 deletions

View file

@ -10,7 +10,7 @@ import (
"maunium.net/go/mautrix/crypto/olm"
)
func init() {
func Register() {
olm.InitNewAccount = func() (olm.Account, error) {
return NewAccount()
}

View file

@ -8,7 +8,7 @@ package pk
import "maunium.net/go/mautrix/crypto/olm"
func init() {
func Register() {
olm.InitNewPKSigningFromSeed = func(seed []byte) (olm.PKSigning, error) {
return NewSigningFromSeed(seed)
}

View file

@ -7,15 +7,13 @@
package goolm
import (
// Need to import these subpackages to ensure they are registered
_ "maunium.net/go/mautrix/crypto/goolm/account"
_ "maunium.net/go/mautrix/crypto/goolm/pk"
_ "maunium.net/go/mautrix/crypto/goolm/session"
"maunium.net/go/mautrix/crypto/goolm/account"
"maunium.net/go/mautrix/crypto/goolm/pk"
"maunium.net/go/mautrix/crypto/goolm/session"
"maunium.net/go/mautrix/crypto/olm"
)
func init() {
func Register() {
olm.Driver = "goolm"
olm.GetVersion = func() (major, minor, patch uint8) {
@ -24,4 +22,8 @@ func init() {
olm.SetPickleKeyImpl = func(key []byte) {
panic("gob and json encoding is deprecated and not supported with goolm")
}
account.Register()
pk.Register()
session.Register()
}

View file

@ -10,7 +10,7 @@ import (
"maunium.net/go/mautrix/crypto/olm"
)
func init() {
func Register() {
// Inbound Session
olm.InitInboundGroupSessionFromPickled = func(pickled, key []byte) (olm.InboundGroupSession, error) {
if len(pickled) == 0 {

View file

@ -23,18 +23,6 @@ type Account struct {
mem []byte
}
func init() {
olm.InitNewAccount = func() (olm.Account, error) {
return NewAccount()
}
olm.InitBlankAccount = func() olm.Account {
return NewBlankAccount()
}
olm.InitNewAccountFromPickled = func(pickled, key []byte) (olm.Account, error) {
return AccountFromPickled(pickled, key)
}
}
// Ensure that [Account] implements [olm.Account].
var _ olm.Account = (*Account)(nil)

View file

@ -21,21 +21,6 @@ type InboundGroupSession struct {
mem []byte
}
func init() {
olm.InitInboundGroupSessionFromPickled = func(pickled, key []byte) (olm.InboundGroupSession, error) {
return InboundGroupSessionFromPickled(pickled, key)
}
olm.InitNewInboundGroupSession = func(sessionKey []byte) (olm.InboundGroupSession, error) {
return NewInboundGroupSession(sessionKey)
}
olm.InitInboundGroupSessionImport = func(sessionKey []byte) (olm.InboundGroupSession, error) {
return InboundGroupSessionImport(sessionKey)
}
olm.InitBlankInboundGroupSession = func() olm.InboundGroupSession {
return NewBlankInboundGroupSession()
}
}
// Ensure that [InboundGroupSession] implements [olm.InboundGroupSession].
var _ olm.InboundGroupSession = (*InboundGroupSession)(nil)

View file

@ -21,18 +21,6 @@ type OutboundGroupSession struct {
mem []byte
}
func init() {
olm.InitNewOutboundGroupSessionFromPickled = func(pickled, key []byte) (olm.OutboundGroupSession, error) {
if len(pickled) == 0 {
return nil, olm.EmptyInput
}
s := NewBlankOutboundGroupSession()
return s, s.Unpickle(pickled, key)
}
olm.InitNewOutboundGroupSession = func() (olm.OutboundGroupSession, error) { return NewOutboundGroupSession() }
olm.InitNewBlankOutboundGroupSession = func() olm.OutboundGroupSession { return NewBlankOutboundGroupSession() }
}
// Ensure that [OutboundGroupSession] implements [olm.OutboundGroupSession].
var _ olm.OutboundGroupSession = (*OutboundGroupSession)(nil)

View file

@ -35,16 +35,6 @@ type PKSigning struct {
// Ensure that [PKSigning] implements [olm.PKSigning].
var _ olm.PKSigning = (*PKSigning)(nil)
func init() {
olm.InitNewPKSigning = func() (olm.PKSigning, error) { return NewPKSigning() }
olm.InitNewPKSigningFromSeed = func(seed []byte) (olm.PKSigning, error) {
return NewPKSigningFromSeed(seed)
}
olm.InitNewPKDecryptionFromPrivateKey = func(privateKey []byte) (olm.PKDecryption, error) {
return NewPkDecryption(privateKey)
}
}
func pkSigningSize() uint {
return uint(C.olm_pk_signing_size())
}

View file

@ -11,7 +11,7 @@ import (
var pickleKey = []byte("maunium.net/go/mautrix/crypto/olm")
func init() {
func Register() {
olm.Driver = "libolm"
olm.GetVersion = func() (major, minor, patch uint8) {
@ -24,4 +24,52 @@ func init() {
olm.SetPickleKeyImpl = func(key []byte) {
pickleKey = key
}
olm.InitNewAccount = func() (olm.Account, error) {
return NewAccount()
}
olm.InitBlankAccount = func() olm.Account {
return NewBlankAccount()
}
olm.InitNewAccountFromPickled = func(pickled, key []byte) (olm.Account, error) {
return AccountFromPickled(pickled, key)
}
olm.InitSessionFromPickled = func(pickled, key []byte) (olm.Session, error) {
return SessionFromPickled(pickled, key)
}
olm.InitNewBlankSession = func() olm.Session {
return NewBlankSession()
}
olm.InitNewPKSigning = func() (olm.PKSigning, error) { return NewPKSigning() }
olm.InitNewPKSigningFromSeed = func(seed []byte) (olm.PKSigning, error) {
return NewPKSigningFromSeed(seed)
}
olm.InitNewPKDecryptionFromPrivateKey = func(privateKey []byte) (olm.PKDecryption, error) {
return NewPkDecryption(privateKey)
}
olm.InitInboundGroupSessionFromPickled = func(pickled, key []byte) (olm.InboundGroupSession, error) {
return InboundGroupSessionFromPickled(pickled, key)
}
olm.InitNewInboundGroupSession = func(sessionKey []byte) (olm.InboundGroupSession, error) {
return NewInboundGroupSession(sessionKey)
}
olm.InitInboundGroupSessionImport = func(sessionKey []byte) (olm.InboundGroupSession, error) {
return InboundGroupSessionImport(sessionKey)
}
olm.InitBlankInboundGroupSession = func() olm.InboundGroupSession {
return NewBlankInboundGroupSession()
}
olm.InitNewOutboundGroupSessionFromPickled = func(pickled, key []byte) (olm.OutboundGroupSession, error) {
if len(pickled) == 0 {
return nil, olm.EmptyInput
}
s := NewBlankOutboundGroupSession()
return s, s.Unpickle(pickled, key)
}
olm.InitNewOutboundGroupSession = func() (olm.OutboundGroupSession, error) { return NewOutboundGroupSession() }
olm.InitNewBlankOutboundGroupSession = func() olm.OutboundGroupSession { return NewBlankOutboundGroupSession() }
}

View file

@ -39,15 +39,6 @@ type Session struct {
// Ensure that [Session] implements [olm.Session].
var _ olm.Session = (*Session)(nil)
func init() {
olm.InitSessionFromPickled = func(pickled, key []byte) (olm.Session, error) {
return SessionFromPickled(pickled, key)
}
olm.InitNewBlankSession = func() olm.Session {
return NewBlankSession()
}
}
// sessionSize is the size of a session object in bytes.
func sessionSize() uint {
return uint(C.olm_session_size())

View file

@ -2,4 +2,10 @@
package crypto
import _ "maunium.net/go/mautrix/crypto/goolm"
import (
"maunium.net/go/mautrix/crypto/goolm"
)
func init() {
goolm.Register()
}

View file

@ -2,4 +2,8 @@
package crypto
import _ "maunium.net/go/mautrix/crypto/libolm"
import "maunium.net/go/mautrix/crypto/libolm"
func init() {
libolm.Register()
}