-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathed25519.go
More file actions
106 lines (89 loc) · 2.91 KB
/
ed25519.go
File metadata and controls
106 lines (89 loc) · 2.91 KB
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
package crypto
import (
"errors"
"fmt"
"crypto/ed25519"
)
// There are two curves commonly used in the 25519 family, c25519 and ed25519,
// they share some properties, for example the private and public key lengths.
// in cases where a propertly refers to either one use the
// term x25519.
const (
// X25519PrivateKeyLength Private key length for c25519 family of crypto primitives
X25519PrivateKeyLength = 64
// X25519PublicKeyLength Public key length for c25519 family of crypto primitives
X25519PublicKeyLength = 32
// X25519SignatureLength Signature length for c25519 family of crypto primitives
X25519SignatureLength = 64
)
// NewEd25519Verifier constructor for ed25519 Verifier
func NewEd25519Verifier(pubKey []byte) (Verifier, error) {
if len(pubKey) != X25519PublicKeyLength {
return nil, errors.New("key should be 32 bytes got " + fmt.Sprint(len(pubKey)))
}
return &ed25519Verifier{
publicKey: pubKey,
suiteType: "ed25519",
}, nil
}
// NewEd25519Signer constructor for ed25519 Signer
func NewEd25519Signer(privKey []byte) (Signer, error) {
if len(privKey) != X25519PrivateKeyLength {
return nil, errors.New("key should be 64 bytes got " + fmt.Sprint(len(privKey)))
}
verifier, verErr := NewEd25519Verifier(privKey[32:])
if verErr != nil {
return nil, verErr
}
return &ed25519Signer{
privKey: privKey,
verifier: verifier,
}, nil
}
// GenerateEd25519KeyPair a valid Curve25519 key pair.
func GenerateEd25519KeyPair() (pub []byte, priv []byte, err error) {
return ed25519.GenerateKey(nil)
}
// Ed25519PublicKeyFromPrivate return the public key associated with a private key for Curve25519.
func Ed25519PublicKeyFromPrivate(privKey []byte) ([]byte, error) {
if len(privKey) != X25519PrivateKeyLength {
return nil, errors.New("key should be 64 bytes got " + fmt.Sprint(len(privKey)))
}
// last 32 bytes of the private key are the public key
return privKey[32:], nil
}
func Ed25519KeyPairFromSeed(seed []byte) (pub []byte, priv []byte, err error) {
if len(seed) != 32 {
return nil, nil, errors.New("invalid seed length, seed must be 32 bytes")
}
priv = ed25519.NewKeyFromSeed(seed)
pub = priv[32:]
return pub, priv, nil
}
// ed25519Verifier Verifies a signature using Curve25519 and ed25519
type ed25519Verifier struct {
publicKey ed25519.PublicKey
suiteType string
}
func (s *ed25519Verifier) SuiteType() string {
return s.suiteType
}
func (s *ed25519Verifier) Verify(toVerify []byte, signature []byte) bool {
if len(signature) != 64 {
return false
}
return ed25519.Verify(s.publicKey, toVerify, signature)
}
type ed25519Signer struct {
privKey ed25519.PrivateKey
verifier Verifier
}
func (s *ed25519Signer) Sign(toSign []byte) ([]byte, error) {
return ed25519.Sign(s.privKey, toSign), nil
}
func (s *ed25519Signer) Verify(toVerify []byte, signature []byte) bool {
return s.verifier.Verify(toVerify, signature)
}
func (s *ed25519Signer) SuiteType() string {
return s.verifier.SuiteType()
}