Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func (c *Context) GenerateECDSAKeyPairWithAttributes(public, private AttributeSe
pkcs11.NewAttribute(pkcs11.CKA_SIGN, true),
pkcs11.NewAttribute(pkcs11.CKA_SENSITIVE, true),
pkcs11.NewAttribute(pkcs11.CKA_EXTRACTABLE, false),
pkcs11.NewAttribute(pkcs11.CKA_DERIVE, true),
})

mech := []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_ECDSA_KEY_PAIR_GEN, nil)}
Expand Down Expand Up @@ -290,6 +291,53 @@ func (c *Context) GenerateECDSAKeyPairWithAttributes(public, private AttributeSe
return k, err
}

// Perform CKM_ECDH1_DERIVE function with the given public key.
//
// This implementation set the derivation parameters to CKD_NULL and therefore
// does not support ANSI X9.63 key ECDH derivation.
func (c *Context) ECDH1Derive(pk Signer, pubkey *ecdsa.PublicKey) ([]byte, error) {
ecpk, ok := pk.(*pkcs11PrivateKeyECDSA)
if !ok {
return nil, errors.New("Private key is not ECDSA")
}

template := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, false),
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_SECRET_KEY),
pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, pkcs11.CKK_GENERIC_SECRET),
pkcs11.NewAttribute(pkcs11.CKA_SENSITIVE, false),
pkcs11.NewAttribute(pkcs11.CKA_EXTRACTABLE, true),
pkcs11.NewAttribute(pkcs11.CKA_ENCRYPT, true),
pkcs11.NewAttribute(pkcs11.CKA_DECRYPT, true),
pkcs11.NewAttribute(pkcs11.CKA_WRAP, true),
pkcs11.NewAttribute(pkcs11.CKA_UNWRAP, true),
pkcs11.NewAttribute(pkcs11.CKA_VALUE_LEN, (pubkey.Curve.Params().BitSize+7)/8),
}
params := pkcs11.ECDH1DeriveParams{KDF: pkcs11.CKD_NULL, PublicKeyData: elliptic.Marshal(pubkey.Curve, pubkey.X, pubkey.Y)}
mech := []*pkcs11.Mechanism{
pkcs11.NewMechanism(pkcs11.CKM_ECDH1_DERIVE, &params),
}

var buf []byte
err := c.withSession(func(session *pkcs11Session) error {
handle, err := session.ctx.DeriveKey(session.handle, mech, ecpk.handle, template)
if err != nil {
return err
}

template = []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_VALUE, nil),
}
attr, err := session.ctx.GetAttributeValue(session.handle, handle, template)
if err != nil {
return err
}
buf = attr[0].Value
return nil
})
return buf, err
}

// Sign signs a message using an ECDSA key.
//
// This completes the implemention of crypto.Signer for pkcs11PrivateKeyECDSA.
Expand Down
25 changes: 25 additions & 0 deletions ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,28 @@ func TestEcdsaRequiredArgs(t *testing.T) {
_, err = ctx.GenerateECDSAKeyPairWithLabel(val, nil, elliptic.P224())
require.Error(t, err)
}

func TestEcdsaECDH1Derive(t *testing.T) {
ctx, err := ConfigureFromFile("config")
require.NoError(t, err)

defer func() {
require.NoError(t, ctx.Close())
}()

key1, err := ctx.GenerateECDSAKeyPair(randomBytes(), elliptic.P256())
require.NoError(t, err)
require.NotNil(t, key1)
defer key1.Delete()

key2, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)

d1, err := ctx.ECDH1Derive(key1, key2.Public().(crypto.PublicKey).(*ecdsa.PublicKey))
require.NoError(t, err)

pub := key1.Public().(crypto.PublicKey).(*ecdsa.PublicKey)
d2, _ := pub.Curve.ScalarMult(pub.X, pub.Y, key2.D.Bytes())

require.Equal(t, d1, d2.Bytes())
}