All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unbound.provider.ECKeyPairGenerator Maven / Gradle / Ivy

package com.unbound.provider;

import com.dyadicsec.provider.KeyGenSpec;
import com.dyadicsec.provider.KeyParameters;
import com.unbound.common.crypto.EC;

import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;

public final class ECKeyPairGenerator extends KeyPairGeneratorSpi
{
  private Partition partition;
  private EC.Curve curve;
  private KeyParameters keyParameter = null;

  ECKeyPairGenerator(Partition partition)
  {
    this.partition = partition;
  }

// --------------------- interface -----------------

  @Override
  public void initialize(int bitSize, SecureRandom random)
  {
    curve = EC.getCurveBySize(bitSize);
    if (curve == null) throw new InvalidParameterException("Unsupported EC key size " + bitSize);
  }

  @Override
  public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException
  {
    if (params instanceof KeyGenSpec)
    {
      keyParameter = ((KeyGenSpec)params).getKeyParams();
      params = ((KeyGenSpec)params).getOriginal();
    }
    else keyParameter = null;

    ECParameterSpec spec;

    if (params instanceof ECParameterSpec) spec = (ECParameterSpec)params;
    else if (params instanceof ECGenParameterSpec)
    {
      try
      {
        AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
        parameters.init(params);
        spec = parameters.getParameterSpec(ECParameterSpec.class);
      }
      catch (Exception e) { throw new InvalidAlgorithmParameterException("Unsupported EC curve", e); }
    }
    else
    {
      throw new InvalidAlgorithmParameterException("ECParameterSpec or ECGenParameterSpec required for EC");
    }
    curve = EC.getCurve(spec);
    if (curve==null) throw new InvalidAlgorithmParameterException("Unsupported EC curve");
  }

  @Override
  public KeyPair generateKeyPair()
  {
    UBECPrivateKey key;
    try
    {
      key = new UBECPrivateKey(partition);
      key.generate(keyParameter,0,curve);
    }
    catch (Exception e)
    {
      throw new ProviderException(e);
    }
    return new KeyPair(key.pub, key);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy