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 static void main(String[] args) { SortedSet names = new TreeSet(enumToList(ECNamedCurveTable.getNames())); names.addAll(enumToList(CustomNamedCurves.getNames())); Iterator it = names.iterator(); while (it.hasNext()) { String name = (String)it.next(); X9ECParameters x9 = CustomNamedCurves.getByName(name); if (x9 == null) { x9 = ECNamedCurveTable.getByName(name); } if (x9 != null && ECAlgorithms.isF2mCurve(x9.getCurve())) { System.out.print(name + ":"); implPrintNonZeroTraceBits(x9); } } }
private static void discoverEndomorphisms(String curveName) { X9ECParameters x9 = ECNamedCurveTable.getByName(curveName); if (x9 == null) { System.err.println("Unknown curve: " + curveName); return; } ECCurve c = x9.getCurve(); if (ECAlgorithms.isFpCurve(c)) { BigInteger characteristic = c.getField().getCharacteristic(); if (c.getA().isZero() && characteristic.mod(ECConstants.THREE).equals(ECConstants.ONE)) { System.out.println("Curve '" + curveName + "' has a 'GLV Type B' endomorphism with these parameters:"); printGLVTypeBParameters(x9); } } }
public static void main(String[] args) { SortedSet names = new TreeSet(enumToList(ECNamedCurveTable.getNames())); names.addAll(enumToList(CustomNamedCurves.getNames())); Iterator it = names.iterator(); while (it.hasNext()) { String name = (String)it.next(); X9ECParameters x9 = CustomNamedCurves.getByName(name); if (x9 == null) { x9 = ECNamedCurveTable.getByName(name); } if (x9 != null && ECAlgorithms.isF2mCurve(x9.getCurve())) { System.out.print(name + ":"); implPrintRootZ(x9); } } }
@Override protected void engineInit(byte[] bytes, String format) throws IOException { if (isASN1FormatString(format)) { X962Parameters params = X962Parameters.getInstance(bytes); ECCurve curve = EC5Util.getCurve(BouncyCastleProvider.CONFIGURATION, params); if (params.isNamedCurve()) { curveName = ECNamedCurveTable.getName(ASN1ObjectIdentifier.getInstance(params.getParameters())); } ecParameterSpec = EC5Util.convertToSpec(params, curve); } else { throw new IOException("Unknown encoded parameters format in AlgorithmParameters object: " + format); } }
public static ASN1ObjectIdentifier getNamedCurveOid( ECParameterSpec ecParameterSpec) { for (Enumeration names = ECNamedCurveTable.getNames(); names.hasMoreElements();) { String name = (String)names.nextElement(); X9ECParameters params = ECNamedCurveTable.getByName(name); if (params.getN().equals(ecParameterSpec.getN()) && params.getH().equals(ecParameterSpec.getH()) && params.getCurve().equals(ecParameterSpec.getCurve()) && params.getG().equals(ecParameterSpec.getG())) { return org.bouncycastle.asn1.x9.ECNamedCurveTable.getOID(name); } } return null; }
private static void discoverEndomorphism(String curveName) { X9ECParameters x9 = ECNamedCurveTable.getByName(curveName); if (x9 == null) { System.err.println("Unknown curve: " + curveName); return; } ECCurve c = x9.getCurve(); if (ECAlgorithms.isFpCurve(c)) { BigInteger characteristic = c.getField().getCharacteristic(); if (c.getA().isZero() && characteristic.mod(ECConstants.THREE).equals(ECConstants.ONE)) { System.out.println("Curve '" + curveName + "' has a 'GLV Type B' endomorphism with these parameters: "); printGLVTypeBParameters(x9); } } }
private static RFC6637 create( String curveName, Supplier<Digest> digestFactory, Supplier<Wrapper> wrapperFactory, int publicKeyAlgID, int symAlgID, int symAlgIDLength, int kdfHashID) { try { ASN1ObjectIdentifier oid = ECNamedCurveTable.getOID(curveName); RFC6637KDF kdf = new RFC6637KDF( digestFactory, oid, (byte) publicKeyAlgID, (byte) symAlgID, (byte) kdfHashID); return new RFC6637(wrapperFactory, curveName, symAlgIDLength, kdf); } catch (IOException ex) { throw new IllegalStateException(ex); } }
private static ECPoint decodePoint( BigInteger encodedPoint, ASN1ObjectIdentifier oid) throws IOException { X9ECParameters curve = ECNamedCurveTable.getByOID(oid); if (curve == null) { throw new IOException(oid.getId() + " does not match any known curve."); } if (!(curve.getCurve() instanceof ECCurve.Fp)) { throw new IOException("Only FPCurves are supported."); } return curve.getCurve().decodePoint(encodedPoint.toByteArray()); }
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 = 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()); }
static X9ECParameters getX9Parameters(ASN1ObjectIdentifier curveOID) { X9ECParameters x9 = CustomNamedCurves.getByOID(curveOID); if (x9 == null) { x9 = ECNamedCurveTable.getByOID(curveOID); } return x9; }
/** * Security Level determines the elliptic curve used in key generation * * @param securityLevel currently 256 or 384 * @throws InvalidArgumentException */ void setSecurityLevel(final int securityLevel) throws InvalidArgumentException { logger.trace(format("setSecurityLevel to %d", securityLevel)); if (securityCurveMapping.isEmpty()) { throw new InvalidArgumentException("Security curve mapping has no entries."); } if (!securityCurveMapping.containsKey(securityLevel)) { StringBuilder sb = new StringBuilder(); String sp = ""; for (int x : securityCurveMapping.keySet()) { sb.append(sp).append(x); sp = ", "; } throw new InvalidArgumentException(format("Illegal security level: %d. Valid values are: %s", securityLevel, sb.toString())); } String lcurveName = securityCurveMapping.get(securityLevel); logger.debug(format("Mapped curve strength %d to %s", securityLevel, lcurveName)); X9ECParameters params = ECNamedCurveTable.getByName(lcurveName); //Check if can match curve name to requested strength. if (params == null) { InvalidArgumentException invalidArgumentException = new InvalidArgumentException( format("Curve %s defined for security strength %d was not found.", curveName, securityLevel)); logger.error(invalidArgumentException); throw invalidArgumentException; } curveName = lcurveName; this.securityLevel = securityLevel; }
/** * Sign data with the specified elliptic curve private key. * * @param privateKey elliptic curve private key. * @param data data to sign * @return the signed data. * @throws CryptoException */ private byte[] ecdsaSignToBytes(ECPrivateKey privateKey, byte[] data) throws CryptoException { try { X9ECParameters params = ECNamedCurveTable.getByName(curveName); BigInteger curveN = params.getN(); Signature sig = SECURITY_PROVIDER == null ? Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM) : Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM, SECURITY_PROVIDER); sig.initSign(privateKey); sig.update(data); byte[] signature = sig.sign(); BigInteger[] sigs = decodeECDSASignature(signature); sigs = preventMalleability(sigs, curveN); ByteArrayOutputStream s = new ByteArrayOutputStream(); DERSequenceGenerator seq = new DERSequenceGenerator(s); seq.addObject(new ASN1Integer(sigs[0])); seq.addObject(new ASN1Integer(sigs[1])); seq.close(); return s.toByteArray(); } catch (Exception e) { throw new CryptoException("Could not sign the message using private key", e); } }
protected ECNamedCurveSpec createNamedCurveSpec(String curveName) throws InvalidAlgorithmParameterException { // NOTE: Don't bother with custom curves here as the curve will be converted to JCE type shortly X9ECParameters p = ECUtils.getDomainParametersFromName(curveName); if (p == null) { try { // Check whether it's actually an OID string (SunJSSE ServerHandshaker setupEphemeralECDHKeys bug) p = ECNamedCurveTable.getByOID(new ASN1ObjectIdentifier(curveName)); if (p == null) { throw new InvalidAlgorithmParameterException("unknown curve OID: " + curveName); } } catch (IllegalArgumentException ex) { throw new InvalidAlgorithmParameterException("unknown curve name: " + curveName); } } // Work-around for JDK bug -- it won't look up named curves properly if seed is present byte[] seed = null; //p.getSeed(); return new ECNamedCurveSpec(curveName, p.getCurve(), p.getG(), p.getN(), p.getH(), seed); }
ECPoint decodePoint(byte[] data) { ECCurve curve = ECNamedCurveTable.getByName(curveName).getCurve(); int compactExportSize = (curve.getFieldSize() + 7) / 8; return data.length == compactExportSize ? ECPointsCompact.decodeFPPoint(curve, data) // Compact keys support, non RFC6636 compliant. : curve.decodePoint(data); }
/** * Parse a secret key from one of the GPG S expression keys. * * @return a secret key object. */ public static PGPSecretKey parseSecretKeyFromSExpr(InputStream inputStream, PBEProtectionRemoverFactory keyProtectionRemoverFactory, KeyFingerPrintCalculator fingerPrintCalculator) throws IOException, PGPException { SXprUtils.skipOpenParenthesis(inputStream); String type; type = SXprUtils.readString(inputStream, inputStream.read()); if (type.equals("protected-private-key")) { SXprUtils.skipOpenParenthesis(inputStream); String curveName; String keyType = SXprUtils.readString(inputStream, inputStream.read()); if (keyType.equals("ecc")) { SXprUtils.skipOpenParenthesis(inputStream); String curveID = SXprUtils.readString(inputStream, inputStream.read()); curveName = SXprUtils.readString(inputStream, inputStream.read()); if (curveName.startsWith("NIST ")) { curveName = curveName.substring("NIST ".length()); } SXprUtils.skipCloseParenthesis(inputStream); } else { throw new PGPException("no curve details found"); } byte[] qVal; SXprUtils.skipOpenParenthesis(inputStream); type = SXprUtils.readString(inputStream, inputStream.read()); if (type.equals("q")) { qVal = SXprUtils.readBytes(inputStream, inputStream.read()); } else { throw new PGPException("no q value found"); } PublicKeyPacket pubPacket = new PublicKeyPacket(PublicKeyAlgorithmTags.ECDSA, new Date(), new ECDSAPublicBCPGKey(ECNamedCurveTable.getOID(curveName), new BigInteger(1, qVal))); SXprUtils.skipCloseParenthesis(inputStream); byte[] dValue = getDValue(inputStream, keyProtectionRemoverFactory, curveName); // TODO: check SHA-1 hash. return new PGPSecretKey(new SecretKeyPacket(pubPacket, SymmetricKeyAlgorithmTags.NULL, null, null, new ECSecretBCPGKey(new BigInteger(1, dValue)).getEncoded()), new PGPPublicKey(pubPacket, fingerPrintCalculator)); } throw new PGPException("unknown key type found"); }
public MessageReply handle(final KeyPairGenerateMessage message) { // TODO: sort out the reply messages try { switch (((Type)message.getType())) { case GENERATE: final NamedKeyGenParams ecKeyGenParams = (NamedKeyGenParams)NamedKeyGenParams.getInstance(message.getPayload()); final List<String> involvedPeers = ecKeyGenParams.getNodesToUse(); X9ECParameters params = CustomNamedCurves.getByName(ecKeyGenParams.getDomainParameters()); if (params == null) { params = ECNamedCurveTable.getByName(ecKeyGenParams.getDomainParameters()); } paramsMap.put(ecKeyGenParams.getKeyID(), new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed())); sharedHMap.init(ecKeyGenParams.getKeyID(), involvedPeers.size()); BigInteger h = generateH(params.getN(), new SecureRandom()); // TODO: provide randomness? ECPoint[] messages = new ECPoint[involvedPeers.size()]; for (int i = 0; i != messages.length; i++) { messages[i] = params.getG().multiply(h); } nodeContext.execute(new SendHTask(message.getAlgorithm(), ecKeyGenParams.getKeyID(), involvedPeers, messages)); final List<String> peerList = ecKeyGenParams.getNodesToUse(); ECNewDKGGenerator generator = (ECNewDKGGenerator)nodeContext.getKeyPairGenerator(ecKeyGenParams.getAlgorithm()); ECCommittedSecretShareMessage[] comMessages = generator.generateThresholdKey( ecKeyGenParams.getKeyID(), paramsMap.get(ecKeyGenParams.getKeyID()), peerList.size(), ecKeyGenParams.getThreshold(), sharedHMap.getShare(ecKeyGenParams.getKeyID()).getValue().normalize()); nodeContext.execute(new SendShareTask(generator, message.getAlgorithm(), ecKeyGenParams.getKeyID(), peerList, comMessages)); return new MessageReply(MessageReply.Type.OKAY); case STORE_H: StoreMessage storeMessage = StoreMessage.getInstance(message.getPayload()); ShareMessage shareMessage = ShareMessage.getInstance(storeMessage.getSecretShareMessage()); nodeContext.execute(new StoreHTask(storeMessage.getID(), shareMessage)); return new MessageReply(MessageReply.Type.OKAY); case STORE: StoreMessage sssMessage = StoreMessage.getInstance(message.getPayload()); // we may not have been asked to generate our share yet, if this is the case we need to queue up our share requests // till we can validate them. generator = (ECNewDKGGenerator)nodeContext.getKeyPairGenerator(message.getAlgorithm()); nodeContext.execute(new StoreShareTask(generator, sssMessage.getID(), sssMessage.getSecretShareMessage())); return new MessageReply(MessageReply.Type.OKAY); default: return new MessageReply(MessageReply.Type.ERROR, new DERUTF8String("Unknown command in NodeKeyGenerationService.")); } } catch (Exception e) { nodeContext.getEventNotifier().notify(EventNotifier.Level.ERROR, "NodeKeyGenerationService failure: " + e.getMessage(), e); return new MessageReply(MessageReply.Type.ERROR, new DERUTF8String("NodeKeyGenerationService failure: " + e.getMessage())); } }