func generateECDHAEADPrivateKey(t *testing.T, c commonpb.EllipticCurveType, ptfmt commonpb.EcPointFormat,
	kt ecdhpb.KeyType, encT *tinkpb.KeyTemplate, cek []byte) *ecdhpb.EcdhAeadPrivateKey {
	t.Helper()
	if ecdhpb.KeyType_OKP.String() == kt.String() {
		return buildXChachaKey(t, ptfmt, encT, c, cek)
	}
	curve, err := hybrid.GetCurve(c.String())
	require.NoError(t, err)
	pvt, err := hybrid.GenerateECDHKeyPair(curve)
	require.NoError(t, err)
	pubKey := ecdhAEADPublicKey(t, c, ptfmt, kt, encT, pvt.PublicKey.Point.X.Bytes(), pvt.PublicKey.Point.Y.Bytes(),
		cek)
	return ecdhesAEADPrivateKey(t, pubKey, pvt.D.Bytes())
}
generateECDHAEADPrivateKey generates a new EC key pair and returns the private key proto.
package ecdh
import (
	"crypto/ed25519"
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"testing"
	"github.com/golang/protobuf/proto"
	"github.com/google/tink/go/aead"
	"github.com/google/tink/go/core/primitiveset"
	hybrid "github.com/google/tink/go/hybrid/subtle"
	"github.com/google/tink/go/keyset"
	commonpb "github.com/google/tink/go/proto/common_go_proto"
	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
	"github.com/google/tink/go/signature"
	"github.com/google/tink/go/subtle/random"
	"github.com/google/tink/go/testkeyset"
	"github.com/google/tink/go/testutil"
	"github.com/stretchr/testify/require"
	"github.com/hyperledger/aries-framework-go/pkg/crypto/tinkcrypto/primitive/composite"
	ecdhpb "github.com/hyperledger/aries-framework-go/pkg/crypto/tinkcrypto/primitive/proto/ecdh_aead_go_proto"
	"github.com/hyperledger/aries-framework-go/pkg/internal/cryptoutil"
)
func TestECDHESFactory(t *testing.T) {
	c := commonpb.EllipticCurveType_NIST_P256
	primaryPtFmt := commonpb.EcPointFormat_UNCOMPRESSED
	rawPtFmt := commonpb.EcPointFormat_COMPRESSED
	primaryEncT := aead.AES128GCMKeyTemplate()
	rawEncT := aead.AES256GCMKeyTemplate()
	kt := ecdhpb.KeyType_EC
	cek := random.GetRandomBytes(32)
	primaryPrivProto := generateECDHAEADPrivateKey(t, c, primaryPtFmt, kt, primaryEncT, cek)
	sPrimaryPriv, err := proto.Marshal(primaryPrivProto)
	require.NoError(t, err)
	primaryPrivKey := testutil.NewKey(
		testutil.NewKeyData(nistpECDHKWPrivateKeyTypeURL, sPrimaryPriv, tinkpb.KeyData_ASYMMETRIC_PRIVATE),
		tinkpb.KeyStatusType_ENABLED, 8, tinkpb.OutputPrefixType_RAW)
	rawPrivProto := generateECDHAEADPrivateKey(t, c, rawPtFmt, kt, rawEncT, cek)
	sRawPriv, err := proto.Marshal(rawPrivProto)
	require.NoError(t, err)
	rawPrivKey := testutil.NewKey(
		testutil.NewKeyData(nistpECDHKWPrivateKeyTypeURL, sRawPriv, tinkpb.KeyData_ASYMMETRIC_PRIVATE),
		tinkpb.KeyStatusType_ENABLED, 11, tinkpb.OutputPrefixType_RAW)
	x25519XChachaPrivProto := generateECDHAEADPrivateKey(t, commonpb.EllipticCurveType_CURVE25519, primaryPtFmt,
		ecdhpb.KeyType_OKP, aead.XChaCha20Poly1305KeyTemplate(), cek)
	sX25519XChachaPriv, err := proto.Marshal(x25519XChachaPrivProto)
	require.NoError(t, err)
	x25519XChachaPrivKey := testutil.NewKey(
		testutil.NewKeyData(x25519ECDHKWPrivateKeyTypeURL, sX25519XChachaPriv, tinkpb.KeyData_ASYMMETRIC_PRIVATE),
		tinkpb.KeyStatusType_ENABLED, 15, tinkpb.OutputPrefixType_RAW)
	privKeys := []*tinkpb.Keyset_Key{primaryPrivKey, rawPrivKey, x25519XChachaPrivKey}
	privKeyset := testutil.NewKeyset(privKeys[0].KeyId, privKeys)
	khPriv, err := testkeyset.NewHandle(privKeyset)
	require.NoError(t, err)
	xPrivKeys := []*tinkpb.Keyset_Key{x25519XChachaPrivKey, rawPrivKey, primaryPrivKey}
	xPrivKeyset := testutil.NewKeyset(xPrivKeys[0].KeyId, xPrivKeys)
	xKHPriv, err := testkeyset.NewHandle(xPrivKeyset)
	require.NoError(t, err)
	khPub, err := khPriv.Public()
	require.NoError(t, err)
	xKHPub, err := xKHPriv.Public()
	require.NoError(t, err)
	e, err := NewECDHEncrypt(khPub)
	require.NoError(t, err)
	d, err := NewECDHDecrypt(khPriv)
	require.NoError(t, err)
	xe, err := NewECDHEncrypt(xKHPub)
	require.NoError(t, err)
	xd, err := NewECDHDecrypt(xKHPriv)
	require.NoError(t, err)
	for i := 0; i < 1000; i++ {
		pt := random.GetRandomBytes(20)
		aadRndNb := random.GetRandomBytes(10)
		aadJSON, err := json.Marshal(aadRndNb)
		require.NoError(t, err)
		aad, err := json.Marshal(&map[string]interface{}{"someFiled": json.RawMessage(aadJSON)})
		require.NoError(t, err)
		aadStr := base64.RawURLEncoding.EncodeToString(aad)
		aad = []byte(aadStr)
		ct, err := e.Encrypt(pt, aad)
		require.NoError(t, err)
		xct, err := xe.Encrypt(pt, aad)
		require.NoError(t, err)
		encData := &composite.EncryptedData{}
		err = json.Unmarshal(ct, encData)
		require.NoError(t, err)
		gotpt, err := d.Decrypt(ct, aad)
		require.NoError(t, err)
		xgotpt, err := xd.Decrypt(xct, aad)
		require.NoError(t, err)
		require.EqualValues(t, pt, gotpt)
		require.EqualValues(t, pt, xgotpt)
	}
}
func ecdhAEADPublicKey(t *testing.T, c commonpb.EllipticCurveType, ptfmt commonpb.EcPointFormat, kt ecdhpb.KeyType,
	encT *tinkpb.KeyTemplate, x, y, cek []byte) *ecdhpb.EcdhAeadPublicKey {
	t.Helper()
	return &ecdhpb.EcdhAeadPublicKey{
		Version: 0,
		Params: &ecdhpb.EcdhAeadParams{
			KwParams: &ecdhpb.EcdhKwParams{
				CurveType: c,
				KeyType:   kt,
			},
			EncParams: &ecdhpb.EcdhAeadEncParams{
				AeadEnc: encT,
				CEK:     cek,
			},
			EcPointFormat: ptfmt,
		},
		X: x,
		Y: y,
	}
}
func ecdhesAEADPrivateKey(t *testing.T, p *ecdhpb.EcdhAeadPublicKey, d []byte) *ecdhpb.EcdhAeadPrivateKey {
	t.Helper()
	return &ecdhpb.EcdhAeadPrivateKey{
		Version:   0,
		PublicKey: p,
		KeyValue:  d,
	}
}