Skip to content

Commit f9d3976

Browse files
committed
more sensible naming.
Calling something a key-pair usually implies public/private. It's awkward to use it to refer to encryption/decryption. Instead, encryption and decription keys together are now called Keys. Apart, they're called subKeys. And a full set (encryption, decryption, public, and private) is now called a KeyPair. I also added some convenience functions.
1 parent ecdb54e commit f9d3976

File tree

10 files changed

+99
-94
lines changed

10 files changed

+99
-94
lines changed

cipherer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
type EncrypterOpts = any
99

1010
type Encrypter interface {
11-
Encrypt(io.Reader, *Message, KeyPair, EncrypterOpts) error
11+
Encrypt(io.Reader, *Message, Key, EncrypterOpts) error
1212
}
1313

1414
type Decrypter interface {

cmd/delphi/encrypt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// PluckPeer plucks out a public key from the [pemBag].
12-
func (app *DelphiApp) PluckPeer() (pubkey delphi.KeyPair) {
12+
func (app *DelphiApp) PluckPeer() (pubkey delphi.Key) {
1313
peer := app.pems.Pluck(delphi.Pubkey)
1414
if peer != nil {
1515
//pubkey = delphi.KeyFromHex(string(peer.Bytes))

cmd/delphi/encrypt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestEncrypt(t *testing.T) {
4848

4949
assert.False(t, delphi.Nonce.IsZero(msg.Nonce))
5050

51-
assert.Len(t, msg.Eph, delphi.KeySize)
51+
assert.Len(t, msg.Eph, delphi.SubKeySize)
5252

5353
assert.Equal(t, "falling-grass", msg.RecipientKey.Nickname())
5454
assert.Equal(t, "bitter-frost", msg.SenderKey.Nickname())

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ require (
1010
github.com/sean9999/pear v0.0.5
1111
github.com/spf13/afero v1.12.0
1212
github.com/stretchr/testify v1.10.0
13-
github.com/vmihailenco/msgpack/v5 v5.4.1
1413
golang.org/x/crypto v0.36.0
1514
)
1615

1716
require (
1817
github.com/DataDog/gostackparse v0.7.0 // indirect
1918
github.com/davecgh/go-spew v1.1.1 // indirect
2019
github.com/pmezard/go-difflib v1.0.0 // indirect
21-
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
2220
golang.org/x/sys v0.31.0 // indirect
2321
golang.org/x/text v0.23.0 // indirect
2422
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e h1:XmA6L9IP
77
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e/go.mod h1:AFIo+02s+12CEg8Gzz9kzhCbmbq6JcKNrhHffCGA9z4=
88
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
99
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10-
github.com/sean9999/hermeti v0.5.0 h1:PXKBi0IIuhtSYydePgPmXHAqfo3ehrpNj2XNsLtF054=
11-
github.com/sean9999/hermeti v0.5.0/go.mod h1:TaERtyaqhd1DmzasV59XB3KAOPFVEm6yMtAG3lpC/ZY=
1210
github.com/sean9999/hermeti v0.5.2 h1:Tm2r9mvJDmr1enKd9F0k3JfrK3lZXdrCpJOXqnDLtgk=
1311
github.com/sean9999/hermeti v0.5.2/go.mod h1:XLlNI3TttQ8GbycFBvB9KJh5NYm2UulSpT7QVX6EQZ0=
1412
github.com/sean9999/pear v0.0.5 h1:IHOYxBo1KymPjyN00EIedY2Ifa5XAZNy4eWgcmI6ssc=
@@ -19,10 +17,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
1917
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2018
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
2119
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
22-
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
23-
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
24-
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
25-
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
2620
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
2721
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
2822
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=

key.go

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"slices"
1313
)
1414

15-
const KeySize = 32
15+
const SubKeySize = 32
1616

17-
// a key is either: a public encryption, public signing, private encryption, or private signing key
18-
type key [KeySize]byte
17+
// a subKey is either: a public encryption, public signing, private encryption, or private signing subKey
18+
type subKey [SubKeySize]byte
1919

2020
// a subKey is zero if all it's bytes are zero
21-
func (s key) IsZero() bool {
21+
func (s subKey) IsZero() bool {
2222
for _, b := range s {
2323
if b != 0 {
2424
return false
@@ -27,74 +27,75 @@ func (s key) IsZero() bool {
2727
return true
2828
}
2929

30-
func (s key) Bytes() []byte {
30+
func (s subKey) Bytes() []byte {
3131
return s[:]
3232
}
3333

3434
func NewPeer() Peer {
35-
return KeyPair{}
35+
return Key{}
3636
}
3737

38-
// a KeyPair is two (specifically one encryption and one signing) keys
39-
type KeyPair [2]key
38+
// a Key is two (specifically one encryption and one signing) keys
39+
type Key [2]subKey
4040

41-
func (k KeyPair) MarshalJSON() ([]byte, error) {
41+
func (k Key) MarshalJSON() ([]byte, error) {
4242
str := k.ToHex()
4343
return json.Marshal(str)
4444
}
4545

46-
func (k KeyPair) MarshalBinary() ([]byte, error) {
46+
func (k Key) MarshalBinary() ([]byte, error) {
4747
return k.Bytes(), nil
4848
}
4949

50-
func (k *KeyPair) UnmarshalBinary(b []byte) error {
51-
copy(k[0][:], b[:KeySize])
52-
copy(k[1][:], b[KeySize:])
50+
func (k *Key) UnmarshalBinary(b []byte) error {
51+
copy(k[0][:], b[:SubKeySize])
52+
copy(k[1][:], b[SubKeySize:])
5353
return nil
5454
}
5555

56-
func (k *KeyPair) UnmarshalJSON(b []byte) error {
56+
func (k *Key) UnmarshalJSON(b []byte) error {
5757
j := KeyFromHex(string(b))
5858
copy(k[:], j[:])
5959
return nil
6060
}
6161

62-
func (k KeyPair) MarshalText() ([]byte, error) {
62+
func (k Key) MarshalText() ([]byte, error) {
6363
return []byte(k.ToHex()), nil
6464
}
6565

6666
// a Key is zero if all it's subKeys are zero
67-
func (k KeyPair) IsZero() bool {
67+
func (k Key) IsZero() bool {
6868
return k[0].IsZero() && k[1].IsZero()
6969
}
7070

71-
func (k KeyPair) From(b []byte) KeyPair {
72-
var enc key
73-
var sig key
74-
copy(enc[:], b[:KeySize])
75-
copy(sig[:], b[KeySize:])
76-
var j KeyPair
71+
func (k Key) From(b []byte) Key {
72+
// TODO: panic or error if the byte slice looks wrong
73+
var enc subKey
74+
var sig subKey
75+
copy(enc[:], b[:SubKeySize])
76+
copy(sig[:], b[SubKeySize:])
77+
var j Key
7778
j[0] = enc
7879
j[1] = sig
7980
return j
8081
}
8182

82-
// a KeyChain is two [KeyPair]s. One public, one private
83-
type KeyChain [2]KeyPair
83+
// a KeyPair is two [Key]s. One public, one private
84+
type KeyPair [2]Key
8485

8586
// a KeyPair is zero if all it's keys are zero
86-
func (kp KeyChain) IsZero() bool {
87+
func (kp KeyPair) IsZero() bool {
8788
return kp[0].IsZero() && kp[1].IsZero()
8889
}
8990

90-
func (k KeyPair) Bytes() []byte {
91-
b := make([]byte, 2*KeySize)
92-
copy(b[:KeySize], k[0][:])
93-
copy(b[KeySize:], k[1][:])
91+
func (k Key) Bytes() []byte {
92+
b := make([]byte, 2*SubKeySize)
93+
copy(b[:SubKeySize], k[0][:])
94+
copy(b[SubKeySize:], k[1][:])
9495
return b
9596
}
9697

97-
func (k KeyPair) ToInt64() int64 {
98+
func (k Key) ToInt64() int64 {
9899
var num int64
99100
buf := bytes.NewReader(k.Bytes())
100101
err := binary.Read(buf, binary.BigEndian, &num)
@@ -104,7 +105,7 @@ func (k KeyPair) ToInt64() int64 {
104105
return num
105106
}
106107

107-
func (k KeyPair) Equal(j KeyPair) bool {
108+
func (k Key) Equal(j Key) bool {
108109
for i := range 2 {
109110

110111
jslice := j[i][:]
@@ -122,62 +123,62 @@ func (k KeyPair) Equal(j KeyPair) bool {
122123
return true
123124
}
124125

125-
func (k KeyPair) Signing() key {
126+
func (k Key) Signing() subKey {
126127
return k[1]
127128
}
128129

129-
func (k KeyPair) Encryption() key {
130+
func (k Key) Encryption() subKey {
130131
return k[0]
131132
}
132133

133-
func (k KeyChain) Bytes() []byte {
134-
b := make([]byte, 4*KeySize)
135-
copy(b[:2*KeySize], k[0].Bytes()) // public
136-
copy(b[2*KeySize:], k[1].Bytes()) // private
134+
func (k KeyPair) Bytes() []byte {
135+
b := make([]byte, 4*SubKeySize)
136+
copy(b[:2*SubKeySize], k[0].Bytes()) // public
137+
copy(b[2*SubKeySize:], k[1].Bytes()) // private
137138
return b
138139
}
139140

140-
func (k KeyPair) ToHex() string {
141+
func (k Key) ToHex() string {
141142
return hex.EncodeToString(k.Bytes())
142143
}
143144

144-
func KeyFromHex(str string) KeyPair {
145+
func KeyFromHex(str string) Key {
145146
bin, err := hex.DecodeString(str)
146147
if err != nil {
147-
return KeyPair{}
148+
return Key{}
148149
}
149150
return KeyFromBytes(bin)
150151
}
151152

152-
func KeyFromBytes(b []byte) KeyPair {
153+
func KeyFromBytes(b []byte) Key {
153154

154155
gotSize := len(b)
155-
wantSize := KeySize * 2
156+
wantSize := SubKeySize * 2
156157

157158
if gotSize != wantSize {
158159
panic(fmt.Sprintf("wrong length for key. Wanted %d but got %d", wantSize, gotSize))
159160
}
160-
k := KeyPair{}
161-
copy(k[0][:], b[:KeySize])
162-
copy(k[1][:], b[KeySize:])
161+
k := Key{}
162+
copy(k[0][:], b[:SubKeySize])
163+
copy(k[1][:], b[SubKeySize:])
163164
return k
164165
}
165166

166-
func NewSubKey(randy io.Reader) key {
167-
sk := key{}
167+
func NewSubKey(randy io.Reader) subKey {
168+
sk := subKey{}
168169
randy.Read(sk[:])
169170
return sk
170171
}
171172

172-
func NewKey(randy io.Reader) KeyPair {
173+
func NewKey(randy io.Reader) Key {
173174
if randy == nil {
174-
return KeyPair{}
175+
return Key{}
175176
}
176-
return KeyPair{NewSubKey(randy), NewSubKey(randy)}
177+
return Key{NewSubKey(randy), NewSubKey(randy)}
177178
}
178179

179180
// NewKeyPair generates valid ed25519 and X25519 keys
180-
func NewKeyPair(randy io.Reader) KeyChain {
181+
func NewKeyPair(randy io.Reader) KeyPair {
181182

182183
/**
183184
* Layout:
@@ -187,7 +188,7 @@ func NewKeyPair(randy io.Reader) KeyChain {
187188
* 4th 32 bytes: private signing key
188189
**/
189190

190-
var kp KeyChain
191+
var kp KeyPair
191192

192193
// encryption keys
193194
ed := ecdh.X25519()
@@ -197,17 +198,17 @@ func NewKeyPair(randy io.Reader) KeyChain {
197198
}
198199
encryptionPub := encryptionPriv.PublicKey()
199200

200-
kp[0][0] = key(encryptionPub.Bytes())
201-
kp[1][0] = key(encryptionPriv.Bytes())
201+
kp[0][0] = subKey(encryptionPub.Bytes())
202+
kp[1][0] = subKey(encryptionPriv.Bytes())
202203

203204
// signing keys
204205
signPub, signPriv, err := ed25519.GenerateKey(randy)
205206
if err != nil {
206207
panic(err)
207208
}
208209

209-
kp[0][1] = key(signPub)
210-
kp[1][1] = key(signPriv[:KeySize])
210+
kp[0][1] = subKey(signPub)
211+
kp[1][1] = subKey(signPriv[:SubKeySize])
211212

212213
return kp
213214
}

key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ import (
88
)
99

1010
func TestSubKey_IsZero(t *testing.T) {
11-
var zeroSubKey key
11+
var zeroSubKey subKey
1212
assert.True(t, zeroSubKey.IsZero())
1313

1414
nonZeroSubKey := NewSubKey(rand.Reader)
1515
assert.False(t, nonZeroSubKey.IsZero())
1616
}
1717

1818
func TestKey_IsZero(t *testing.T) {
19-
var zeroKey KeyPair
19+
var zeroKey Key
2020
assert.True(t, zeroKey.IsZero())
2121

2222
nonZeroKey := NewKey(rand.Reader)
2323
assert.False(t, nonZeroKey.IsZero())
2424
}
2525

2626
func TestKeyPair_IsZero(t *testing.T) {
27-
var zeroKeyPair KeyChain
27+
var zeroKeyPair KeyPair
2828
assert.True(t, zeroKeyPair.IsZero())
2929

3030
nonZeroKeyPair := NewKeyPair(rand.Reader)

message.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ var ErrNotImplemented = errors.New("not implemented")
2525
type Message struct {
2626
readBuffer []byte `msgpack:"-"`
2727
Subject Subject `msgpack:"subj" json:"subj"`
28-
RecipientKey KeyPair `msgpack:"to" json:"to"`
29-
SenderKey KeyPair `msgpack:"from" json:"from"`
28+
RecipientKey Key `msgpack:"to" json:"to"`
29+
SenderKey Key `msgpack:"from" json:"from"`
3030
Headers KV `msgpack:"hdrs" json:"hdrs"` // additional authenticated data (AAD)
3131
Eph []byte `msgpack:"eph" json:"eph"`
3232
Nonce Nonce `msgpack:"nonce" json:"nonce"`
@@ -194,13 +194,13 @@ func (msg *Message) FromPEM(p pem.Block) error {
194194
if err != nil {
195195
return err
196196
}
197-
msg.SenderKey = KeyPair{}.From(pubKeyBytes)
197+
msg.SenderKey = Key{}.From(pubKeyBytes)
198198
case "to":
199199
pubKeyBytes, err := extractB64(p.Headers, "to")
200200
if err != nil {
201201
return err
202202
}
203-
msg.RecipientKey = KeyPair{}.From(pubKeyBytes)
203+
msg.RecipientKey = Key{}.From(pubKeyBytes)
204204
case "eph":
205205
bin, err := extractB64(p.Headers, "eph")
206206
if err != nil {
@@ -341,8 +341,8 @@ func (msg *Message) Sign(randy io.Reader, signer crypto.Signer) error {
341341
func (msg *Message) Verify() bool {
342342
digest, err := msg.Digest()
343343
if err != nil {
344-
panic(err)
345-
} // TODO: do we really want to panic here?
344+
return false
345+
}
346346
pubKey := ed25519.PublicKey(msg.SenderKey.Signing().Bytes())
347347
return ed25519.Verify(pubKey, digest, msg.Sig)
348348
}

0 commit comments

Comments
 (0)