Search in sources :

Example 1 with MQVParameterSpec

use of com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec in project LinLong-Java by zhenwei1108.

the class KeyAgreementSpi method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (!(key instanceof DHPrivateKey)) {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
    }
    DHPrivateKey privKey = (DHPrivateKey) key;
    if (params != null) {
        if (// p, g override.
        params instanceof DHParameterSpec) {
            DHParameterSpec p = (DHParameterSpec) params;
            this.p = p.getP();
            this.g = p.getG();
            this.dheParameters = null;
            this.ukmParameters = null;
        } else if (params instanceof DHUParameterSpec) {
            if (unifiedAgreement == null) {
                throw new InvalidAlgorithmParameterException("agreement algorithm not DHU based");
            }
            this.p = privKey.getParams().getP();
            this.g = privKey.getParams().getG();
            this.dheParameters = (DHUParameterSpec) params;
            this.ukmParameters = ((DHUParameterSpec) params).getUserKeyingMaterial();
            if (dheParameters.getEphemeralPublicKey() != null) {
                unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(dheParameters.getEphemeralPublicKey())));
            } else {
                unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey())));
            }
        } else if (params instanceof MQVParameterSpec) {
            if (mqvAgreement == null) {
                throw new InvalidAlgorithmParameterException("agreement algorithm not MQV based");
            }
            this.p = privKey.getParams().getP();
            this.g = privKey.getParams().getG();
            this.mqvParameters = (MQVParameterSpec) params;
            this.ukmParameters = ((MQVParameterSpec) params).getUserKeyingMaterial();
            if (mqvParameters.getEphemeralPublicKey() != null) {
                mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(mqvParameters.getEphemeralPublicKey())));
            } else {
                mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey())));
            }
        } else if (params instanceof UserKeyingMaterialSpec) {
            if (kdf == null) {
                throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
            }
            this.p = privKey.getParams().getP();
            this.g = privKey.getParams().getG();
            this.dheParameters = null;
            this.ukmParameters = ((UserKeyingMaterialSpec) params).getUserKeyingMaterial();
        } else {
            throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
        }
    } else {
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
    }
    this.x = privKey.getX();
    this.result = bigIntToBytes(x);
}
Also used : DHMQVPrivateParameters(com.github.zhenwei.core.crypto.params.DHMQVPrivateParameters) DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHUPrivateParameters(com.github.zhenwei.core.crypto.params.DHUPrivateParameters) DHUParameterSpec(com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec) DHParameterSpec(javax.crypto.spec.DHParameterSpec) InvalidKeyException(java.security.InvalidKeyException) MQVParameterSpec(com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec)

Example 2 with MQVParameterSpec

use of com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec 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)

Example 3 with MQVParameterSpec

use of com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec in project LinLong-Java by zhenwei1108.

the class JceKeyAgreeRecipientInfoGenerator method generateRecipientEncryptedKeys.

public ASN1Sequence generateRecipientEncryptedKeys(AlgorithmIdentifier keyAgreeAlgorithm, AlgorithmIdentifier keyEncryptionAlgorithm, GenericKey contentEncryptionKey) throws CMSException {
    if (recipientIDs.isEmpty()) {
        throw new CMSException("No recipients associated with generator - use addRecipient()");
    }
    init(keyAgreeAlgorithm.getAlgorithm());
    PrivateKey senderPrivateKey = this.senderPrivateKey;
    ASN1ObjectIdentifier keyAgreementOID = keyAgreeAlgorithm.getAlgorithm();
    ASN1EncodableVector recipientEncryptedKeys = new ASN1EncodableVector();
    for (int i = 0; i != recipientIDs.size(); i++) {
        PublicKey recipientPublicKey = (PublicKey) recipientKeys.get(i);
        KeyAgreeRecipientIdentifier karId = (KeyAgreeRecipientIdentifier) recipientIDs.get(i);
        try {
            AlgorithmParameterSpec agreementParamSpec;
            ASN1ObjectIdentifier keyEncAlg = keyEncryptionAlgorithm.getAlgorithm();
            if (CMSUtils.isMQV(keyAgreementOID)) {
                agreementParamSpec = new MQVParameterSpec(ephemeralKP, recipientPublicKey, userKeyingMaterial);
            } else if (CMSUtils.isEC(keyAgreementOID)) {
                byte[] ukmKeyingMaterial = ecc_cms_Generator.generateKDFMaterial(keyEncryptionAlgorithm, keySizeProvider.getKeySize(keyEncAlg), userKeyingMaterial);
                agreementParamSpec = new UserKeyingMaterialSpec(ukmKeyingMaterial);
            } else if (CMSUtils.isRFC2631(keyAgreementOID)) {
                if (userKeyingMaterial != null) {
                    agreementParamSpec = new UserKeyingMaterialSpec(userKeyingMaterial);
                } else {
                    if (keyAgreementOID.equals(PKCSObjectIdentifiers.id_alg_SSDH)) {
                        throw new CMSException("User keying material must be set for static keys.");
                    }
                    agreementParamSpec = null;
                }
            } else if (CMSUtils.isGOST(keyAgreementOID)) {
                if (userKeyingMaterial != null) {
                    agreementParamSpec = new UserKeyingMaterialSpec(userKeyingMaterial);
                } else {
                    throw new CMSException("User keying material must be set for static keys.");
                }
            } else {
                throw new CMSException("Unknown key agreement algorithm: " + keyAgreementOID);
            }
            // Use key agreement to choose a wrap key for this recipient
            KeyAgreement keyAgreement = helper.createKeyAgreement(keyAgreementOID);
            keyAgreement.init(senderPrivateKey, agreementParamSpec, random);
            keyAgreement.doPhase(recipientPublicKey, true);
            SecretKey keyEncryptionKey = keyAgreement.generateSecret(keyEncAlg.getId());
            // Wrap the content encryption key with the agreement key
            Cipher keyEncryptionCipher = helper.createCipher(keyEncAlg);
            ASN1OctetString encryptedKey;
            if (keyEncAlg.equals(CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap) || keyEncAlg.equals(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap)) {
                keyEncryptionCipher.init(Cipher.WRAP_MODE, keyEncryptionKey, new GOST28147WrapParameterSpec(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_A_ParamSet, userKeyingMaterial));
                byte[] encKeyBytes = keyEncryptionCipher.wrap(helper.getJceKey(contentEncryptionKey));
                Gost2814789EncryptedKey encKey = new Gost2814789EncryptedKey(Arrays.copyOfRange(encKeyBytes, 0, encKeyBytes.length - 4), Arrays.copyOfRange(encKeyBytes, encKeyBytes.length - 4, encKeyBytes.length));
                encryptedKey = new DEROctetString(encKey.getEncoded(ASN1Encoding.DER));
            } else {
                keyEncryptionCipher.init(Cipher.WRAP_MODE, keyEncryptionKey, random);
                byte[] encryptedKeyBytes = keyEncryptionCipher.wrap(helper.getJceKey(contentEncryptionKey));
                encryptedKey = new DEROctetString(encryptedKeyBytes);
            }
            recipientEncryptedKeys.add(new RecipientEncryptedKey(karId, encryptedKey));
        } catch (GeneralSecurityException e) {
            throw new CMSException("cannot perform agreement step: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new CMSException("unable to encode wrapped key: " + e.getMessage(), e);
        }
    }
    return new DERSequence(recipientEncryptedKeys);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) PrivateKey(java.security.PrivateKey) OriginatorPublicKey(com.github.zhenwei.pkix.util.asn1.cms.OriginatorPublicKey) PublicKey(java.security.PublicKey) GOST28147WrapParameterSpec(com.github.zhenwei.provider.jcajce.spec.GOST28147WrapParameterSpec) RecipientEncryptedKey(com.github.zhenwei.pkix.util.asn1.cms.RecipientEncryptedKey) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) Gost2814789EncryptedKey(com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey) SecretKey(javax.crypto.SecretKey) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) Cipher(javax.crypto.Cipher) KeyAgreement(javax.crypto.KeyAgreement) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) MQVParameterSpec(com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) CMSException(com.github.zhenwei.pkix.cms.CMSException) KeyAgreeRecipientIdentifier(com.github.zhenwei.pkix.util.asn1.cms.KeyAgreeRecipientIdentifier)

Example 4 with MQVParameterSpec

use of com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec in project LinLong-Java by zhenwei1108.

the class KeyAgreementSpi method initFromKey.

private void initFromKey(Key key, AlgorithmParameterSpec parameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (agreement instanceof ECMQVBasicAgreement) {
        mqvParameters = null;
        if (!(key instanceof MQVPrivateKey) && !(parameterSpec instanceof MQVParameterSpec)) {
            throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement requires " + getSimpleName(MQVParameterSpec.class) + " for initialisation");
        }
        ECPrivateKeyParameters staticPrivKey;
        ECPrivateKeyParameters ephemPrivKey;
        ECPublicKeyParameters ephemPubKey;
        if (key instanceof MQVPrivateKey) {
            MQVPrivateKey mqvPrivKey = (MQVPrivateKey) key;
            staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey());
            ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey());
            ephemPubKey = null;
            if (mqvPrivKey.getEphemeralPublicKey() != null) {
                ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey());
            }
        } else {
            MQVParameterSpec mqvParameterSpec = (MQVParameterSpec) parameterSpec;
            staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
            ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvParameterSpec.getEphemeralPrivateKey());
            ephemPubKey = null;
            if (mqvParameterSpec.getEphemeralPublicKey() != null) {
                ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvParameterSpec.getEphemeralPublicKey());
            }
            mqvParameters = mqvParameterSpec;
            ukmParameters = mqvParameterSpec.getUserKeyingMaterial();
        }
        MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
        this.parameters = staticPrivKey.getParameters();
        // TODO Validate that all the keys are using the same parameters?
        ((ECMQVBasicAgreement) agreement).init(localParams);
    } else if (parameterSpec instanceof DHUParameterSpec) {
        if (!(agreement instanceof ECDHCUnifiedAgreement)) {
            throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement cannot be used with " + getSimpleName(DHUParameterSpec.class));
        }
        DHUParameterSpec dheParameterSpec = (DHUParameterSpec) parameterSpec;
        ECPrivateKeyParameters staticPrivKey;
        ECPrivateKeyParameters ephemPrivKey;
        ECPublicKeyParameters ephemPubKey;
        staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
        ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(dheParameterSpec.getEphemeralPrivateKey());
        ephemPubKey = null;
        if (dheParameterSpec.getEphemeralPublicKey() != null) {
            ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(dheParameterSpec.getEphemeralPublicKey());
        }
        dheParameters = dheParameterSpec;
        ukmParameters = dheParameterSpec.getUserKeyingMaterial();
        ECDHUPrivateParameters localParams = new ECDHUPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
        this.parameters = staticPrivKey.getParameters();
        ((ECDHCUnifiedAgreement) agreement).init(localParams);
    } else {
        if (!(key instanceof PrivateKey)) {
            throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPrivateKey.class) + " for initialisation");
        }
        if (kdf == null && parameterSpec instanceof UserKeyingMaterialSpec) {
            throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
        }
        ECPrivateKeyParameters privKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
        this.parameters = privKey.getParameters();
        ukmParameters = (parameterSpec instanceof UserKeyingMaterialSpec) ? ((UserKeyingMaterialSpec) parameterSpec).getUserKeyingMaterial() : null;
        ((BasicAgreement) agreement).init(privKey);
    }
}
Also used : ECDHCUnifiedAgreement(com.github.zhenwei.core.crypto.agreement.ECDHCUnifiedAgreement) ECDHUPrivateParameters(com.github.zhenwei.core.crypto.params.ECDHUPrivateParameters) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ECPrivateKey(com.github.zhenwei.provider.jce.interfaces.ECPrivateKey) MQVPrivateKey(com.github.zhenwei.provider.jce.interfaces.MQVPrivateKey) PrivateKey(java.security.PrivateKey) DHUParameterSpec(com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec) ECMQVBasicAgreement(com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement) ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) MQVParameterSpec(com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec) MQVPrivateKey(com.github.zhenwei.provider.jce.interfaces.MQVPrivateKey) MQVPrivateParameters(com.github.zhenwei.core.crypto.params.MQVPrivateParameters)

Aggregations

MQVParameterSpec (com.github.zhenwei.provider.jcajce.spec.MQVParameterSpec)4 UserKeyingMaterialSpec (com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec)4 CMSException (com.github.zhenwei.pkix.cms.CMSException)2 DHUParameterSpec (com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 PrivateKey (java.security.PrivateKey)2 PublicKey (java.security.PublicKey)2 KeyAgreement (javax.crypto.KeyAgreement)2 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DERSequence (com.github.zhenwei.core.asn1.DERSequence)1 Gost2814789EncryptedKey (com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 ECDHCUnifiedAgreement (com.github.zhenwei.core.crypto.agreement.ECDHCUnifiedAgreement)1 ECMQVBasicAgreement (com.github.zhenwei.core.crypto.agreement.ECMQVBasicAgreement)1 DHMQVPrivateParameters (com.github.zhenwei.core.crypto.params.DHMQVPrivateParameters)1 DHUPrivateParameters (com.github.zhenwei.core.crypto.params.DHUPrivateParameters)1