// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.kems;

import java.math.BigInteger;
import org.bouncycastle.crypto.CryptoServiceProperties;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
import org.bouncycastle.crypto.CryptoServicePurpose;
import org.bouncycastle.crypto.constraints.ConstraintUtils;
import org.bouncycastle.crypto.DerivationFunction;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.EncapsulatedSecretExtractor;

public class RSAKEMExtractor implements EncapsulatedSecretExtractor
{
    private final RSAKeyParameters privKey;
    private final int keyLen;
    private DerivationFunction kdf;
    
    public RSAKEMExtractor(final RSAKeyParameters privKey, final int keyLen, final DerivationFunction kdf) {
        if (!privKey.isPrivate()) {
            throw new IllegalArgumentException("private key required for encryption");
        }
        this.privKey = privKey;
        this.keyLen = keyLen;
        this.kdf = kdf;
        CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties("RSAKem", ConstraintUtils.bitsOfSecurityFor(this.privKey.getModulus()), privKey, CryptoServicePurpose.DECRYPTION));
    }
    
    @Override
    public byte[] extractSecret(final byte[] magnitude) {
        final BigInteger modulus = this.privKey.getModulus();
        return RSAKEMGenerator.generateKey(this.kdf, modulus, new BigInteger(1, magnitude).modPow(this.privKey.getExponent(), modulus), this.keyLen);
    }
    
    @Override
    public int getEncapsulationLength() {
        return (this.privKey.getModulus().bitLength() + 7) / 8;
    }
}
