@Override
  protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
      throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (params != null) {
      throw new InvalidAlgorithmParameterException(
          "unknown param type: " + params.getClass().getName());
    }

    engineInitInternal(opmode, key);
  }
Пример #2
0
  protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
      throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;

    if (params == null || params instanceof OAEPParameterSpec) {
      if (key instanceof RSAPublicKey) {
        if (privateKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
          throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
        }

        param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
      } else if (key instanceof RSAPrivateKey) {
        if (publicKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
          throw new InvalidKeyException("mode 2 requires RSAPublicKey");
        }

        param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) key);
      } else {
        throw new InvalidKeyException("unknown key type passed to RSA");
      }

      if (params != null) {
        OAEPParameterSpec spec = (OAEPParameterSpec) params;

        paramSpec = params;

        if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1")
            && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
          throw new InvalidAlgorithmParameterException(
              "unknown mask generation function specified");
        }

        if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
          throw new InvalidAlgorithmParameterException("unkown MGF parameters");
        }

        Digest digest = DigestFactory.getDigest(spec.getDigestAlgorithm());

        if (digest == null) {
          throw new InvalidAlgorithmParameterException(
              "no match on digest algorithm: " + spec.getDigestAlgorithm());
        }

        MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
        Digest mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());

        if (mgfDigest == null) {
          throw new InvalidAlgorithmParameterException(
              "no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
        }

        cipher =
            new OAEPEncoding(
                new RSABlindedEngine(),
                digest,
                mgfDigest,
                ((PSource.PSpecified) spec.getPSource()).getValue());
      }
    } else {
      throw new InvalidAlgorithmParameterException(
          "unknown parameter type: " + params.getClass().getName());
    }

    if (!(cipher instanceof RSABlindedEngine)) {
      if (random != null) {
        param = new ParametersWithRandom(param, random);
      } else {
        param = new ParametersWithRandom(param, new SecureRandom());
      }
    }

    bOut.reset();

    switch (opmode) {
      case Cipher.ENCRYPT_MODE:
      case Cipher.WRAP_MODE:
        cipher.init(true, param);
        break;
      case Cipher.DECRYPT_MODE:
      case Cipher.UNWRAP_MODE:
        cipher.init(false, param);
        break;
      default:
        throw new InvalidParameterException("unknown opmode " + opmode + " passed to RSA");
    }
  }
Пример #3
0
  public void initialize(AlgorithmParameterSpec params, SecureRandom random)
      throws InvalidAlgorithmParameterException {
    if (params instanceof ECParameterSpec) {
      ECParameterSpec p = (ECParameterSpec) params;
      this.ecParams = params;

      param =
          new ECKeyGenerationParameters(
              new ECDomainParameters(p.getCurve(), p.getG(), p.getN()), random);

      engine.init(param);
      initialised = true;
    } else if (params instanceof java.security.spec.ECParameterSpec) {
      java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) params;
      this.ecParams = params;

      ECCurve curve = EC5Util.convertCurve(p.getCurve());
      ECPoint g = EC5Util.convertPoint(curve, p.getGenerator(), false);

      param =
          new ECKeyGenerationParameters(
              new ECDomainParameters(curve, g, p.getOrder(), BigInteger.valueOf(p.getCofactor())),
              random);

      engine.init(param);
      initialised = true;
    } else if (params instanceof ECGenParameterSpec
        || params instanceof ECNamedCurveGenParameterSpec) {
      String curveName;

      if (params instanceof ECGenParameterSpec) {
        curveName = ((ECGenParameterSpec) params).getName();
      } else {
        curveName = ((ECNamedCurveGenParameterSpec) params).getName();
      }

      ECDomainParameters ecP = ECGOST3410NamedCurves.getByName(curveName);
      if (ecP == null) {
        throw new InvalidAlgorithmParameterException("unknown curve name: " + curveName);
      }

      this.ecParams =
          new ECNamedCurveSpec(
              curveName, ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());

      java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;

      ECCurve curve = EC5Util.convertCurve(p.getCurve());
      ECPoint g = EC5Util.convertPoint(curve, p.getGenerator(), false);

      param =
          new ECKeyGenerationParameters(
              new ECDomainParameters(curve, g, p.getOrder(), BigInteger.valueOf(p.getCofactor())),
              random);

      engine.init(param);
      initialised = true;
    } else if (params == null && BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa() != null) {
      ECParameterSpec p = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
      this.ecParams = params;

      param =
          new ECKeyGenerationParameters(
              new ECDomainParameters(p.getCurve(), p.getG(), p.getN()), random);

      engine.init(param);
      initialised = true;
    } else if (params == null && BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa() == null) {
      throw new InvalidAlgorithmParameterException("null parameter passed but no implicitCA set");
    } else {
      throw new InvalidAlgorithmParameterException(
          "parameter object not a ECParameterSpec: " + params.getClass().getName());
    }
  }