protected byte[] engineGetEncoded( String format) { if (isASN1FormatString(format) || format.equalsIgnoreCase("X.509")) { return engineGetEncoded(); } return null; }
protected Key engineTranslateKey( Key key) throws InvalidKeyException { if (key instanceof DHPublicKey) { return new BCElGamalPublicKey((DHPublicKey)key); } else if (key instanceof DHPrivateKey) { return new BCElGamalPrivateKey((DHPrivateKey)key); } else if (key instanceof ElGamalPublicKey) { return new BCElGamalPublicKey((ElGamalPublicKey)key); } else if (key instanceof ElGamalPrivateKey) { return new BCElGamalPrivateKey((ElGamalPrivateKey)key); } throw new InvalidKeyException("key type unknown"); }
protected PrivateKey engineGeneratePrivate( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof ElGamalPrivateKeySpec) { return new BCElGamalPrivateKey((ElGamalPrivateKeySpec)keySpec); } else if (keySpec instanceof DHPrivateKeySpec) { return new BCElGamalPrivateKey((DHPrivateKeySpec)keySpec); } return super.engineGeneratePrivate(keySpec); }
protected void engineInit( byte[] params, String format) throws IOException { if (isASN1FormatString(format) || format.equalsIgnoreCase("X.509")) { engineInit(params); } else { throw new IOException("Unknown parameter format " + format); } }
throws IllegalBlockSizeException, BadPaddingException, ShortBufferException if (outputOffset + engineGetOutputSize(inputLen) > output.length) byte[] out = getOutput();
protected PublicKey engineGeneratePublic( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof ElGamalPublicKeySpec) { return new BCElGamalPublicKey((ElGamalPublicKeySpec)keySpec); } else if (keySpec instanceof DHPublicKeySpec) { return new BCElGamalPublicKey((DHPublicKeySpec)keySpec); } return super.engineGeneratePublic(keySpec); }
protected void engineInit( int opmode, Key key, SecureRandom random) throws InvalidKeyException { try { engineInit(opmode, key, (AlgorithmParameterSpec)null, random); } catch (InvalidAlgorithmParameterException e) { // this shouldn't happen throw new InvalidKeyException("Eeeek! " + e.toString(), e); } }
protected byte[] engineDoFinal( byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { if (input != null) { bOut.write(input, inputOffset, inputLen); } if (cipher instanceof ElGamalEngine) { if (bOut.size() > cipher.getInputBlockSize() + 1) { throw new ArrayIndexOutOfBoundsException("too much data for ElGamal block"); } } else { if (bOut.size() > cipher.getInputBlockSize()) { throw new ArrayIndexOutOfBoundsException("too much data for ElGamal block"); } } return getOutput(); }
protected AlgorithmParameters engineGenerateParameters() { ElGamalParametersGenerator pGen = new ElGamalParametersGenerator(); if (random != null) { pGen.init(strength, 20, random); } else { pGen.init(strength, 20, CryptoServicesRegistrar.getSecureRandom()); } ElGamalParameters p = pGen.generateParameters(); AlgorithmParameters params; try { params = createParametersInstance("ElGamal"); params.init(new DHParameterSpec(p.getP(), p.getG(), l)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; } }
/** * Return a PKCS8 representation of the key. The sequence returned * represents a full PrivateKeyInfo object. * * @return a PKCS8 representation of the key. */ public byte[] getEncoded() { try { PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(OIWObjectIdentifiers.elGamalAlgorithm, new ElGamalParameter(elSpec.getP(), elSpec.getG())), new ASN1Integer(getX())); return info.getEncoded(ASN1Encoding.DER); } catch (IOException e) { return null; } }
protected AlgorithmParameters engineGetParameters() { if (engineParams == null) { if (paramSpec != null) { try { engineParams = createParametersInstance("OAEP"); engineParams.init(paramSpec); } catch (Exception e) { throw new RuntimeException(e.toString()); } } } return engineParams; }
initFromSpec(OAEPParameterSpec.DEFAULT); initFromSpec(new OAEPParameterSpec("MD5", "MGF1", new MGF1ParameterSpec("MD5"), PSource.PSpecified.DEFAULT)); initFromSpec(OAEPParameterSpec.DEFAULT); initFromSpec(new OAEPParameterSpec("SHA-224", "MGF1", new MGF1ParameterSpec("SHA-224"), PSource.PSpecified.DEFAULT)); initFromSpec(new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT)); initFromSpec(new OAEPParameterSpec("SHA-384", "MGF1", MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT)); initFromSpec(new OAEPParameterSpec("SHA-512", "MGF1", MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT)); initFromSpec(new OAEPParameterSpec("SHA3-224", "MGF1", new MGF1ParameterSpec("SHA3-224"), PSource.PSpecified.DEFAULT)); initFromSpec(new OAEPParameterSpec("SHA3-256", "MGF1", new MGF1ParameterSpec("SHA3-256"), PSource.PSpecified.DEFAULT)); initFromSpec(new OAEPParameterSpec("SHA3-384", "MGF1", new MGF1ParameterSpec("SHA3-384"), PSource.PSpecified.DEFAULT)); initFromSpec(new OAEPParameterSpec("SHA3-512", "MGF1", new MGF1ParameterSpec("SHA3-512"), PSource.PSpecified.DEFAULT));
public void configure(ConfigurableProvider provider) { provider.addAlgorithm("AlgorithmParameterGenerator.ELGAMAL", PREFIX + "AlgorithmParameterGeneratorSpi"); provider.addAlgorithm("AlgorithmParameterGenerator.ElGamal", PREFIX + "AlgorithmParameterGeneratorSpi"); provider.addAlgorithm("AlgorithmParameters.ELGAMAL", PREFIX + "AlgorithmParametersSpi"); provider.addAlgorithm("AlgorithmParameters.ElGamal", PREFIX + "AlgorithmParametersSpi"); provider.addAlgorithm("Cipher.ELGAMAL", PREFIX + "CipherSpi$NoPadding"); provider.addAlgorithm("Cipher.ElGamal", PREFIX + "CipherSpi$NoPadding"); provider.addAlgorithm("Alg.Alias.Cipher.ELGAMAL/ECB/PKCS1PADDING", "ELGAMAL/PKCS1"); provider.addAlgorithm("Alg.Alias.Cipher.ELGAMAL/NONE/PKCS1PADDING", "ELGAMAL/PKCS1"); provider.addAlgorithm("Alg.Alias.Cipher.ELGAMAL/NONE/NOPADDING", "ELGAMAL"); provider.addAlgorithm("Cipher.ELGAMAL/PKCS1", PREFIX + "CipherSpi$PKCS1v1_5Padding"); provider.addAlgorithm("KeyFactory.ELGAMAL", PREFIX + "KeyFactorySpi"); provider.addAlgorithm("KeyFactory.ElGamal", PREFIX + "KeyFactorySpi"); provider.addAlgorithm("KeyPairGenerator.ELGAMAL", PREFIX + "KeyPairGeneratorSpi"); provider.addAlgorithm("KeyPairGenerator.ElGamal", PREFIX + "KeyPairGeneratorSpi"); AsymmetricKeyInfoConverter keyFact = new KeyFactorySpi(); registerOid(provider, OIWObjectIdentifiers.elGamalAlgorithm, "ELGAMAL", keyFact); registerOidAlgorithmParameterGenerator(provider, OIWObjectIdentifiers.elGamalAlgorithm, "ELGAMAL"); } }
public KeyPair generateKeyPair() { if (!initialised) { DHParameterSpec dhParams = BouncyCastleProvider.CONFIGURATION.getDHDefaultParameters(strength); if (dhParams != null) { param = new ElGamalKeyGenerationParameters(random, new ElGamalParameters(dhParams.getP(), dhParams.getG(), dhParams.getL())); } else { ElGamalParametersGenerator pGen = new ElGamalParametersGenerator(); pGen.init(strength, certainty, random); param = new ElGamalKeyGenerationParameters(random, pGen.generateParameters()); } engine.init(param); initialised = true; } AsymmetricCipherKeyPair pair = engine.generateKeyPair(); ElGamalPublicKeyParameters pub = (ElGamalPublicKeyParameters)pair.getPublic(); ElGamalPrivateKeyParameters priv = (ElGamalPrivateKeyParameters)pair.getPrivate(); return new KeyPair(new BCElGamalPublicKey(pub), new BCElGamalPrivateKey(priv)); } }
public boolean equals( Object o) { if (!(o instanceof DHPrivateKey)) { return false; } DHPrivateKey other = (DHPrivateKey)o; return this.getX().equals(other.getX()) && this.getParams().getG().equals(other.getParams().getG()) && this.getParams().getP().equals(other.getParams().getP()) && this.getParams().getL() == other.getParams().getL(); }
public boolean equals( Object o) { if (!(o instanceof DHPublicKey)) { return false; } DHPublicKey other = (DHPublicKey)o; return this.getY().equals(other.getY()) && this.getParams().getG().equals(other.getParams().getG()) && this.getParams().getP().equals(other.getParams().getP()) && this.getParams().getL() == other.getParams().getL(); }
public PrivateKey generatePrivate(PrivateKeyInfo info) throws IOException { ASN1ObjectIdentifier algOid = info.getPrivateKeyAlgorithm().getAlgorithm(); if (algOid.equals(PKCSObjectIdentifiers.dhKeyAgreement)) { return new BCElGamalPrivateKey(info); } else if (algOid.equals(X9ObjectIdentifiers.dhpublicnumber)) { return new BCElGamalPrivateKey(info); } else if (algOid.equals(OIWObjectIdentifiers.elGamalAlgorithm)) { return new BCElGamalPrivateKey(info); } else { throw new IOException("algorithm identifier " + algOid + " in key not recognised"); } }
public PublicKey generatePublic(SubjectPublicKeyInfo info) throws IOException { ASN1ObjectIdentifier algOid = info.getAlgorithm().getAlgorithm(); if (algOid.equals(PKCSObjectIdentifiers.dhKeyAgreement)) { return new BCElGamalPublicKey(info); } else if (algOid.equals(X9ObjectIdentifiers.dhpublicnumber)) { return new BCElGamalPublicKey(info); } else if (algOid.equals(OIWObjectIdentifiers.elGamalAlgorithm)) { return new BCElGamalPublicKey(info); } else { throw new IOException("algorithm identifier " + algOid + " in key not recognised"); } } }