IAIK PKCS#11包装器:ECDH KeyAgreement示例

问题描述 投票:1回答:1

我尝试使用IAIK PKCS#11包装器(https://jce.iaik.tugraz.at/sic/Products/Core_Crypto_Toolkits/PKCS_11_Wrapper)执行一些ECDH密钥协议,并且不使用JCE提供程序。到目前为止,我没有找到任何示例,特别是设置密钥模板和机制(以及机制参数)。

您是否有一些示例,如何使用BouncyCastle以最佳方式执行此操作并验证结果?

谢谢!

java pkcs#11 iaik-jce ecdh
1个回答
1
投票

最后,我让它独立工作。

首先要知道,IAIK PKCS#11包装器不支持PKCS#11的所有密钥派生功能。 DHKeyDerivationParameters.KeyDerivationFunctionType指定它支持的内容,遗憾的是,虽然提供了long,但它会检查值是否已知,因此您不能简单地提供为其他KDF函数定义的值。但是,如果您的PKCS#11模块支持它,您可以使用DHKeyDerivationParameters.KeyDerivationFunctionType.NULL并自行进行派生。

对于以下代码片段,让session为一些iaik.pkcs.pkcs11.Session,经过适当的身份验证以使用所选的ECDH密钥。

执行以下操作以获取密钥,在本例中为AES(2Des和3DES或其他AES长度大致相同):

final long CKA_VALUE_LEN = 0x00000161;

byte[] deriveKey(byte[] publicKey, byte[] salt, long keyDerivationFunction) throws Exception {
    // setting up mechanism:
    EcDH1KeyDerivationParameters params = new EcDH1KeyDerivationParameters(keyDerivationFunction, salt, publicKey);
    Mechanism mechanism = Mechanism.get(PKCS11Constants.CKM_ECDH1_DERIVE );
    mechanism.setParameters(params);

    // setting up keyTemplate, specifying how the derived key looks like:
    Key keyTemplate = new AESSecretKey();
    keyTemplate.putAttribute(CKA_VALUE_LEN, new Long(32));

    AESSecretKey derivedKey = ((AESSecretKey)session.deriveKey(mechanism, key, keyTemplate));
    return derivedKey.getValue().getByteArrayValue();
}

要检索普通ECDH共享密钥,请执行以下操作:

byte[] getSharedSecret(byte[] publicKey) throws Exception{
    // setting up mechanism:
    EcDH1KeyDerivationParameters params = new EcDH1KeyDerivationParameters(DHKeyDerivationParameters.KeyDerivationFunctionType.NULL, null, publicKey);
    Mechanism mechanism = Mechanism.get(PKCS11Constants.CKM_ECDH1_DERIVE );
    mechanism.setParameters(params);

    // four our PKCS#11 module, using a GenericSecretKey without length returns
    // the complete derived secret:
    Key keyTemplate = new GenericSecretKey();

    GenericSecretKey derivedKey = ((GenericSecretKey)session.deriveKey(mechanism, key, keyTemplate));
    return derivedKey.getValue().getByteArrayValue();
}

要执行“其他”并验证派生值是否符合预期,您可以使用BouncyCastle和以下代码:

void testKeyDerivation(ECPublicKey otherPublic, byte[] salt) throws Exception{
    // create some keypair, which fits to the EC key, IAIK is using:        
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
    keyGen.initialize(otherPublic.getParams());
    KeyPair testKeyPair = keyGen.generateKeyPair();
    ECPublicKey publicTestKey = (ECPublicKey) testKeyPair.getPublic();

    // convert the JCE Publickey to the required format, using BouncyCastle:
    byte[] encodedPublicTestKey = EC5Util.convertPoint(publicTestKey.getParams(), publicTestKey.getW(),false).getEncoded(false);
    // format is 0x04 X Y where X and Y are byte[], containing the (padded) coordinates of the point, 
    // specifying the public key    


    // in fact, you need to do only one of these, but I want to show, how both works:
    byte[] iaikDerivedKey =  deriveKey(encodedPublicTestKey, salt, DHKeyDerivationParameters.KeyDerivationFunctionType.SHA1_KDF);
    byte[] iaikDerivedSecret =  getSharedSecret(encodedPublicTestKey);


    // verify that both sides indeed agree:
    KeyAgreement ka = KeyAgreement.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
    ka.init(testKeyPair.getPrivate());
    ka.doPhase(otherPublic, true);
    byte [] secret = ka.generateSecret();

    Assert.assertTrue(Arrays.equals(iaikDerivedSecret,  secret));

    Digest digest = new SHA1Digest();
    KDF2BytesGenerator kdf = new KDF2BytesGenerator(digest);
    DerivationParameters derivationParameters = new KDFParameters(secret,salt);

    kdf.init(derivationParameters);
    byte[] derivedKey = new byte[iaikDerivedKey.length];
    kdf.generateBytes(derivedKey, 0, iaikDerivedKey.length);
    Assert.assertTrue(Arrays.equals(iaikDerivedKey,  derivedKey));
}

这对我使用IAIK PKCS#11包装版本1.5和BouncyCastle版本1.59,使用我的公司PKCS#11中间件和一些智能卡。我希望它也可以帮助其他人,尝试做同样的事情。

© www.soinside.com 2019 - 2024. All rights reserved.