diff options
author | Gibheer <gibheer@gmail.com> | 2015-02-15 21:04:59 +0100 |
---|---|---|
committer | Gibheer <gibheer@gmail.com> | 2015-02-15 21:04:59 +0100 |
commit | f9164f3f99edf4ae8343d6c67b915e788a5624f8 (patch) | |
tree | c3a7029eeb1392bcb73d3bbedec837ff46652591 /ecdsa.go |
initial commit for pki
pki is a small library to make building some of the crypto stuff easier
in go.
Diffstat (limited to 'ecdsa.go')
-rw-r--r-- | ecdsa.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/ecdsa.go b/ecdsa.go new file mode 100644 index 0000000..170786b --- /dev/null +++ b/ecdsa.go @@ -0,0 +1,69 @@ +package pkilib + +import ( + "crypto" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/x509" + "encoding/pem" + "errors" +) + +const ( + PemLabelEcdsa = "EC PRIVATE KEY" +) + +type ( + // This type handles the function calls to the ecdsa private key by + // implementing the interface. + EcdsaPrivateKey struct { + private_key *ecdsa.PrivateKey + } + + EcdsaPublicKey struct { + public_key *ecdsa.PublicKey + } +) + +// generate a new ecdsa private key +func NewPrivateKeyEcdsa(curve elliptic.Curve) (*EcdsaPrivateKey, error) { + key, err := ecdsa.GenerateKey(curve, rand.Reader) + if err != nil { return nil, err } + return &EcdsaPrivateKey{key}, nil +} + +// load the private key from the raw data +func LoadPrivateKeyEcdsa(raw []byte) (*EcdsaPrivateKey, error) { + key, err := x509.ParseECPrivateKey(raw) + if err != nil { return nil, err } + return &EcdsaPrivateKey{key}, nil +} + +// derive a public key from the private key +func (pr EcdsaPrivateKey) Public() PublicKey { + return &EcdsaPublicKey{pr.private_key.Public().(*ecdsa.PublicKey)} +} + +// sign a message with the private key +func (pr EcdsaPrivateKey) Sign(message []byte) ([]byte, error) { + return make([]byte, 0), errors.New("not implemented yet!") +} + +// get the private key +func (pr EcdsaPrivateKey) privateKey() crypto.PrivateKey { + return pr.private_key +} + +// implement Pemmer interface +func (pr EcdsaPrivateKey) MarshalPem() (marshalledPemBlock, error) { + asn1, err := x509.MarshalECPrivateKey(pr.private_key) + if err != nil { return nil, err } + pem_block := pem.Block{Type: PemLabelEcdsa, Bytes: asn1} + return pem.EncodeToMemory(&pem_block), nil +} + +// verify a message using the ecdsa public key +func (pu *EcdsaPublicKey) Verify(message []byte, signature []byte) (bool, error) { + return false, errors.New("not implemented yet!") +} |