11 pb "github.com/libp2p/go-libp2p-crypto/pb"
13 sha256 "github.com/minio/sha256-simd"
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")
21 // RsaPrivateKey is an rsa private key
22 type RsaPrivateKey struct {
27 // RsaPublicKey is an rsa public key
28 type RsaPublicKey struct {
32 // GenerateRSAKeyPair generates a new rsa private and public key
33 func GenerateRSAKeyPair(bits int, src io.Reader) (PrivKey, PubKey, error) {
35 return nil, nil, ErrRsaKeyTooSmall
37 priv, err := rsa.GenerateKey(src, bits)
42 return &RsaPrivateKey{sk: priv}, &RsaPublicKey{pk}, nil
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)
55 func (pk *RsaPublicKey) Type() pb.KeyType {
59 // Bytes returns protobuf bytes of a public key
60 func (pk *RsaPublicKey) Bytes() ([]byte, error) {
61 return MarshalPublicKey(pk)
64 func (pk *RsaPublicKey) Raw() ([]byte, error) {
65 return x509.MarshalPKIXPublicKey(pk.k)
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)
73 // Equals checks whether this key is equal to another
74 func (pk *RsaPublicKey) Equals(k Key) bool {
75 return KeyEqual(pk, k)
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[:])
84 // GetPublic returns a public key
85 func (sk *RsaPrivateKey) GetPublic() PubKey {
87 sk.pk = &sk.sk.PublicKey
89 return &RsaPublicKey{sk.pk}
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)
97 func (sk *RsaPrivateKey) Type() pb.KeyType {
101 // Bytes returns protobuf bytes from a private key
102 func (sk *RsaPrivateKey) Bytes() ([]byte, error) {
103 return MarshalPrivateKey(sk)
106 func (sk *RsaPrivateKey) Raw() ([]byte, error) {
107 b := x509.MarshalPKCS1PrivateKey(sk.sk)
111 // Equals checks whether this key is equal to another
112 func (sk *RsaPrivateKey) Equals(k Key) bool {
113 return KeyEqual(sk, k)
116 // UnmarshalRsaPrivateKey returns a private key from the input x509 bytes
117 func UnmarshalRsaPrivateKey(b []byte) (PrivKey, error) {
118 sk, err := x509.ParsePKCS1PrivateKey(b)
122 if sk.N.BitLen() < 512 {
123 return nil, ErrRsaKeyTooSmall
125 return &RsaPrivateKey{sk: sk}, nil
128 // MarshalRsaPrivateKey returns the x509 bytes of the private key
129 func MarshalRsaPrivateKey(k *RsaPrivateKey) []byte {
130 return x509.MarshalPKCS1PrivateKey(k.sk)
133 // UnmarshalRsaPublicKey returns a public key from the input x509 bytes
134 func UnmarshalRsaPublicKey(b []byte) (PubKey, error) {
135 pub, err := x509.ParsePKIXPublicKey(b)
139 pk, ok := pub.(*rsa.PublicKey)
141 return nil, errors.New("not actually an rsa public key")
143 if pk.N.BitLen() < 512 {
144 return nil, ErrRsaKeyTooSmall
146 return &RsaPublicKey{pk}, nil
149 // MarshalRsaPublicKey returns the x509 bytes from the public key
150 func MarshalRsaPublicKey(k *RsaPublicKey) ([]byte, error) {
151 return x509.MarshalPKIXPublicKey(k.k)