forked from keys-pub/keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x25519.go
159 lines (136 loc) · 3.57 KB
/
x25519.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package keys
import (
"bytes"
"github.com/pkg/errors"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/nacl/box"
)
// X25519PublicKey is the public key part of a x25519 key.
type X25519PublicKey struct {
id ID
publicKey *[32]byte
}
// X25519 key type.
const X25519 KeyType = "x25519"
const x25519KeyHRP string = "kbx"
// X25519Public public key type.
const X25519Public KeyType = "x25519-public"
// X25519Key is a X25519 assymmetric encryption key.
type X25519Key struct {
id ID
publicKey *X25519PublicKey
privateKey *[32]byte
}
// ID is key identifer.
func (k *X25519Key) ID() ID {
return k.id
}
// Type of key.
func (k *X25519Key) Type() KeyType {
return X25519
}
// Bytes for key.
func (k *X25519Key) Bytes() []byte {
return k.privateKey[:]
}
// Bytes32 for key.
func (k *X25519Key) Bytes32() *[32]byte {
return k.privateKey
}
// PrivateKey returns private part of this X25519Key.
func (k *X25519Key) PrivateKey() *[32]byte {
return k.privateKey
}
// PublicKey returns public part of this X25519Key.
func (k *X25519Key) PublicKey() *X25519PublicKey {
return k.publicKey
}
// GenerateX25519Key creates a new X25519Key.
func GenerateX25519Key() *X25519Key {
logger.Infof("Generating X25519 key...")
key := NewX25519KeyFromSeed(Rand32())
return key
}
// NewX25519KeyFromSeed from seed.
func NewX25519KeyFromSeed(seed *[32]byte) *X25519Key {
publicKey, privateKey, err := box.GenerateKey(bytes.NewReader(seed[:]))
if err != nil {
panic(err)
}
return &X25519Key{
id: MustID(x25519KeyHRP, publicKey[:]),
publicKey: NewX25519PublicKey(publicKey),
privateKey: privateKey,
}
}
// NewX25519KeyFromPrivateKey creates a X25519Key from private key bytes.
func NewX25519KeyFromPrivateKey(privateKey *[32]byte) *X25519Key {
publicKey := new([32]byte)
curve25519.ScalarBaseMult(publicKey, privateKey)
return &X25519Key{
id: MustID(x25519KeyHRP, publicKey[:]),
privateKey: privateKey,
publicKey: NewX25519PublicKey(publicKey),
}
}
// NewX25519PublicKeyFromID converts ID to X25519PublicKey.
func NewX25519PublicKeyFromID(id ID) (*X25519PublicKey, error) {
if id == "" {
return nil, errors.Errorf("empty id")
}
hrp, b, err := id.Decode()
if err != nil {
return nil, err
}
switch hrp {
case x25519KeyHRP:
if len(b) != 32 {
return nil, errors.Errorf("invalid box public key bytes")
}
return NewX25519PublicKey(Bytes32(b)), nil
case edx25519KeyHRP:
spk, err := NewEdX25519PublicKeyFromID(id)
if err != nil {
return nil, err
}
return spk.X25519PublicKey(), nil
default:
return nil, errors.Errorf("unrecognized key type")
}
}
// Seal encrypts message with nacl.box Seal.
func (k *X25519Key) Seal(b []byte, nonce *[24]byte, recipient *X25519PublicKey) []byte {
return box.Seal(nil, b, nonce, recipient.Bytes32(), k.privateKey)
}
// Open decrypts message with nacl.box Open.
func (k *X25519Key) Open(b []byte, nonce *[24]byte, sender *X25519PublicKey) ([]byte, bool) {
return box.Open(nil, b, nonce, sender.Bytes32(), k.privateKey)
}
// NewX25519PublicKey creates X25519PublicKey.
// Metadata is optional.
func NewX25519PublicKey(b *[32]byte) *X25519PublicKey {
id, err := NewID(x25519KeyHRP, b[:])
if err != nil {
panic(err)
}
return &X25519PublicKey{
id: id,
publicKey: b,
}
}
// ID for box public key.
func (k *X25519PublicKey) ID() ID {
return k.id
}
// Type of key.
func (k *X25519PublicKey) Type() KeyType {
return X25519Public
}
// Bytes for key.
func (k *X25519PublicKey) Bytes() []byte {
return k.publicKey[:]
}
// Bytes32 for key.
func (k *X25519PublicKey) Bytes32() *[32]byte {
return k.publicKey
}