2

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

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

谢谢你!

4

1 回答 1

2

最后,我让它自己工作。

首先要知道,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 Wrapper 1.5 版和 BouncyCastle 1.59 版,使用我的公司 PKCS#11 中间件和一些智能卡。我希望它也可以帮助其他人,尝试做同样的事情。

于 2018-02-08T13:10:38.603 回答