-
Notifications
You must be signed in to change notification settings - Fork 3
/
key.go
73 lines (65 loc) · 2.46 KB
/
key.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
package rome
import (
"errors"
"hash"
"math/big"
)
var (
// ErrWrongKey is returned if the key is the wrong type
ErrWrongKey = errors.New("wrong key type or curve")
// ErrInvalidPem is returned when invalid PEM data is attempted to be decoded
ErrInvalidPem = errors.New("invalid PEM data failed to parse")
// ErrDerivePub is returned if there is a error in extracting the pub key from private D
ErrDerivePub = errors.New("could not derive public key")
)
// PrivateKey holds the D point for the curve and the public
// key.
type PrivateKey interface {
// Sign returns a ASN.1 formatted signature
Sign(digest []byte) ([]byte, error)
// Public returns the public key interface
Public() PublicKey
// Private returns the private key as PEM ANS.1 DER bytes
//
// Example Output:
//
// -----BEGIN EC PUBLIC KEY-----
// MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQAcnk2OsBaHEE1LW40x5ZyRubtyYN0
// P0lfNYr/J621MzgmHFWUhPXiGiNi5OLsoWkXAWBqoM5JHPI4GJXzrjBjh2gAgve4
// miuuyibmAF+KgXN8t24pm/Wo2owBTXjTPn2R4kPf8lvkeom3/uM8OQUxx3sn4Gld
// wnDkkVtMdB42du+DMQw=
//
// -----END EC PUBLIC KEY-----
Private() ([]byte, error)
// Private returns the private key as ANS.1 DER bytes
PrivateASN1() ([]byte, error)
// PrivateRaw if a elliptic or edwards curve the returned bytes will
// be the value D
PrivateRaw() []byte
// Decrypt will take a ECIES encrypted ciphertext and decrypt it using the
// private key
Decrypt(ciphertext []byte, cipher Cipher, hash hash.Hash, option ...Option) ([]byte, error)
}
// PublicKey is a Elliptic/Edward curve public key
type PublicKey interface {
// Name returns the curve name
Name() string
// Size returns the key size in bytes
Size() int
// Verify will take a ASN.1 signature and return true if it's valid
Verify(digest []byte, signature []byte) (bool, error)
// Points returns the Elliptic/Edward Curve coordinates
Points() (x *big.Int, y *big.Int)
// Key returns the public key in PEM ASN.1 DER format
Key() ([]byte, error)
// KeyASN1 returns the public key formatted in ASN.1
KeyASN1() ([]byte, error)
// DH takes a hasher and the ephemeral private key
DH(h hash.Hash, g PrivateKey, options ...Option) ([]byte, error)
// Encrypt will uses ECIES to encrypt your message to the public key
Encrypt(msg []byte, cipher Cipher, hash hash.Hash, options ...Option) ([]byte, error)
// Fingerprint returns the hashed ASN.1 digest representing this
// public key. This function will panic if it fails to encode the
// public key.
Fingerprint(hash.Hash) []byte
}