Java Code Examples for org.bouncycastle.jce.spec.ECNamedCurveParameterSpec#getG()
The following examples show how to use
org.bouncycastle.jce.spec.ECNamedCurveParameterSpec#getG() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SoftKeymasterBlob.java From keystore-decryptor with Apache License 2.0 | 7 votes |
private static ECPrivateKey toJcaPrivateKey(org.bouncycastle.asn1.sec.ECPrivateKey ecPrivateKey) throws GeneralSecurityException { String curveName = null; ASN1ObjectIdentifier curveId = (ASN1ObjectIdentifier) ecPrivateKey.getParameters(); if (curveId.equals(secp224r1_OID)) { curveName = "secp224r1"; } else if (curveId.equals(prime256v1_OID)) { curveName = "prime256v1"; } else if (curveId.equals(secp384r1_OID)) { curveName = "secp384r1"; } else if (curveId.equals(secp521r1_OID)) { curveName = "secp521r1"; } else { throw new IllegalStateException("Unknown curve OID: " + curveId); } ECNamedCurveParameterSpec sp = ECNamedCurveTable.getParameterSpec(curveName); ECParameterSpec params = new ECNamedCurveSpec(sp.getName(), sp.getCurve(), sp.getG(), sp.getN(), sp.getH()); ECPrivateKeySpec pkSpec = new ECPrivateKeySpec(ecPrivateKey.getKey(), params); KeyFactory kf = KeyFactory.getInstance("EC"); ECPrivateKey privateKey = (ECPrivateKey) kf.generatePrivate(pkSpec); return privateKey; }
Example 2
Source File: LocalIdentity.java From ts3j with Apache License 2.0 | 6 votes |
/** * Generates a new identity with a given security level target. * @param securityLevel security level to generate for (may take time) * @return local identity with given security level * @throws GeneralSecurityException */ public static LocalIdentity generateNew(int securityLevel) throws GeneralSecurityException { ECNamedCurveParameterSpec ecp = ECNamedCurveTable.getParameterSpec("prime256v1"); ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); ECKeyGenerationParameters keyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom()); ECKeyPairGenerator generator = new ECKeyPairGenerator(); generator.init(keyGenParams); AsymmetricCipherKeyPair keyPair = generator.generateKeyPair(); ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic(); LocalIdentity localIdentity = load(publicKey.getQ().normalize(), privateKey.getD()); localIdentity.improveSecurity(securityLevel); return localIdentity; }
Example 3
Source File: KeyCodec.java From UAF with Apache License 2.0 | 6 votes |
/** * Decode based on X, Y 32 byte integers * * @param pubKey * @param curveName * - Example secp256r1 * @return * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws NoSuchProviderException */ public static PublicKey getPubKeyFromCurve(byte[] pubKey, String curveName) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { ECNamedCurveParameterSpec spec = ECNamedCurveTable .getParameterSpec(curveName); KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider()); ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, spec.getCurve(), spec.getG(), spec.getN()); ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey); ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params); ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec); return pk; }
Example 4
Source File: Account.java From ontology-java-sdk with GNU Lesser General Public License v3.0 | 5 votes |
public Account(byte[] prikey, SignatureScheme scheme) throws Exception { Security.addProvider(new BouncyCastleProvider()); signatureScheme = scheme; if (scheme == SignatureScheme.SM3WITHSM2) { this.keyType = KeyType.SM2; this.curveParams = new Object[]{Curve.SM2P256V1.toString()}; } else if (scheme == SignatureScheme.SHA256WITHECDSA) { this.keyType = KeyType.ECDSA; this.curveParams = new Object[]{Curve.P256.toString()}; } switch (scheme) { case SHA256WITHECDSA: case SM3WITHSM2: BigInteger d = new BigInteger(1, prikey); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) this.curveParams[0]); ECParameterSpec paramSpec = new ECNamedCurveSpec(spec.getName(), spec.getCurve(), spec.getG(), spec.getN()); ECPrivateKeySpec priSpec = new ECPrivateKeySpec(d, paramSpec); KeyFactory kf = KeyFactory.getInstance("EC", "BC"); this.privateKey = kf.generatePrivate(priSpec); org.bouncycastle.math.ec.ECPoint Q = spec.getG().multiply(d).normalize(); if (Q == null || Q.getAffineXCoord() == null || Q.getAffineYCoord() == null) { throw new SDKException(ErrorCode.OtherError("normalize error")); } ECPublicKeySpec pubSpec = new ECPublicKeySpec( new ECPoint(Q.getAffineXCoord().toBigInteger(), Q.getAffineYCoord().toBigInteger()), paramSpec); this.publicKey = kf.generatePublic(pubSpec); this.addressU160 = Address.addressFromPubKey(serializePublicKey()); break; default: throw new Exception(ErrorCode.UnsupportedKeyType); } }
Example 5
Source File: PushService.java From org.openhab.ui.habot with Eclipse Public License 1.0 | 5 votes |
private boolean verifyKeyPair() { ECNamedCurveParameterSpec curveParameters = ECNamedCurveTable.getParameterSpec(Utils.CURVE); ECPoint g = curveParameters.getG(); ECPoint sG = g.multiply(((ECPrivateKey) privateKey).getS()); return sG.equals(((ECPublicKey) publicKey).getQ()); }
Example 6
Source File: Crypto.java From webauthndemo with Apache License 2.0 | 5 votes |
public static PublicKey getECPublicKey(java.security.spec.ECPoint w, String stdCurveName) throws NoSuchAlgorithmException, InvalidKeySpecException { ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(stdCurveName); java.security.spec.ECParameterSpec params = new ECNamedCurveSpec(parameterSpec.getName(), parameterSpec.getCurve(), parameterSpec.getG(), parameterSpec.getN(), parameterSpec.getH(), parameterSpec.getSeed()); KeySpec keySpec = new java.security.spec.ECPublicKeySpec(w, params); KeyFactory keyFactory = KeyFactory.getInstance("EC"); return keyFactory.generatePublic(keySpec); }
Example 7
Source File: Utils.java From webpush-java with MIT License | 5 votes |
/** * Verify that the private key belongs to the public key. * * @param privateKey * @param publicKey * @return */ public static boolean verifyKeyPair(PrivateKey privateKey, PublicKey publicKey) { ECNamedCurveParameterSpec curveParameters = ECNamedCurveTable.getParameterSpec(CURVE); ECPoint g = curveParameters.getG(); ECPoint sG = g.multiply(((java.security.interfaces.ECPrivateKey) privateKey).getS()); return sG.equals(((ECPublicKey) publicKey).getQ()); }
Example 8
Source File: KeyCodec.java From UAF with Apache License 2.0 | 5 votes |
/** * Decode based on d - 32 byte integer * * @param privKey * @param curveName * - Example secp256r1 * @return * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws NoSuchProviderException */ public static PrivateKey getPrivKeyFromCurve(byte[] privKey, String curveName) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { ECNamedCurveParameterSpec spec = ECNamedCurveTable .getParameterSpec(curveName); KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider()); ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, spec.getCurve(), spec.getG(), spec.getN()); ECPrivateKeySpec priKey = new ECPrivateKeySpec(new BigInteger(privKey), // d params); return kf.generatePrivate(priKey); }
Example 9
Source File: JWKParser.java From keycloak with Apache License 2.0 | 5 votes |
private PublicKey createECPublicKey() { String crv = (String) jwk.getOtherClaims().get(ECPublicJWK.CRV); BigInteger x = new BigInteger(1, Base64Url.decode((String) jwk.getOtherClaims().get(ECPublicJWK.X))); BigInteger y = new BigInteger(1, Base64Url.decode((String) jwk.getOtherClaims().get(ECPublicJWK.Y))); String name; switch (crv) { case "P-256" : name = "secp256r1"; break; case "P-384" : name = "secp384r1"; break; case "P-521" : name = "secp521r1"; break; default : throw new RuntimeException("Unsupported curve"); } try { ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(name); ECNamedCurveSpec params = new ECNamedCurveSpec("prime256v1", spec.getCurve(), spec.getG(), spec.getN()); ECPoint point = new ECPoint(x, y); ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params); KeyFactory kf = KeyFactory.getInstance("ECDSA"); return kf.generatePublic(pubKeySpec); } catch (Exception e) { throw new RuntimeException(e); } }
Example 10
Source File: Account.java From ontology-java-sdk with GNU Lesser General Public License v3.0 | 4 votes |
private void parsePublicKey(byte[] data) throws Exception { if (data == null) { throw new Exception(ErrorCode.NullInput); } if (data.length < 2) { throw new Exception(ErrorCode.InvalidData); } if(data.length == 33){ this.keyType = KeyType.ECDSA; } else if(data.length == 35) { this.keyType = KeyType.fromLabel(data[0]); } this.privateKey = null; this.publicKey = null; switch (this.keyType) { case ECDSA: this.keyType = KeyType.ECDSA; this.curveParams = new Object[]{Curve.P256.toString()}; ECNamedCurveParameterSpec spec0 = ECNamedCurveTable.getParameterSpec(Curve.P256.toString()); ECParameterSpec param0 = new ECNamedCurveSpec(spec0.getName(), spec0.getCurve(), spec0.getG(), spec0.getN()); ECPublicKeySpec pubSpec0 = new ECPublicKeySpec( ECPointUtil.decodePoint( param0.getCurve(), Arrays.copyOfRange(data, 0, data.length)), param0); KeyFactory kf0 = KeyFactory.getInstance("EC", "BC"); this.publicKey = kf0.generatePublic(pubSpec0); break; case SM2: // this.keyType = KeyType.fromLabel(data[0]); Curve c = Curve.fromLabel(data[1]); this.curveParams = new Object[]{c.toString()}; ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(c.toString()); ECParameterSpec param = new ECNamedCurveSpec(spec.getName(), spec.getCurve(), spec.getG(), spec.getN()); ECPublicKeySpec pubSpec = new ECPublicKeySpec( ECPointUtil.decodePoint( param.getCurve(), Arrays.copyOfRange(data, 2, data.length)), param); KeyFactory kf = KeyFactory.getInstance("EC", "BC"); this.publicKey = kf.generatePublic(pubSpec); break; default: throw new Exception(ErrorCode.UnknownKeyType); } }
Example 11
Source File: Ts3Crypt.java From ts3j with Apache License 2.0 | 4 votes |
private static ECDomainParameters getDomainParameters() { ECNamedCurveParameterSpec ecp = ECNamedCurveTable.getParameterSpec("prime256v1"); return new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); }
Example 12
Source File: ECGEN.java From warp10-platform with Apache License 2.0 | 2 votes |
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object top = stack.pop(); if (!(top instanceof String)) { throw new WarpScriptException(getName() + " expects a curve name."); } String name = (String) top; ECKeyPairGenerator gen = new ECKeyPairGenerator(); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(name); if (null == spec) { throw new WarpScriptException(getName() + " only supports the following curves: " + getCurves() + "."); } ECCurve curve = spec.getCurve(); ECDomainParameters domainParams = new ECDomainParameters(curve, spec.getG(),spec.getN(), spec.getH(), spec.getSeed()); ECKeyGenerationParameters params = new ECKeyGenerationParameters(domainParams, CryptoHelper.getSecureRandom()); gen.init(params); final AsymmetricCipherKeyPair keypair = gen.generateKeyPair(); ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keypair.getPrivate(); ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keypair.getPublic(); Map<String,String> keyparams = new HashMap<String,String>(); keyparams.put(Constants.KEY_CURVE, name); keyparams.put(Constants.KEY_D, privateKey.getD().toString()); stack.push(keyparams); keyparams = new HashMap<String,String>(); keyparams.put(Constants.KEY_CURVE, name); keyparams.put(Constants.KEY_Q, Hex.encodeHexString(publicKey.getQ().getEncoded())); stack.push(keyparams); return stack; }