List of usage examples for org.bouncycastle.asn1.nist NISTNamedCurves getByName
public static X9ECParameters getByName(String name)
From source file:com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyV1KeyFile.java
License:Apache License
private PrivateKey createECDSAPrivateKey(KeyType kt, PlainBuffer buffer, String name) throws GeneralSecurityException, Buffer.BufferException { kt.readPubKeyFromBuffer(buffer); // Public key BigInteger s = new BigInteger(1, buffer.readBytes()); X9ECParameters ecParams = NISTNamedCurves.getByName(name); ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec(name, ecParams.getCurve(), ecParams.getG(), ecParams.getN());//from w w w . ja v a 2 s. c o m ECPrivateKeySpec pks = new ECPrivateKeySpec(s, ecCurveSpec); return SecurityUtils.getKeyFactory("ECDSA").generatePrivate(pks); }
From source file:COSE.OneKey.java
public X9ECParameters GetCurve() throws CoseException { if (OneKey.this.get(KeyKeys.KeyType) != KeyKeys.KeyType_EC2) throw new CoseException("Not an EC2 key"); CBORObject cnCurve = OneKey.this.get(KeyKeys.EC2_Curve); if (cnCurve == KeyKeys.EC2_P256) return NISTNamedCurves.getByName("P-256"); if (cnCurve == KeyKeys.EC2_P384) return NISTNamedCurves.getByName("P-384"); if (cnCurve == KeyKeys.EC2_P521) return NISTNamedCurves.getByName("P-521"); throw new CoseException("Unsupported curve " + cnCurve); }
From source file:COSE.OneKey.java
static private OneKey generateECDSAKey(String curveName, CBORObject curve) { X9ECParameters p = NISTNamedCurves.getByName(curveName); ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null); pGen.init(genParam);//from w w w. ja v a2 s . c om AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); ECPublicKeyParameters keyPublic = (ECPublicKeyParameters) p1.getPublic(); ECPrivateKeyParameters keyPrivate = (ECPrivateKeyParameters) p1.getPrivate(); byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded(); byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded(); boolean signY = true; byte[] rgbD = keyPrivate.getD().toByteArray(); OneKey key = new OneKey(); key.add(KeyKeys.KeyType, KeyKeys.KeyType_EC2); key.add(KeyKeys.EC2_Curve, curve); key.add(KeyKeys.EC2_X, CBORObject.FromObject(rgbX)); key.add(KeyKeys.EC2_Y, CBORObject.FromObject(rgbY)); key.add(KeyKeys.EC2_D, CBORObject.FromObject(rgbD)); return key; }
From source file:COSE.Sign1MessageTest.java
@BeforeClass public static void setUpClass() throws CoseException { X9ECParameters p = NISTNamedCurves.getByName("P-256"); ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null); pGen.init(genParam);//from www . j a v a 2s.c o m AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); keyPublic = (ECPublicKeyParameters) p1.getPublic(); keyPrivate = (ECPrivateKeyParameters) p1.getPrivate(); byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded(); byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded(); boolean signY = true; byte[] rgbD = keyPrivate.getD().toByteArray(); CBORObject key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX); key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY); cnKeyPublic = new OneKey(key); key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX); key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY); cnKeyPublicCompressed = new OneKey(key); key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_D.AsCBOR(), rgbD); cnKeyPrivate = new OneKey(key); }
From source file:COSE.SignCommon.java
/** * //from ww w . j a va 2 s.c o m * @deprecated As of COSE 0.9.1, replaced by {@link #OneKey.GetCurve()}. * @param cnKey key to get the curve for * @return BouncyCastle object describing the curve. * @throws CoseException Errors generated by the COSE module */ @Deprecated static X9ECParameters GetCurve(CBORObject cnKey) throws CoseException { if (cnKey.get(KeyKeys.KeyType.AsCBOR()) != KeyKeys.KeyType_EC2) throw new CoseException("Not an EC2 key"); CBORObject cnCurve = cnKey.get(KeyKeys.EC2_Curve.AsCBOR()); if (cnCurve == KeyKeys.EC2_P256) return NISTNamedCurves.getByName("P-256"); if (cnCurve == KeyKeys.EC2_P384) return NISTNamedCurves.getByName("P-384"); if (cnCurve == KeyKeys.EC2_P521) return NISTNamedCurves.getByName("P-521"); throw new CoseException("Unsupported curve " + cnCurve); }
From source file:net.schmizz.sshj.common.ECDSAVariationsAdapter.java
License:Apache License
static PublicKey readPubKeyFromBuffer(Buffer<?> buf, String variation) throws GeneralSecurityException { String algorithm = BASE_ALGORITHM_NAME + variation; if (!SecurityUtils.isBouncyCastleRegistered()) { throw new GeneralSecurityException("BouncyCastle is required to read a key of type " + algorithm); }/* w w w.j a v a 2s. c o m*/ try { // final String algo = buf.readString(); it has been already read final String curveName = buf.readString(); final int keyLen = buf.readUInt32AsInt(); final byte x04 = buf.readByte(); // it must be 0x04, but don't think // we need that check final byte[] x = new byte[(keyLen - 1) / 2]; final byte[] y = new byte[(keyLen - 1) / 2]; buf.readRawBytes(x); buf.readRawBytes(y); if (log.isDebugEnabled()) { log.debug(String.format("Key algo: %s, Key curve: %s, Key Len: %s, 0x04: %s\nx: %s\ny: %s", algorithm, curveName, keyLen, x04, Arrays.toString(x), Arrays.toString(y))); } if (!SUPPORTED_CURVES.values().contains(curveName)) { throw new GeneralSecurityException(String.format("Unknown curve %s", curveName)); } BigInteger bigX = new BigInteger(1, x); BigInteger bigY = new BigInteger(1, y); String name = NIST_CURVES_NAMES.get(variation); X9ECParameters ecParams = NISTNamedCurves.getByName(name); ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec(name, ecParams.getCurve(), ecParams.getG(), ecParams.getN()); ECPoint p = new ECPoint(bigX, bigY); ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(p, ecCurveSpec); KeyFactory keyFactory = KeyFactory.getInstance("ECDSA"); return keyFactory.generatePublic(publicKeySpec); } catch (Exception ex) { throw new GeneralSecurityException(ex); } }
From source file:org.hyperledger.fabric.sdk.security.CryptoPrimitives.java
License:Open Source License
/** * ecdsaSignToBytes - sign to bytes/*from w w w .j a v a2 s . c o m*/ * * @param privateKey private key. * @param data data to sign * @return * @throws CryptoException */ public byte[] ecdsaSignToBytes(PrivateKey privateKey, byte[] data) throws CryptoException { try { byte[] encoded = data; encoded = hash(data); // char[] hexenncoded = Hex.encodeHex(encoded); // encoded = new String(hexenncoded).getBytes(); X9ECParameters params = NISTNamedCurves.getByName(this.curveName); BigInteger curve_N = params.getN(); ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), curve_N, params.getH()); ECDSASigner signer = new ECDSASigner(); ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(((ECPrivateKey) privateKey).getS(), ecParams); signer.init(true, privKey); BigInteger[] sigs = signer.generateSignature(encoded); sigs = preventMalleability(sigs, curve_N); ByteArrayOutputStream s = new ByteArrayOutputStream(); DERSequenceGenerator seq = new DERSequenceGenerator(s); seq.addObject(new ASN1Integer(sigs[0])); seq.addObject(new ASN1Integer(sigs[1])); seq.close(); byte[] ret = s.toByteArray(); return ret; } catch (Exception e) { throw new CryptoException("Could not sign the message using private key", e); } }