Search in sources :

Example 1 with AsymmetricKeyParameter

use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.

the class PKCS1Encoding method init.

public void init(boolean forEncryption, CipherParameters param) {
    AsymmetricKeyParameter kParam;
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom rParam = (ParametersWithRandom) param;
        this.random = rParam.getRandom();
        kParam = (AsymmetricKeyParameter) rParam.getParameters();
    } else {
        kParam = (AsymmetricKeyParameter) param;
        if (!kParam.isPrivate() && forEncryption) {
            this.random = CryptoServicesRegistrar.getSecureRandom();
        }
    }
    engine.init(forEncryption, param);
    this.forPrivateKey = kParam.isPrivate();
    this.forEncryption = forEncryption;
    this.blockBuffer = new byte[engine.getOutputBlockSize()];
    if (pLen > 0 && fallback == null && random == null) {
        throw new IllegalArgumentException("encoder requires random");
    }
}
Also used : AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom)

Example 2 with AsymmetricKeyParameter

use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.

the class OpenSSHPublicKeyUtil method parsePublicKey.

/**
 * Parse a public key from an SSHBuffer instance.
 *
 * @param buffer containing the SSH public key.
 * @return A CipherParameters instance.
 */
public static AsymmetricKeyParameter parsePublicKey(SSHBuffer buffer) {
    AsymmetricKeyParameter result = null;
    String magic = buffer.readString();
    if (RSA.equals(magic)) {
        BigInteger e = buffer.readBigNumPositive();
        BigInteger n = buffer.readBigNumPositive();
        result = new RSAKeyParameters(false, n, e);
    } else if (DSS.equals(magic)) {
        BigInteger p = buffer.readBigNumPositive();
        BigInteger q = buffer.readBigNumPositive();
        BigInteger g = buffer.readBigNumPositive();
        BigInteger pubKey = buffer.readBigNumPositive();
        result = new DSAPublicKeyParameters(pubKey, new DSAParameters(p, q, g));
    } else if (magic.startsWith(ECDSA)) {
        String curveName = buffer.readString();
        ASN1ObjectIdentifier oid = SSHNamedCurves.getByName(curveName);
        X9ECParameters x9ECParameters = SSHNamedCurves.getParameters(oid);
        if (x9ECParameters == null) {
            throw new IllegalStateException("unable to find curve for " + magic + " using curve name " + curveName);
        }
        ECCurve curve = x9ECParameters.getCurve();
        byte[] pointRaw = buffer.readBlock();
        result = new ECPublicKeyParameters(curve.decodePoint(pointRaw), new ECNamedDomainParameters(oid, x9ECParameters));
    } else if (ED_25519.equals(magic)) {
        byte[] pubKeyBytes = buffer.readBlock();
        if (pubKeyBytes.length != Ed25519PublicKeyParameters.KEY_SIZE) {
            throw new IllegalStateException("public key value of wrong length");
        }
        result = new Ed25519PublicKeyParameters(pubKeyBytes, 0);
    }
    if (result == null) {
        throw new IllegalArgumentException("unable to parse key");
    }
    if (buffer.hasRemaining()) {
        throw new IllegalArgumentException("decoded key has trailing data");
    }
    return result;
}
Also used : DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) BigInteger(java.math.BigInteger) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 3 with AsymmetricKeyParameter

use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.

the class RSADigestSigner method init.

/**
 * Initialize the signer for signing or verification.
 *
 * @param forSigning true if for signing, false otherwise
 * @param parameters necessary parameters.
 */
public void init(boolean forSigning, CipherParameters parameters) {
    this.forSigning = forSigning;
    AsymmetricKeyParameter k;
    if (parameters instanceof ParametersWithRandom) {
        k = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters();
    } else {
        k = (AsymmetricKeyParameter) parameters;
    }
    if (forSigning && !k.isPrivate()) {
        throw new IllegalArgumentException("signing requires private key");
    }
    if (!forSigning && k.isPrivate()) {
        throw new IllegalArgumentException("verification requires public key");
    }
    reset();
    rsaEngine.init(forSigning, parameters);
}
Also used : AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom)

Example 4 with AsymmetricKeyParameter

use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.

the class DigestingMessageSigner method init.

public void init(boolean forSigning, CipherParameters param) {
    this.forSigning = forSigning;
    AsymmetricKeyParameter k;
    if (param instanceof ParametersWithRandom) {
        k = (AsymmetricKeyParameter) ((ParametersWithRandom) param).getParameters();
    } else {
        k = (AsymmetricKeyParameter) param;
    }
    if (forSigning && !k.isPrivate()) {
        throw new IllegalArgumentException("Signing Requires Private Key.");
    }
    if (!forSigning && k.isPrivate()) {
        throw new IllegalArgumentException("Verification Requires Public Key.");
    }
    reset();
    messSigner.init(forSigning, param);
}
Also used : AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom)

Example 5 with AsymmetricKeyParameter

use of com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter in project LinLong-Java by zhenwei1108.

the class IESCipher method engineDoFinal.

// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    if (inputLen != 0) {
        buffer.write(input, inputOffset, inputLen);
    }
    byte[] in = buffer.toByteArray();
    buffer.reset();
    // Convert parameters for use in IESEngine
    CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
    if (engineSpec.getNonce() != null) {
        params = new ParametersWithIV(params, engineSpec.getNonce());
    }
    DHParameters dhParams = ((DHKeyParameters) key).getParameters();
    byte[] V;
    if (otherKeyParameter != null) {
        try {
            if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
                engine.init(true, otherKeyParameter, key, params);
            } else {
                engine.init(false, key, otherKeyParameter, params);
            }
            return engine.processBlock(in, 0, in.length);
        } catch (Exception e) {
            throw new BadBlockException("unable to process block", e);
        }
    }
    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
        // Generate the ephemeral key pair
        DHKeyPairGenerator gen = new DHKeyPairGenerator();
        gen.init(new DHKeyGenerationParameters(random, dhParams));
        EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {

            public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
                byte[] Vloc = new byte[(((DHKeyParameters) keyParameter).getParameters().getP().bitLength() + 7) / 8];
                byte[] Vtmp = BigIntegers.asUnsignedByteArray(((DHPublicKeyParameters) keyParameter).getY());
                if (Vtmp.length > Vloc.length) {
                    throw new IllegalArgumentException("Senders's public key longer than expected.");
                } else {
                    System.arraycopy(Vtmp, 0, Vloc, Vloc.length - Vtmp.length, Vtmp.length);
                }
                return Vloc;
            }
        });
        // Encrypt the buffer
        try {
            engine.init(key, params, kGen);
            return engine.processBlock(in, 0, in.length);
        } catch (Exception e) {
            throw new BadBlockException("unable to process block", e);
        }
    } else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
        // Decrypt the buffer
        try {
            engine.init(key, params, new DHIESPublicKeyParser(((DHKeyParameters) key).getParameters()));
            return engine.processBlock(in, 0, in.length);
        } catch (InvalidCipherTextException e) {
            throw new BadBlockException("unable to process block", e);
        }
    } else {
        throw new IllegalStateException("IESCipher not initialised");
    }
}
Also used : EphemeralKeyPairGenerator(com.github.zhenwei.core.crypto.generators.EphemeralKeyPairGenerator) DHKeyParameters(com.github.zhenwei.core.crypto.params.DHKeyParameters) BadBlockException(com.github.zhenwei.provider.jcajce.provider.util.BadBlockException) KeyEncoder(com.github.zhenwei.core.crypto.KeyEncoder) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) DHPublicKeyParameters(com.github.zhenwei.core.crypto.params.DHPublicKeyParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHKeyPairGenerator(com.github.zhenwei.core.crypto.generators.DHKeyPairGenerator) DHKeyGenerationParameters(com.github.zhenwei.core.crypto.params.DHKeyGenerationParameters) BadBlockException(com.github.zhenwei.provider.jcajce.provider.util.BadBlockException) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BadPaddingException(javax.crypto.BadPaddingException) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) DHIESPublicKeyParser(com.github.zhenwei.core.crypto.parsers.DHIESPublicKeyParser) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters)

Aggregations

AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)19 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)7 BigInteger (java.math.BigInteger)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)2 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)2 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)2 KeyEncoder (com.github.zhenwei.core.crypto.KeyEncoder)2 EphemeralKeyPairGenerator (com.github.zhenwei.core.crypto.generators.EphemeralKeyPairGenerator)2 DHPrivateKeyParameters (com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters)2 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)2 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)2 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)2 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)2 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)2 Ed25519PrivateKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters)2 Ed25519PublicKeyParameters (com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters)2 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)2 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)2