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

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

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.provider;

import com.unbound.client.ECPrivateKeyObject;
import com.unbound.client.Partition;
import com.unbound.common.crypto.EC;
import com.unbound.common.crypto.SystemProvider;

import java.security.*;
import java.security.spec.*;

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

  UBECKeyPairGenerator(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()
  {
    try
    {
      ECPrivateKeyObject object = partition.generateEcKey(null, curve, keyParameter);
      ECPoint point = object.getPoint();
      PublicKey publicKey = SystemProvider.KeyFactory.getInstance("EC").generatePublic(new ECPublicKeySpec(point, curve.spec));
      return new KeyPair(publicKey, new UBECPrivateKey(object));
    }
    catch (Exception e) { throw new ProviderException(e); }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy