Search in sources :

Example 1 with MQVuserKeyingMaterial

use of com.github.zhenwei.pkix.util.asn1.cms.ecc.MQVuserKeyingMaterial in project LinLong-Java by zhenwei1108.

the class JceKeyAgreeRecipient method calculateAgreedWrapKey.

private SecretKey calculateAgreedWrapKey(AlgorithmIdentifier keyEncAlg, AlgorithmIdentifier wrapAlg, PublicKey senderPublicKey, ASN1OctetString userKeyingMaterial, PrivateKey receiverPrivateKey, KeyMaterialGenerator kmGen) throws CMSException, GeneralSecurityException, IOException {
    receiverPrivateKey = CMSUtils.cleanPrivateKey(receiverPrivateKey);
    if (CMSUtils.isMQV(keyEncAlg.getAlgorithm())) {
        MQVuserKeyingMaterial ukm = MQVuserKeyingMaterial.getInstance(userKeyingMaterial.getOctets());
        SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(getPrivateKeyAlgorithmIdentifier(), ukm.getEphemeralPublicKey().getPublicKey().getBytes());
        X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubInfo.getEncoded());
        KeyFactory fact = helper.createKeyFactory(keyEncAlg.getAlgorithm());
        PublicKey ephemeralKey = fact.generatePublic(pubSpec);
        KeyAgreement agreement = helper.createKeyAgreement(keyEncAlg.getAlgorithm());
        byte[] ukmKeyingMaterial = (ukm.getAddedukm() != null) ? ukm.getAddedukm().getOctets() : null;
        if (kmGen == old_ecc_cms_Generator) {
            ukmKeyingMaterial = old_ecc_cms_Generator.generateKDFMaterial(wrapAlg, keySizeProvider.getKeySize(wrapAlg), ukmKeyingMaterial);
        }
        agreement.init(receiverPrivateKey, new MQVParameterSpec(receiverPrivateKey, ephemeralKey, ukmKeyingMaterial));
        agreement.doPhase(senderPublicKey, true);
        return agreement.generateSecret(wrapAlg.getAlgorithm().getId());
    } else {
        KeyAgreement agreement = helper.createKeyAgreement(keyEncAlg.getAlgorithm());
        UserKeyingMaterialSpec userKeyingMaterialSpec = null;
        if (CMSUtils.isEC(keyEncAlg.getAlgorithm())) {
            if (userKeyingMaterial != null) {
                byte[] ukmKeyingMaterial = kmGen.generateKDFMaterial(wrapAlg, keySizeProvider.getKeySize(wrapAlg), userKeyingMaterial.getOctets());
                userKeyingMaterialSpec = new UserKeyingMaterialSpec(ukmKeyingMaterial);
            } else {
                byte[] ukmKeyingMaterial = kmGen.generateKDFMaterial(wrapAlg, keySizeProvider.getKeySize(wrapAlg), null);
                userKeyingMaterialSpec = new UserKeyingMaterialSpec(ukmKeyingMaterial);
            }
        } else if (CMSUtils.isRFC2631(keyEncAlg.getAlgorithm())) {
            if (userKeyingMaterial != null) {
                userKeyingMaterialSpec = new UserKeyingMaterialSpec(userKeyingMaterial.getOctets());
            }
        } else if (CMSUtils.isGOST(keyEncAlg.getAlgorithm())) {
            if (userKeyingMaterial != null) {
                userKeyingMaterialSpec = new UserKeyingMaterialSpec(userKeyingMaterial.getOctets());
            }
        } else {
            throw new CMSException("Unknown key agreement algorithm: " + keyEncAlg.getAlgorithm());
        }
        agreement.init(receiverPrivateKey, userKeyingMaterialSpec);
        agreement.doPhase(senderPublicKey, true);
        return agreement.generateSecret(wrapAlg.getAlgorithm().getId());
    }
}
Also used : MQVuserKeyingMaterial(com.github.zhenwei.pkix.util.asn1.cms.ecc.MQVuserKeyingMaterial) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyAgreement(javax.crypto.KeyAgreement) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) MQVParameterSpec(com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec) KeyFactory(java.security.KeyFactory) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Aggregations

SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 CMSException (com.github.zhenwei.pkix.cms.CMSException)1 MQVuserKeyingMaterial (com.github.zhenwei.pkix.util.asn1.cms.ecc.MQVuserKeyingMaterial)1 MQVParameterSpec (com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec)1 UserKeyingMaterialSpec (com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec)1 KeyFactory (java.security.KeyFactory)1 PublicKey (java.security.PublicKey)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 KeyAgreement (javax.crypto.KeyAgreement)1