summary refs log tree commit diff
path: root/vendor/maunium.net/go/mautrix/crypto/olm/pk.go
blob: 70ee452deb88e4ce6fd11c854a639acb3caee0ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2024 Sumner Evans
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package olm

import (
	"maunium.net/go/mautrix/id"
)

// PKSigning is an interface for signing messages.
type PKSigning interface {
	// Seed returns the seed of the key.
	Seed() []byte

	// PublicKey returns the public key.
	PublicKey() id.Ed25519

	// Sign creates a signature for the given message using this key.
	Sign(message []byte) ([]byte, error)

	// SignJSON creates a signature for the given object after encoding it to
	// canonical JSON.
	SignJSON(obj any) (string, error)
}

// PKDecryption is an interface for decrypting messages.
type PKDecryption interface {
	// PublicKey returns the public key.
	PublicKey() id.Curve25519

	// Decrypt verifies and decrypts the given message.
	Decrypt(ephemeralKey, mac, ciphertext []byte) ([]byte, error)
}

var InitNewPKSigning func() (PKSigning, error)
var InitNewPKSigningFromSeed func(seed []byte) (PKSigning, error)
var InitNewPKDecryptionFromPrivateKey func(privateKey []byte) (PKDecryption, error)

// NewPKSigning creates a new [PKSigning] object, containing a key pair for
// signing messages.
func NewPKSigning() (PKSigning, error) {
	return InitNewPKSigning()
}

// NewPKSigningFromSeed creates a new PKSigning object using the given seed.
func NewPKSigningFromSeed(seed []byte) (PKSigning, error) {
	return InitNewPKSigningFromSeed(seed)
}

// NewPKDecryptionFromPrivateKey creates a new [PKDecryption] from a
// base64-encoded private key.
func NewPKDecryptionFromPrivateKey(privateKey []byte) (PKDecryption, error) {
	return InitNewPKDecryptionFromPrivateKey(privateKey)
}