forked from janimo/textsecure
/
prekeys.go
145 lines (124 loc) · 4.28 KB
/
prekeys.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package axolotl
// PreKey and SignedPreKey support
import (
"github.com/golang/protobuf/proto"
protobuf "github.com/janimo/textsecure/axolotl/protobuf"
)
var maxValue uint32 = 0xFFFFFF
// PreKeyRecord represents a prekey, and is simply wrapper around the corresponding protobuf struct
type PreKeyRecord struct {
Pkrs *protobuf.PreKeyRecordStructure
}
// NewPreKeyRecord creates a new PreKeyRecord instance
func NewPreKeyRecord(id uint32, kp *ECKeyPair) *PreKeyRecord {
pkr := &PreKeyRecord{
&protobuf.PreKeyRecordStructure{
Id: &id,
PublicKey: kp.PublicKey.Key()[:],
PrivateKey: kp.PrivateKey.Key()[:],
},
}
return pkr
}
// LoadPreKeyRecord creates a PreKeyRecord instance from a serialized bytestream
func LoadPreKeyRecord(serialized []byte) (*PreKeyRecord, error) {
record := &PreKeyRecord{Pkrs: &protobuf.PreKeyRecordStructure{}}
err := proto.Unmarshal(serialized, record.Pkrs)
if err != nil {
return nil, err
}
return record, nil
}
// Serialize marshals the prekey into a protobuf.
func (record *PreKeyRecord) Serialize() ([]byte, error) {
b, err := proto.Marshal(record.Pkrs)
if err != nil {
return nil, err
}
return b, nil
}
func (record *PreKeyRecord) getKeyPair() *ECKeyPair {
return MakeECKeyPair(record.Pkrs.GetPrivateKey(), record.Pkrs.GetPublicKey())
}
// GenerateLastResortPreKey creates the last resort PreKey.
// Clients should do this only once, at install time, and durably store it for
// the length of the install.
func GenerateLastResortPreKey() *PreKeyRecord {
return NewPreKeyRecord(maxValue, NewECKeyPair())
}
// GeneratePreKeys creates a list of PreKeys.
// Clients should do this at install time, and subsequently any time the list
// of PreKeys stored on the server runs low.
func GeneratePreKeys(start, count int) []*PreKeyRecord {
records := make([]*PreKeyRecord, count)
for i := 0; i < count; i++ {
records[i] = NewPreKeyRecord(uint32(start+i), NewECKeyPair())
}
return records
}
// SignedPreKeyRecord represents a prekey, and is simply wrapper around the corresponding protobuf struct
type SignedPreKeyRecord struct {
Spkrs *protobuf.SignedPreKeyRecordStructure
}
// NewSignedPreKeyRecord creates a new SignedPreKeyRecord instance
func NewSignedPreKeyRecord(id uint32, timestamp uint64, kp *ECKeyPair, signature []byte) *SignedPreKeyRecord {
return &SignedPreKeyRecord{
&protobuf.SignedPreKeyRecordStructure{
Id: &id,
PublicKey: kp.PublicKey.Key()[:],
PrivateKey: kp.PrivateKey.Key()[:],
Timestamp: ×tamp,
Signature: signature,
},
}
}
// LoadSignedPreKeyRecord creates a SignedPreKeyRecord instance from a serialized bytestream
func LoadSignedPreKeyRecord(serialized []byte) (*SignedPreKeyRecord, error) {
record := &SignedPreKeyRecord{Spkrs: &protobuf.SignedPreKeyRecordStructure{}}
err := proto.Unmarshal(serialized, record.Spkrs)
if err != nil {
return nil, err
}
return record, nil
}
// Serialize marshals the signed prekey into a protobuf.
func (record *SignedPreKeyRecord) Serialize() ([]byte, error) {
b, err := proto.Marshal(record.Spkrs)
if err != nil {
return nil, err
}
return b, nil
}
func (record *SignedPreKeyRecord) getKeyPair() *ECKeyPair {
return MakeECKeyPair(record.Spkrs.GetPrivateKey(), record.Spkrs.GetPublicKey())
}
// PreKeyBundle contains the data required to initialize a sender session.
// It is constructed from PreKeys registered by the peer.
type PreKeyBundle struct {
RegistrationID uint32
DeviceID uint32
PreKeyID uint32
PreKeyPublic *ECPublicKey
SignedPreKeyID int32
SignedPreKeyPublic *ECPublicKey
SignedPreKeySignature [64]byte
IdentityKey *IdentityKey
}
// NewPreKeyBundle creates a PreKeyBundle structure with the given fields.
func NewPreKeyBundle(registrationID, deviceID, preKeyID uint32, preKey *ECPublicKey,
signedPreKeyID int32, signedPreKey *ECPublicKey, signature []byte,
identityKey *IdentityKey) (*PreKeyBundle, error) {
pkb := &PreKeyBundle{
RegistrationID: registrationID,
DeviceID: deviceID,
PreKeyID: preKeyID,
PreKeyPublic: preKey,
SignedPreKeyID: signedPreKeyID,
SignedPreKeyPublic: signedPreKey,
IdentityKey: identityKey,
}
copy(pkb.SignedPreKeySignature[:], signature)
return pkb, nil
}