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
|
package message
import (
//"encoding/hex"
"fmt"
"time"
"gnunet/crypto"
"gnunet/enums"
"gnunet/util"
"github.com/bfix/gospel/crypto/ed25519"
"github.com/bfix/gospel/data"
)
// EphKeyBlock defines the layout of signed ephemeral key with attributes.
type EphKeyBlock struct {
Purpose *crypto.SignaturePurpose // signature purpose: SIG_ECC_KEY
CreateTime util.AbsoluteTime // Time of key creation
ExpireTime util.RelativeTime // Time to live for key
EphemeralKey []byte `size:"32"` // Ephemeral EdDSA public key
PeerID *util.PeerID // Peer identity (EdDSA public key)
}
// EphemeralKeyMsg announces a new transient key for a peer. The key is signed
// by the issuing peer.
type EphemeralKeyMsg struct {
MsgSize uint16 `order:"big"` // total size of message
MsgType uint16 `order:"big"` // CORE_EPHEMERAL_KEY (88)
SenderStatus uint32 `order:"big"` // enum PeerStateMachine
Signature []byte `size:"64"` // EdDSA signature
SignedBlock *EphKeyBlock
}
// NewEphemeralKeyMsg creates an empty message for key announcement.
func NewEphemeralKeyMsg() *EphemeralKeyMsg {
return &EphemeralKeyMsg{
MsgSize: 160,
MsgType: CORE_EPHEMERAL_KEY,
SenderStatus: 1,
Signature: make([]byte, 64),
SignedBlock: &EphKeyBlock{
Purpose: &crypto.SignaturePurpose{
Size: 88,
Purpose: enums.SIG_ECC_KEY,
},
CreateTime: util.AbsoluteTimeNow(),
ExpireTime: util.NewRelativeTime(12 * time.Hour),
EphemeralKey: make([]byte, 32),
PeerID: util.NewPeerID(nil),
},
}
}
// String returns a human-readable representation of the message.
func (m *EphemeralKeyMsg) String() string {
return fmt.Sprintf("EphKeyMsg{peer=%s,ephkey=%s,create=%s,expire=%s,status=%d}",
util.EncodeBinaryToString(m.SignedBlock.PeerID.Key),
util.EncodeBinaryToString(m.SignedBlock.EphemeralKey),
m.SignedBlock.CreateTime, m.SignedBlock.ExpireTime,
m.SenderStatus)
}
// Header returns the message header in a separate instance.
func (msg *EphemeralKeyMsg) Header() *MessageHeader {
return &MessageHeader{msg.MsgSize, msg.MsgType}
}
// Public extracts the public key of an announcing peer.
func (m *EphemeralKeyMsg) Public() *ed25519.PublicKey {
return ed25519.NewPublicKeyFromBytes(m.SignedBlock.PeerID.Key)
}
// Verify the integrity of the message data using the public key of the
// announcing peer.
func (m *EphemeralKeyMsg) Verify(pub *ed25519.PublicKey) (bool, error) {
data, err := data.Marshal(m.SignedBlock)
if err != nil {
return false, err
}
sig, err := ed25519.NewEdSignatureFromBytes(m.Signature)
if err != nil {
return false, err
}
return pub.EdVerify(data, sig)
}
// NewEphemeralKey creates a new ephemeral key signed by a long-term private
// key and the corresponding GNUnet message to announce the new key.
func NewEphemeralKey(peerId []byte, ltPrv *ed25519.PrivateKey) (*ed25519.PrivateKey, *EphemeralKeyMsg, error) {
msg := NewEphemeralKeyMsg()
copy(msg.SignedBlock.PeerID.Key, peerId)
seed := util.NewRndArray(32)
prv := ed25519.NewPrivateKeyFromSeed(seed)
copy(msg.SignedBlock.EphemeralKey, prv.Public().Bytes())
data, err := data.Marshal(msg.SignedBlock)
if err != nil {
return nil, nil, err
}
sig, err := ltPrv.EdSign(data)
if err != nil {
return nil, nil, err
}
copy(msg.Signature, sig.Bytes())
return prv, msg, nil
}
|