OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / libp2p / go-libp2p-crypto / rsa.go
1 package crypto
2
3 import (
4         "crypto"
5         "crypto/rand"
6         "crypto/rsa"
7         "crypto/x509"
8         "errors"
9         "io"
10
11         pb "github.com/libp2p/go-libp2p-crypto/pb"
12
13         sha256 "github.com/minio/sha256-simd"
14 )
15
16 // ErrRsaKeyTooSmall is returned when trying to generate or parse an RSA key
17 // that's smaller than 512 bits. Keys need to be larger enough to sign a 256bit
18 // hash so this is a reasonable absolute minimum.
19 var ErrRsaKeyTooSmall = errors.New("rsa keys must be >= 512 bits to be useful")
20
21 // RsaPrivateKey is an rsa private key
22 type RsaPrivateKey struct {
23         sk *rsa.PrivateKey
24         pk *rsa.PublicKey
25 }
26
27 // RsaPublicKey is an rsa public key
28 type RsaPublicKey struct {
29         k *rsa.PublicKey
30 }
31
32 // GenerateRSAKeyPair generates a new rsa private and public key
33 func GenerateRSAKeyPair(bits int, src io.Reader) (PrivKey, PubKey, error) {
34         if bits < 512 {
35                 return nil, nil, ErrRsaKeyTooSmall
36         }
37         priv, err := rsa.GenerateKey(src, bits)
38         if err != nil {
39                 return nil, nil, err
40         }
41         pk := &priv.PublicKey
42         return &RsaPrivateKey{sk: priv}, &RsaPublicKey{pk}, nil
43 }
44
45 // Verify compares a signature against input data
46 func (pk *RsaPublicKey) Verify(data, sig []byte) (bool, error) {
47         hashed := sha256.Sum256(data)
48         err := rsa.VerifyPKCS1v15(pk.k, crypto.SHA256, hashed[:], sig)
49         if err != nil {
50                 return false, err
51         }
52         return true, nil
53 }
54
55 func (pk *RsaPublicKey) Type() pb.KeyType {
56         return pb.KeyType_RSA
57 }
58
59 // Bytes returns protobuf bytes of a public key
60 func (pk *RsaPublicKey) Bytes() ([]byte, error) {
61         return MarshalPublicKey(pk)
62 }
63
64 func (pk *RsaPublicKey) Raw() ([]byte, error) {
65         return x509.MarshalPKIXPublicKey(pk.k)
66 }
67
68 // Encrypt returns encrypted bytes from the inpu data
69 func (pk *RsaPublicKey) Encrypt(b []byte) ([]byte, error) {
70         return rsa.EncryptPKCS1v15(rand.Reader, pk.k, b)
71 }
72
73 // Equals checks whether this key is equal to another
74 func (pk *RsaPublicKey) Equals(k Key) bool {
75         return KeyEqual(pk, k)
76 }
77
78 // Sign returns a signature of the input data
79 func (sk *RsaPrivateKey) Sign(message []byte) ([]byte, error) {
80         hashed := sha256.Sum256(message)
81         return rsa.SignPKCS1v15(rand.Reader, sk.sk, crypto.SHA256, hashed[:])
82 }
83
84 // GetPublic returns a public key
85 func (sk *RsaPrivateKey) GetPublic() PubKey {
86         if sk.pk == nil {
87                 sk.pk = &sk.sk.PublicKey
88         }
89         return &RsaPublicKey{sk.pk}
90 }
91
92 // Decrypt returns decrypted bytes of the input encrypted bytes
93 func (sk *RsaPrivateKey) Decrypt(b []byte) ([]byte, error) {
94         return rsa.DecryptPKCS1v15(rand.Reader, sk.sk, b)
95 }
96
97 func (sk *RsaPrivateKey) Type() pb.KeyType {
98         return pb.KeyType_RSA
99 }
100
101 // Bytes returns protobuf bytes from a private key
102 func (sk *RsaPrivateKey) Bytes() ([]byte, error) {
103         return MarshalPrivateKey(sk)
104 }
105
106 func (sk *RsaPrivateKey) Raw() ([]byte, error) {
107         b := x509.MarshalPKCS1PrivateKey(sk.sk)
108         return b, nil
109 }
110
111 // Equals checks whether this key is equal to another
112 func (sk *RsaPrivateKey) Equals(k Key) bool {
113         return KeyEqual(sk, k)
114 }
115
116 // UnmarshalRsaPrivateKey returns a private key from the input x509 bytes
117 func UnmarshalRsaPrivateKey(b []byte) (PrivKey, error) {
118         sk, err := x509.ParsePKCS1PrivateKey(b)
119         if err != nil {
120                 return nil, err
121         }
122         if sk.N.BitLen() < 512 {
123                 return nil, ErrRsaKeyTooSmall
124         }
125         return &RsaPrivateKey{sk: sk}, nil
126 }
127
128 // MarshalRsaPrivateKey returns the x509 bytes of the private key
129 func MarshalRsaPrivateKey(k *RsaPrivateKey) []byte {
130         return x509.MarshalPKCS1PrivateKey(k.sk)
131 }
132
133 // UnmarshalRsaPublicKey returns a public key from the input x509 bytes
134 func UnmarshalRsaPublicKey(b []byte) (PubKey, error) {
135         pub, err := x509.ParsePKIXPublicKey(b)
136         if err != nil {
137                 return nil, err
138         }
139         pk, ok := pub.(*rsa.PublicKey)
140         if !ok {
141                 return nil, errors.New("not actually an rsa public key")
142         }
143         if pk.N.BitLen() < 512 {
144                 return nil, ErrRsaKeyTooSmall
145         }
146         return &RsaPublicKey{pk}, nil
147 }
148
149 // MarshalRsaPublicKey returns the x509 bytes from the public key
150 func MarshalRsaPublicKey(k *RsaPublicKey) ([]byte, error) {
151         return x509.MarshalPKIXPublicKey(k.k)
152 }