diff --git a/binding.go b/binding.go index 685e9b804774ee6d8c680237046fe89fd7bfb4cc..986174dd5ccab0adea6934e1ce2e47250d5dc143 100644 --- a/binding.go +++ b/binding.go @@ -5,6 +5,7 @@ package ctidh import "C" import ( "bytes" + "crypto/hmac" "encoding/pem" "fmt" "io/ioutil" @@ -156,6 +157,11 @@ func (p *PublicKey) FromBytes(data []byte) error { return nil } +// Equal is a constant time comparison of the two public keys. +func (p *PublicKey) Equal(publicKey *PublicKey) bool { + return hmac.Equal(p.Bytes(), publicKey.Bytes()) +} + // Blind performs a blinding operation // and mutates the public key. // See notes below about blinding operation with CTIDH. @@ -215,6 +221,11 @@ func (p *PrivateKey) FromBytes(data []byte) error { return nil } +// Equal is a constant time comparison of the two private keys. +func (p *PrivateKey) Equal(privateKey *PrivateKey) bool { + return hmac.Equal(p.Bytes(), privateKey.Bytes()) +} + // ToPEM writes out the PrivateKey to a PEM block. func (p *PrivateKey) ToPEM() (*pem.Block, error) { keyType := Name() + " PRIVATE KEY" @@ -267,6 +278,12 @@ func (p *PrivateKey) FromPEMFile(f string) error { return nil } +// PublicKey returns the public key associated +// with the given private key. +func (p *PrivateKey) PublicKey() *PublicKey { + return DerivePublicKey(p) +} + // DerivePublicKey derives a public key given a private key. func DerivePublicKey(privKey *PrivateKey) *PublicKey { var base C.public_key