Debian Codegraph @

crypto/olm/pk.go

raw
1 package olm
2
3 // #cgo LDFLAGS: -lolm -lstdc++
4 // #include <olm/olm.h>
5 // #include <olm/pk.h>
6 import "C"
7
8 import (
9 "crypto/rand"
10 "encoding/json"
11 "unsafe"
12
13 "github.com/tidwall/sjson"
14
15 "maunium.net/go/mautrix/crypto/canonicaljson"
16 "maunium.net/go/mautrix/id"
17 )
18
19 // PkSigning stores a key pair for signing messages.
20 type PkSigning struct {
21 int *C.OlmPkSigning
22 mem []byte
23 PublicKey id.Ed25519
24 Seed []byte
25 }
26
27 func pkSigningSize() uint {
28 return uint(C.olm_pk_signing_size())
29 }
30
31 func pkSigningSeedLength() uint {
32 return uint(C.olm_pk_signing_seed_length())
33 }
34
35 func pkSigningPublicKeyLength() uint {
36 return uint(C.olm_pk_signing_public_key_length())
37 }
38
39 func pkSigningSignatureLength() uint {
40 return uint(C.olm_pk_signature_length())
41 }
42
43 func NewBlankPkSigning() *PkSigning {
44 memory := make([]byte, pkSigningSize())
45 return &PkSigning{
46 int: C.olm_pk_signing(unsafe.Pointer(&memory[0])),
47 mem: memory,
48 }
49 }
50
51 // Clear clears the underlying memory of a PkSigning object.
52 func (p *PkSigning) Clear() {
53 C.olm_clear_pk_signing((*C.OlmPkSigning)(p.int))
54 }
55
56 // NewPkSigningFromSeed creates a new PkSigning object using the given seed.
57 func NewPkSigningFromSeed(seed []byte) (*PkSigning, error) {
58 p := NewBlankPkSigning()
59 p.Clear()
60 pubKey := make([]byte, pkSigningPublicKeyLength())
61 if C.olm_pk_signing_key_from_seed((*C.OlmPkSigning)(p.int),
62 unsafe.Pointer(&pubKey[0]), C.size_t(len(pubKey)),
63 unsafe.Pointer(&seed[0]), C.size_t(len(seed))) == errorVal() {
64 return nil, p.lastError()
65 }
66 p.PublicKey = id.Ed25519(pubKey)
67 p.Seed = seed
68 return p, nil
69 }
70
71 // NewPkSigning creates a new PkSigning object, containing a key pair for signing messages.
72 func NewPkSigning() (*PkSigning, error) {
73 // Generate the seed
74 seed := make([]byte, pkSigningSeedLength())
75 _, err := rand.Read(seed)
76 if err != nil {
77 panic(NotEnoughGoRandom)
78 }
79 pk, err := NewPkSigningFromSeed(seed)
80 return pk, err
81 }
82
83 // Sign creates a signature for the given message using this key.
84 func (p *PkSigning) Sign(message []byte) ([]byte, error) {
85 signature := make([]byte, pkSigningSignatureLength())
86 if C.olm_pk_sign((*C.OlmPkSigning)(p.int), (*C.uint8_t)(unsafe.Pointer(&message[0])), C.size_t(len(message)),
87 (*C.uint8_t)(unsafe.Pointer(&signature[0])), C.size_t(len(signature))) == errorVal() {
88 return nil, p.lastError()
89 }
90 return signature, nil
91 }
92
93 // SignJSON creates a signature for the given object after encoding it to canonical JSON.
94 func (p *PkSigning) SignJSON(obj interface{}) (string, error) {
95 objJSON, err := json.Marshal(obj)
96 if err != nil {
97 return "", err
98 }
99 objJSON, _ = sjson.DeleteBytes(objJSON, "unsigned")
100 objJSON, _ = sjson.DeleteBytes(objJSON, "signatures")
101 signature, err := p.Sign(canonicaljson.CanonicalJSONAssumeValid(objJSON))
102 if err != nil {
103 return "", err
104 }
105 return string(signature), nil
106 }
107
108 // lastError returns the last error that happened in relation to this PkSigning object.
109 func (p *PkSigning) lastError() error {
110 return convertError(C.GoString(C.olm_pk_signing_last_error((*C.OlmPkSigning)(p.int))))
111 }