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());
}
}
Aggregations