public JCEECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
public void processServerKeyExchange(InputStream input) throws IOException { SecurityParameters securityParameters = context.getSecurityParameters(); Signer signer = initVerifyer(tlsSigner, securityParameters); InputStream sigIn = new SignerInputStream(input, signer); ECDomainParameters curve_params = TlsECCUtils.readECParameters(namedCurves, clientECPointFormats, sigIn); byte[] point = TlsUtils.readOpaque8(sigIn); byte[] sigByte = TlsUtils.readOpaque16(input); if (!signer.verifySignature(sigByte)) { throw new TlsFatalAlert(AlertDescription.decrypt_error); } this.ecAgreeServerPublicKey = TlsECCUtils.validateECPublicKey(TlsECCUtils.deserializeECPublicKey( clientECPointFormats, curve_params, point)); }
public static ECDomainParameters getParametersForNamedCurve(int namedCurve) { String curveName = getNameOfNamedCurve(namedCurve); if (curveName == null) { return null; } // Lazily created the first time a particular curve is accessed X9ECParameters ecP = SECNamedCurves.getByName(curveName); if (ecP == null) { return null; } // It's a bit inefficient to do this conversion every time return new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed()); }
public void processClientKeyExchange(InputStream input) throws IOException { if (ecAgreeClientPublicKey != null) { // For ecdsa_fixed_ecdh and rsa_fixed_ecdh, the key arrived in the client certificate return; } byte[] point = TlsUtils.readOpaque8(input); ECDomainParameters curve_params = this.ecAgreeServerPrivateKey.getParameters(); this.ecAgreeClientPublicKey = TlsECCUtils.validateECPublicKey(TlsECCUtils.deserializeECPublicKey( serverECPointFormats, curve_params, point)); }
public DSTU4145ECBinary(ECDomainParameters params) { if (!(params.getCurve() instanceof ECCurve.F2m)) { throw new IllegalArgumentException("only binary domain is possible"); } // We always use big-endian in parameter encoding ECCurve.F2m curve = (ECCurve.F2m)params.getCurve(); f = new DSTU4145BinaryField(curve.getM(), curve.getK1(), curve.getK2(), curve.getK3()); a = new ASN1Integer(curve.getA().toBigInteger()); X9IntegerConverter converter = new X9IntegerConverter(); b = new DEROctetString(converter.integerToBytes(curve.getB().toBigInteger(), converter.getByteLength(curve))); n = new ASN1Integer(params.getN()); bp = new DEROctetString(DSTU4145PointEncoder.encodePoint(params.getG())); }
public BCDSTU4145PublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
public BCECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec, ProviderConfiguration configuration) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } this.configuration = configuration; }
public BCECGOST3410PublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = createSpec(ellipticCurve, dp); } else { this.ecSpec = spec; } }
JCEECPrivateKey( String algorithm, ECPrivateKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.d = params.getD(); if (spec == null) { this.ecSpec = new ECParameterSpec( dp.getCurve(), dp.getG(), dp.getN(), dp.getH(), dp.getSeed()); } else { this.ecSpec = spec; } }
static public AsymmetricKeyParameter generatePublicKeyParameter( PublicKey key) throws InvalidKeyException { if (key instanceof ECPublicKey) { ECPublicKey k = (ECPublicKey)key; ECParameterSpec s = k.getParams(); return new ECPublicKeyParameters( k.getQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN())); } throw new InvalidKeyException("can't identify EC public key."); }
static public AsymmetricKeyParameter generatePrivateKeyParameter( PrivateKey key) throws InvalidKeyException { if (key instanceof ECPrivateKey) { ECPrivateKey k = (ECPrivateKey)key; ECParameterSpec s = k.getParams(); return new ECPrivateKeyParameters( k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN())); } throw new InvalidKeyException("can't identify EC private key."); }
public void initialize( int strength, SecureRandom random) { this.strength = strength; this.random = random; this.ecParams = (ECParameterSpec)ecParameters.get(new Integer(strength)); if (ecParams != null) { param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random); engine.init(param); initialised = true; } }
public void initialize( AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(params instanceof ECParameterSpec)) { throw new InvalidAlgorithmParameterException("parameter object not a ECParameterSpec"); } this.ecParams = (ECParameterSpec)params; param = new ECKeyGenerationParameters(new ECDomainParameters(ecParams.getCurve(), ecParams.getG(), ecParams.getN()), random); engine.init(param); initialised = true; }
JCEECPublicKey( String algorithm, ECPublicKeyParameters params, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.q = params.getQ(); if (spec == null) { this.ecSpec = new ECParameterSpec( dp.getCurve(), dp.getG(), dp.getN(), dp.getH(), dp.getSeed()); } else { this.ecSpec = spec; } }
public void processServerKeyExchange(InputStream input) throws IOException { SecurityParameters securityParameters = context.getSecurityParameters(); SignerInputBuffer buf = new SignerInputBuffer(); InputStream teeIn = new TeeInputStream(input, buf); ECDomainParameters curve_params = TlsECCUtils.readECParameters(namedCurves, clientECPointFormats, teeIn); byte[] point = TlsUtils.readOpaque8(teeIn); DigitallySigned signed_params = parseSignature(input); Signer signer = initVerifyer(tlsSigner, signed_params.getAlgorithm(), securityParameters); buf.updateSigner(signer); if (!signer.verifySignature(signed_params.getSignature())) { throw new TlsFatalAlert(AlertDescription.decrypt_error); } this.ecAgreePublicKey = TlsECCUtils.validateECPublicKey(TlsECCUtils.deserializeECPublicKey( clientECPointFormats, curve_params, point)); }
public void processServerKeyExchange(InputStream input) throws IOException { this.psk_identity_hint = TlsUtils.readOpaque16(input); if (this.keyExchange == KeyExchangeAlgorithm.DHE_PSK) { ServerDHParams serverDHParams = ServerDHParams.parse(input); this.dhAgreePublicKey = TlsDHUtils.validateDHPublicKey(serverDHParams.getPublicKey()); this.dhParameters = dhAgreePublicKey.getParameters(); } else if (this.keyExchange == KeyExchangeAlgorithm.ECDHE_PSK) { ECDomainParameters ecParams = TlsECCUtils.readECParameters(namedCurves, clientECPointFormats, input); byte[] point = TlsUtils.readOpaque8(input); this.ecAgreePublicKey = TlsECCUtils.validateECPublicKey(TlsECCUtils.deserializeECPublicKey( clientECPointFormats, ecParams, point)); } }
public static ECDomainParameters getParametersForNamedCurve(int namedCurve) { String curveName = getNameOfNamedCurve(namedCurve); if (curveName == null) { return null; } // Parameters are lazily created the first time a particular curve is accessed X9ECParameters ecP = CustomNamedCurves.getByName(curveName); if (ecP == null) { ecP = ECNamedCurveTable.getByName(curveName); if (ecP == null) { return null; } } // It's a bit inefficient to do this conversion every time return new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed()); }
public BigInteger calculateAgreement( CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey; ECDomainParameters params = pub.getParameters(); BigInteger hd = params.getH().multiply(key.getD()).mod(params.getN()); ECPoint P = pub.getQ().multiply(hd).normalize(); if (P.isInfinity()) { throw new IllegalStateException("Infinity is not a valid agreement value for ECDHC"); } return P.getAffineXCoord().toBigInteger(); }
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: the input cipherText will * need to be preserved in order to complete the transformation to the new public key. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewPublicKeyTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(cipherText.getY()) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Process a single EC point using the basic ElGamal algorithm. * * @param point the EC point to process. * @return the result of the Elgamal process. */ public ECPair encrypt(ECPoint point) { if (key == null) { throw new IllegalStateException("ECElGamalEncryptor not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger k = ECUtil.generateK(ec.getN(), random); ECMultiplier basePointMultiplier = createBasePointMultiplier(); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(point) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECFixedTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = this.k.mod(n); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(cipherText.getX()), key.getQ().multiply(k).add(cipherText.getY()) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/************* * This method will create the ECDSA public and private key which is returned in a Byte array of Byte arrays, index 0 is the private key and * index 1 of the array will return the ECDSA public key * * -Wisdom: This guy didn't check whether the returned Private Key would be more than 32. * @return */ private byte[][] ECDSAgeneratePublicAndPrivateKey(){ int length = 0; byte[][] keys; do{ ECKeyPairGenerator gen = new ECKeyPairGenerator(); SecureRandom secureRandom = new SecureRandom(); X9ECParameters secnamecurves = SECNamedCurves.getByName("secp256k1"); ECDomainParameters ecParams = new ECDomainParameters(secnamecurves.getCurve(), secnamecurves.getG(), secnamecurves.getN(), secnamecurves.getH()); ECKeyGenerationParameters keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom); gen.init(keyGenParam); AsymmetricCipherKeyPair kp = gen.generateKeyPair(); ECPrivateKeyParameters privatekey = (ECPrivateKeyParameters)kp.getPrivate(); ECPoint dd = secnamecurves.getG().multiply(privatekey.getD()); byte[] publickey=new byte[65]; System.arraycopy(dd.getY().toBigInteger().toByteArray(), 0, publickey, 64-dd.getY().toBigInteger().toByteArray().length+1, dd.getY().toBigInteger().toByteArray().length); System.arraycopy(dd.getX().toBigInteger().toByteArray(), 0, publickey, 32-dd.getX().toBigInteger().toByteArray().length+1, dd.getX().toBigInteger().toByteArray().length); publickey[0]=4; length = privatekey.getD().toByteArray().length; keys = new byte[][]{privatekey.getD().toByteArray(),publickey}; }while(length != 32); return keys; }
/** * Transform an existing cipher test pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewRandomnessTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(cipherText.getX()), key.getQ().multiply(k).add(cipherText.getY()) }; ec.getCurve().normalizeAll(gamma_phi); lastK = k; return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * return a parameter spec representing the passed in named * curve. The routine returns null if the curve is not present. * * @param name the name of the curve requested * @return a parameter spec for the curve, null if it is not available. */ public static ECNamedCurveParameterSpec getParameterSpec( String name) { ECDomainParameters ecP = ECGOST3410NamedCurves.getByName(name); if (ecP == null) { try { ecP = ECGOST3410NamedCurves.getByOID(new ASN1ObjectIdentifier(name)); } catch (IllegalArgumentException e) { return null; // not an oid. } } if (ecP == null) { return null; } return new ECNamedCurveParameterSpec( name, ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed()); }
public JCEECPrivateKey( String algorithm, ECPrivateKeyParameters params, JCEECPublicKey pubKey, ECParameterSpec spec) { ECDomainParameters dp = params.getParameters(); this.algorithm = algorithm; this.d = params.getD(); if (spec == null) { EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed()); this.ecSpec = new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getX().toBigInteger(), dp.getG().getY().toBigInteger()), dp.getN(), dp.getH().intValue()); } else { this.ecSpec = spec; } publicKey = getPublicKeyDetails(pubKey); }
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters dp) { return new ECParameterSpec( ellipticCurve, new ECPoint( dp.getG().getX().toBigInteger(), dp.getG().getY().toBigInteger()), dp.getN(), dp.getH().intValue()); }
public static ECPublicKeyParameters deserializeECPublicKey(short[] ecPointFormats, ECDomainParameters curve_params, byte[] encoding) throws IOException { try { ECPoint Y = deserializeECPoint(ecPointFormats, curve_params.getCurve(), encoding); return new ECPublicKeyParameters(Y, curve_params); } catch (RuntimeException e) { throw new TlsFatalAlert(AlertDescription.illegal_parameter); } }
public static AsymmetricCipherKeyPair generateECKeyPair(SecureRandom random, ECDomainParameters ecParams) { ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator(); ECKeyGenerationParameters keyGenerationParameters = new ECKeyGenerationParameters(ecParams, random); keyPairGenerator.init(keyGenerationParameters); return keyPairGenerator.generateKeyPair(); }
public BigInteger calculateAgreement( CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey; ECDomainParameters params = pub.getParameters(); ECPoint P = pub.getQ().multiply(params.getH().multiply(key.getD())); // if (p.isInfinity()) throw new RuntimeException("Invalid public key"); return P.getX().toBigInteger(); }
public static ECDomainParameters getByName( String name) { ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)objIds.get(name); if (oid != null) { return (ECDomainParameters)params.get(oid); } return null; }
/** * All named curves have the following oid format: 1.2.804.2.1.1.1.1.3.1.1.2.X * where X is the curve number 0-9 */ public static ECDomainParameters getByOID(ASN1ObjectIdentifier oid) { String oidStr = oid.getId(); if (oidStr.startsWith(oidBase)) { int index = Integer.parseInt(oidStr.substring(oidStr.length() - 1)); return params[index]; } return null; }