Documentation ¶
Index ¶
- Constants
- Variables
- func ECDSAKeyPairFromKey(pri *ecdsa.PrivateKey) (PriKey, PubKey, error)
- func GenerateECDSAKeyPair(src io.Reader) (PriKey, PubKey, error)
- func GenerateECDSAKeyPairWithCurve(curve elliptic.Curve, src io.Reader) (PriKey, PubKey, error)
- func GenerateEd25519Key(src io.Reader) (PriKey, PubKey, error)
- func GenerateKeyPair(typ int, bits ...int) (PriKey, PubKey, error)
- func GenerateKeyPairWithReader(typ int, src io.Reader, bits ...int) (PriKey, PubKey, error)
- func GenerateRSAKeyPair(bits int, src io.Reader) (PriKey, PubKey, error)
- func GenerateSecp256k1Key(src io.Reader) (PriKey, PubKey, error)
- func KeyEqual(k1, k2 Key) bool
- func MarshalECDSAPrivateKey(ePri ECDSAPrivateKey) (res []byte, err error)
- func MarshalECDSAPublicKey(ePub ECDSAPublicKey) (res []byte, err error)
- func PEMEncodeECDSAPrivateKey(key *ECDSAPrivateKey) ([]byte, error)
- func PEMEncodeECDSAPublicKey(pk *ECDSAPublicKey) ([]byte, error)
- func PEMEncodeEd25519PrivateKey(key *Ed25519PrivateKey) ([]byte, error)
- func PEMEncodeEd25519PublicKey(pk *Ed25519PublicKey) ([]byte, error)
- func PEMEncodePriKey(priKey PriKey) ([]byte, error)
- func PEMEncodePubKey(pubKey PubKey) ([]byte, error)
- func PEMEncodeRsaPrivateKey(key *RsaPrivateKey) ([]byte, error)
- func PEMEncodeRsaPublicKey(pk *RsaPublicKey) ([]byte, error)
- func PEMEncodeSecp256k1PrivateKey(key *Secp256k1PrivateKey) ([]byte, error)
- func PEMEncodeSecp256k1PublicKey(pk *Secp256k1PublicKey) ([]byte, error)
- func ProtoMarshalPriKey(k PriKey) ([]byte, error)
- func ProtoMarshalPubKey(k PubKey) ([]byte, error)
- func PubKeyToProto(k PubKey) (*pb.PublicKey, error)
- type ECDSAPrivateKey
- type ECDSAPublicKey
- type ECDSASig
- type Ed25519PrivateKey
- type Ed25519PublicKey
- type Key
- type PriKey
- func PEMDecodePriKey(pemBytes []byte) (PriKey, error)
- func PriKeyFromProto(pbPriKey *pb.PrivateKey) (PriKey, error)
- func ProtoUnmarshalPriKey(data []byte) (PriKey, error)
- func UnmarshalECDSAPrivateKey(data []byte) (res PriKey, err error)
- func UnmarshalEd25519PrivateKey(data []byte) (PriKey, error)
- func UnmarshalRsaPrivateKey(b []byte) (key PriKey, err error)
- func UnmarshalSecp256k1PrivateKey(data []byte) (k PriKey, err error)
- type PriKeyUnmarshaler
- type PubKey
- func ECDSAPublicKeyFromPubKey(pub ecdsa.PublicKey) (PubKey, error)
- func PEMDecodePubKey(pemBytes []byte) (PubKey, error)
- func ProtoUnmarshalPubKey(data []byte) (PubKey, error)
- func PubKeyFromProto(pbPubKey *pb.PublicKey) (PubKey, error)
- func UnmarshalECDSAPublicKey(data []byte) (key PubKey, err error)
- func UnmarshalEd25519PublicKey(data []byte) (PubKey, error)
- func UnmarshalRsaPublicKey(b []byte) (key PubKey, err error)
- func UnmarshalSecp256k1PublicKey(data []byte) (_k PubKey, err error)
- type PubKeyUnmarshaler
- type RsaPrivateKey
- type RsaPublicKey
- type Secp256k1PrivateKey
- type Secp256k1PublicKey
Constants ¶
const ( // RSA is an enum for the supported RSA key type. RSA = iota // Ed25519 is an enum for the supported Ed25519 key type. Ed25519 // Secp256k1 is an enum for the supported Secp256k1 key type. Secp256k1 // ECDSA is an enum for the supported ECDSA key type. ECDSA // PEMBlockTypeRsaPrivateKey represents the PEM block type for RSA private key. PEMBlockTypeRsaPrivateKey = "RSA PRIVATE KEY" // PEMBlockTypeEd25519PrivateKey represents the PEM block type for Ed25519 private key. PEMBlockTypeEd25519PrivateKey = "ED25519 PRIVATE KEY" // PEMBlockTypeSecp256k1PrivateKey represents the PEM block type for secp256k1 private key. PEMBlockTypeSecp256k1PrivateKey = "SECP256K1 PRIVATE KEY" // PEMBlockTypeECDSAPrivateKey represents the PEM block type for ECDSA private key. PEMBlockTypeECDSAPrivateKey = "ECDSA PRIVATE KEY" // PEMBlockTypeRsaPublicKey represents the PEM block type for RSA public key. PEMBlockTypeRsaPublicKey = "RSA PUBLIC KEY" // PEMBlockTypeEd25519PublicKey represents the PEM block type for Ed25519 public key. PEMBlockTypeEd25519PublicKey = "ED25519 PUBLIC KEY" // PEMBlockTypeSecp256k1PublicKey represents the PEM block type for secp256k1 public key. PEMBlockTypeSecp256k1PublicKey = "SECP256K1 PUBLIC KEY" // PEMBlockTypeECDSAPublicKey represents the PEM block type for ECDSA public key. PEMBlockTypeECDSAPublicKey = "ECDSA PUBLIC KEY" )
const WeakRsaKeyEnv = "RAINBOW_BEE_ALLOW_WEAK_RSA_KEYS"
WeakRsaKeyEnv is an environment variable which, when set, lowers the minimum required bits of RSA keys to 512. This should be used exclusively in test situations.
Variables ¶
var ( // ErrNotECDSAPubKey is returned when the public key passed is not an ecdsa public key. ErrNotECDSAPubKey = errors.New("not an ecdsa public key") // ErrNilSig is returned when the signature is nil. ErrNilSig = errors.New("sig is nil") // ErrNilPrivateKey is returned when a nil private key is provided. ErrNilPrivateKey = errors.New("private key is nil") // ErrNilPublicKey is returned when a nil public key is provided. ErrNilPublicKey = errors.New("public key is nil") // ECDSACurve is the default ecdsa curve used. ECDSACurve = elliptic.P256() )
var ( // ErrBadKeyType is returned when a key is not supported. ErrBadKeyType = errors.New("invalid or unsupported key type") // ErrPEMDecodeFailed is returned when decode pem to key failed. ErrPEMDecodeFailed = errors.New("failed to decode pem") // KeyTypes is a list of supported keys KeyTypes = []int{ RSA, Ed25519, Secp256k1, ECDSA, } // PriKeyUnmarshalers is a map of PriKey unmarshalers by key type. PriKeyUnmarshalers = map[pb.KeyType]PriKeyUnmarshaler{ pb.KeyType_RSA: UnmarshalRsaPrivateKey, pb.KeyType_Ed25519: UnmarshalEd25519PrivateKey, pb.KeyType_Secp256k1: UnmarshalSecp256k1PrivateKey, pb.KeyType_ECDSA: UnmarshalECDSAPrivateKey, } // PubKeyUnmarshalers is a map of PubKey unmarshalers by key type. PubKeyUnmarshalers = map[pb.KeyType]PubKeyUnmarshaler{ pb.KeyType_RSA: UnmarshalRsaPublicKey, pb.KeyType_Ed25519: UnmarshalEd25519PublicKey, pb.KeyType_Secp256k1: UnmarshalSecp256k1PublicKey, pb.KeyType_ECDSA: UnmarshalECDSAPublicKey, } )
var ErrRsaKeyTooBig error = fmt.Errorf("rsa keys must be <= %d bits", maxRsaKeyBits)
var ErrRsaKeyTooSmall error
ErrRsaKeyTooSmall is returned when trying to generate or parse an RSA key that's smaller than MinRsaKeyBits bits. In test
var MinRsaKeyBits = 2048
Functions ¶
func ECDSAKeyPairFromKey ¶
func ECDSAKeyPairFromKey(pri *ecdsa.PrivateKey) (PriKey, PubKey, error)
ECDSAKeyPairFromKey generates a new ecdsa private and public key from an input private key.
func GenerateECDSAKeyPair ¶
GenerateECDSAKeyPair generates a new ecdsa private and public key.
func GenerateECDSAKeyPairWithCurve ¶
GenerateECDSAKeyPairWithCurve generates a new ecdsa private and public key with a specified curve.
func GenerateEd25519Key ¶
GenerateEd25519Key generates a new ed25519 private and public key pair.
func GenerateKeyPair ¶
GenerateKeyPair generates a private and public key. When generating Rsa type Key, bits need to be provided.
func GenerateKeyPairWithReader ¶
GenerateKeyPairWithReader returns a keypair of the given type and bit-size. When generating Rsa type Key, bits need to be provided.
func GenerateRSAKeyPair ¶
GenerateRSAKeyPair generates a new rsa private and public key.
func GenerateSecp256k1Key ¶
GenerateSecp256k1Key generates a new Secp256k1 private and public key pair
func KeyEqual ¶
KeyEqual checks whether two Keys are equivalent (have identical byte representations).
func MarshalECDSAPrivateKey ¶
func MarshalECDSAPrivateKey(ePri ECDSAPrivateKey) (res []byte, err error)
MarshalECDSAPrivateKey returns x509 bytes from a private key.
func MarshalECDSAPublicKey ¶
func MarshalECDSAPublicKey(ePub ECDSAPublicKey) (res []byte, err error)
MarshalECDSAPublicKey returns x509 bytes from a public key.
func PEMEncodeECDSAPrivateKey ¶
func PEMEncodeECDSAPrivateKey(key *ECDSAPrivateKey) ([]byte, error)
PEMEncodeECDSAPrivateKey encodes an ECDSA private key into PEM format.
func PEMEncodeECDSAPublicKey ¶
func PEMEncodeECDSAPublicKey(pk *ECDSAPublicKey) ([]byte, error)
PEMEncodeECDSAPublicKey encodes an ECDSA public key into PEM format.
func PEMEncodeEd25519PrivateKey ¶
func PEMEncodeEd25519PrivateKey(key *Ed25519PrivateKey) ([]byte, error)
PEMEncodeEd25519PrivateKey encodes an Ed25519 private key into PEM format.
func PEMEncodeEd25519PublicKey ¶
func PEMEncodeEd25519PublicKey(pk *Ed25519PublicKey) ([]byte, error)
PEMEncodeEd25519PublicKey encodes an Ed25519 public key into PEM format.
func PEMEncodePriKey ¶
PEMEncodePriKey encodes a private key into PEM format based on the key's type.
func PEMEncodePubKey ¶
PEMEncodePubKey encodes a public key into PEM format based on the key's type.
func PEMEncodeRsaPrivateKey ¶
func PEMEncodeRsaPrivateKey(key *RsaPrivateKey) ([]byte, error)
PEMEncodeRsaPrivateKey encodes an Rsa private key into PEM format.
func PEMEncodeRsaPublicKey ¶
func PEMEncodeRsaPublicKey(pk *RsaPublicKey) ([]byte, error)
PEMEncodeRsaPublicKey encodes an Rsa public key into PEM format.
func PEMEncodeSecp256k1PrivateKey ¶
func PEMEncodeSecp256k1PrivateKey(key *Secp256k1PrivateKey) ([]byte, error)
PEMEncodeSecp256k1PrivateKey encodes an Secp256k1 private key into PEM format.
func PEMEncodeSecp256k1PublicKey ¶
func PEMEncodeSecp256k1PublicKey(pk *Secp256k1PublicKey) ([]byte, error)
PEMEncodeSecp256k1PublicKey encodes an Secp256k1 public key into PEM format.
func ProtoMarshalPriKey ¶
ProtoMarshalPriKey converts a key object into its protobuf serialized form.
func ProtoMarshalPubKey ¶
ProtoMarshalPubKey converts a public key object into a protobuf serialized public key.
Types ¶
type ECDSAPrivateKey ¶
type ECDSAPrivateKey struct {
// contains filtered or unexported fields
}
ECDSAPrivateKey is an implementation of an ECDSA private key.
func PEMDecodeECDSAPrivateKey ¶
func PEMDecodeECDSAPrivateKey(pemBytes []byte) (*ECDSAPrivateKey, error)
PEMDecodeECDSAPrivateKey decodes a PEM-encoded ECDSA private key.
func (*ECDSAPrivateKey) Equals ¶
func (ePri *ECDSAPrivateKey) Equals(o Key) bool
Equals compares two private keys.
func (*ECDSAPrivateKey) GetPublic ¶
func (ePri *ECDSAPrivateKey) GetPublic() PubKey
GetPublic returns a public key.
func (*ECDSAPrivateKey) Raw ¶
func (ePri *ECDSAPrivateKey) Raw() (res []byte, err error)
Raw returns x509 bytes from a private key.
func (*ECDSAPrivateKey) Sign ¶
func (ePri *ECDSAPrivateKey) Sign(data []byte) (sig []byte, err error)
Sign returns the signature of the input data.
func (*ECDSAPrivateKey) Type ¶
func (ePri *ECDSAPrivateKey) Type() pb.KeyType
Type returns the key type.
type ECDSAPublicKey ¶
type ECDSAPublicKey struct {
// contains filtered or unexported fields
}
ECDSAPublicKey is an implementation of an ECDSA public key.
func PEMDecodeECDSAPublicKey ¶
func PEMDecodeECDSAPublicKey(pemBytes []byte) (*ECDSAPublicKey, error)
PEMDecodeECDSAPublicKey decodes a PEM-encoded ECDSA public key.
func (*ECDSAPublicKey) Equals ¶
func (ePub *ECDSAPublicKey) Equals(o Key) bool
Equals compares to public keys.
func (*ECDSAPublicKey) Raw ¶
func (ePub *ECDSAPublicKey) Raw() ([]byte, error)
Raw returns x509 bytes from a public key.
func (*ECDSAPublicKey) Type ¶
func (ePub *ECDSAPublicKey) Type() pb.KeyType
Type returns the key type.
type Ed25519PrivateKey ¶
type Ed25519PrivateKey struct {
// contains filtered or unexported fields
}
Ed25519PrivateKey is an ed25519 private key.
func PEMDecodeEd25519PrivateKey ¶
func PEMDecodeEd25519PrivateKey(pemBytes []byte) (*Ed25519PrivateKey, error)
PEMDecodeEd25519PrivateKey decodes a PEM-encoded Ed25519 private key.
func (*Ed25519PrivateKey) Equals ¶
func (k *Ed25519PrivateKey) Equals(o Key) bool
Equals compares two ed25519 private keys.
func (*Ed25519PrivateKey) GetPublic ¶
func (k *Ed25519PrivateKey) GetPublic() PubKey
GetPublic returns an ed25519 public key from a private key.
func (*Ed25519PrivateKey) Raw ¶
func (k *Ed25519PrivateKey) Raw() ([]byte, error)
Raw private key bytes.
func (*Ed25519PrivateKey) Sign ¶
func (k *Ed25519PrivateKey) Sign(msg []byte) (res []byte, err error)
Sign returns a signature from an input message.
func (*Ed25519PrivateKey) Type ¶
func (k *Ed25519PrivateKey) Type() pb.KeyType
Type of the private key (Ed25519).
type Ed25519PublicKey ¶
type Ed25519PublicKey struct {
// contains filtered or unexported fields
}
Ed25519PublicKey is an ed25519 public key.
func PEMDecodeEd25519PublicKey ¶
func PEMDecodeEd25519PublicKey(pemBytes []byte) (*Ed25519PublicKey, error)
PEMDecodeEd25519PublicKey decodes a PEM-encoded Ed25519 public key.
func (*Ed25519PublicKey) Equals ¶
func (k *Ed25519PublicKey) Equals(o Key) bool
Equals compares two ed25519 public keys.
func (*Ed25519PublicKey) Raw ¶
func (k *Ed25519PublicKey) Raw() ([]byte, error)
Raw public key bytes.
func (*Ed25519PublicKey) Type ¶
func (k *Ed25519PublicKey) Type() pb.KeyType
Type of the public key (Ed25519).
type Key ¶
type Key interface { // Equals checks whether two PubKeys are the same. Equals(Key) bool // Raw returns the raw bytes of the key (not wrapped). // // This function is the inverse of {Pri,Pub}KeyUnmarshaler. Raw() ([]byte, error) // Type returns the protobuf key type. Type() pb.KeyType }
Key represents a crypto key that can be compared to another key.
type PriKey ¶
type PriKey interface { Key // Sign signs the given bytes. Sign([]byte) ([]byte, error) // GetPublic returns a public key paired with this private key. GetPublic() PubKey }
PriKey represents a private key that can be used to generate a public key and sign data.
func PEMDecodePriKey ¶
PEMDecodePriKey decodes a PEM-encoded private key based on the key's type.
func PriKeyFromProto ¶
func PriKeyFromProto(pbPriKey *pb.PrivateKey) (PriKey, error)
PriKeyFromProto converts an un-serialized protobuf PrivateKey message into its representative object.
func ProtoUnmarshalPriKey ¶
ProtoUnmarshalPriKey converts a protobuf serialized private key into its representative object.
func UnmarshalECDSAPrivateKey ¶
UnmarshalECDSAPrivateKey returns a private key from x509 bytes.
func UnmarshalEd25519PrivateKey ¶
UnmarshalEd25519PrivateKey returns a private key from input bytes.
func UnmarshalRsaPrivateKey ¶
UnmarshalRsaPrivateKey returns a private key from the input x509 bytes.
func UnmarshalSecp256k1PrivateKey ¶
UnmarshalSecp256k1PrivateKey returns a private key from bytes
type PriKeyUnmarshaler ¶
PriKeyUnmarshaler is a func that creates a PriKey from a given slice of bytes.
type PubKey ¶
type PubKey interface { Key // Verify that 'sig' is the signed hash of 'data'. Verify(data []byte, sig []byte) (bool, error) }
PubKey is a public key that can be used to verify data signed with the corresponding private key.
func ECDSAPublicKeyFromPubKey ¶
ECDSAPublicKeyFromPubKey generates a new ecdsa public key from an input public key.
func PEMDecodePubKey ¶
PEMDecodePubKey decodes a PEM-encoded public key based on the key's type.
func ProtoUnmarshalPubKey ¶
ProtoUnmarshalPubKey converts a protobuf serialized public key into its representative object.
func PubKeyFromProto ¶
PubKeyFromProto converts an un-serialized protobuf PublicKey message into its representative object.
func UnmarshalECDSAPublicKey ¶
UnmarshalECDSAPublicKey returns the public key from x509 bytes.
func UnmarshalEd25519PublicKey ¶
UnmarshalEd25519PublicKey returns a public key from input bytes.
func UnmarshalRsaPublicKey ¶
UnmarshalRsaPublicKey returns a public key from the input x509 bytes.
func UnmarshalSecp256k1PublicKey ¶
UnmarshalSecp256k1PublicKey returns a public key from bytes
type PubKeyUnmarshaler ¶
PubKeyUnmarshaler is a func that creates a PubKey from a given slice of bytes.
type RsaPrivateKey ¶
type RsaPrivateKey struct {
// contains filtered or unexported fields
}
RsaPrivateKey is a rsa private key.
func PEMDecodeRsaPrivateKey ¶
func PEMDecodeRsaPrivateKey(pemBytes []byte) (*RsaPrivateKey, error)
PEMDecodeRsaPrivateKey decodes a PEM-encoded Rsa private key.
func (*RsaPrivateKey) Equals ¶
func (sk *RsaPrivateKey) Equals(k Key) bool
Equals checks whether this key is equal to another.
func (*RsaPrivateKey) GetPublic ¶
func (sk *RsaPrivateKey) GetPublic() PubKey
GetPublic returns a public key.
func (*RsaPrivateKey) Raw ¶
func (sk *RsaPrivateKey) Raw() (res []byte, err error)
func (*RsaPrivateKey) Sign ¶
func (sk *RsaPrivateKey) Sign(message []byte) (sig []byte, err error)
Sign returns a signature of the input data.
func (*RsaPrivateKey) Type ¶
func (sk *RsaPrivateKey) Type() pb.KeyType
type RsaPublicKey ¶
type RsaPublicKey struct {
// contains filtered or unexported fields
}
RsaPublicKey is a rsa public key.
func PEMDecodeRsaPublicKey ¶
func PEMDecodeRsaPublicKey(pemBytes []byte) (*RsaPublicKey, error)
PEMDecodeRsaPublicKey decodes a PEM-encoded Rsa public key.
func (*RsaPublicKey) Equals ¶
func (pk *RsaPublicKey) Equals(k Key) bool
Equals checks whether this key is equal to another.
func (*RsaPublicKey) Raw ¶
func (pk *RsaPublicKey) Raw() (res []byte, err error)
func (*RsaPublicKey) Type ¶
func (pk *RsaPublicKey) Type() pb.KeyType
type Secp256k1PrivateKey ¶
type Secp256k1PrivateKey secp256k1.PrivateKey
Secp256k1PrivateKey is a Secp256k1 private key
func PEMDecodeSecp256k1PrivateKey ¶
func PEMDecodeSecp256k1PrivateKey(pemBytes []byte) (*Secp256k1PrivateKey, error)
PEMDecodeSecp256k1PrivateKey decodes a PEM-encoded Secp256k1 private key.
func (*Secp256k1PrivateKey) Equals ¶
func (k *Secp256k1PrivateKey) Equals(o Key) bool
Equals compares two private keys
func (*Secp256k1PrivateKey) GetPublic ¶
func (k *Secp256k1PrivateKey) GetPublic() PubKey
GetPublic returns a public key
func (*Secp256k1PrivateKey) Raw ¶
func (k *Secp256k1PrivateKey) Raw() ([]byte, error)
Raw returns the bytes of the key
func (*Secp256k1PrivateKey) Sign ¶
func (k *Secp256k1PrivateKey) Sign(data []byte) (_sig []byte, err error)
Sign returns a signature from input data
func (*Secp256k1PrivateKey) Type ¶
func (k *Secp256k1PrivateKey) Type() pb.KeyType
Type returns the private key type
type Secp256k1PublicKey ¶
type Secp256k1PublicKey secp256k1.PublicKey
Secp256k1PublicKey is a Secp256k1 public key
func PEMDecodeSecp256k1PublicKey ¶
func PEMDecodeSecp256k1PublicKey(pemBytes []byte) (*Secp256k1PublicKey, error)
PEMDecodeSecp256k1PublicKey decodes a PEM-encoded Secp256k1 public key.
func (*Secp256k1PublicKey) Equals ¶
func (k *Secp256k1PublicKey) Equals(o Key) bool
Equals compares two public keys
func (*Secp256k1PublicKey) Raw ¶
func (k *Secp256k1PublicKey) Raw() (res []byte, err error)
Raw returns the bytes of the key
func (*Secp256k1PublicKey) Type ¶
func (k *Secp256k1PublicKey) Type() pb.KeyType
Type returns the public key type