Example #1
0
  protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    CipherParameters param;

    if (publicKey instanceof ECPublicKey) {
      param = ECUtil.generatePublicKeyParameter(publicKey);
    } else if (publicKey instanceof GOST3410Key) {
      param = GOST3410Util.generatePublicKeyParameter(publicKey);
    } else {
      try {
        byte[] bytes = publicKey.getEncoded();

        publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));

        if (publicKey instanceof ECPublicKey) {
          param = ECUtil.generatePublicKeyParameter(publicKey);
        } else {
          throw new InvalidKeyException("can't recognise key type in DSA based signer");
        }
      } catch (Exception e) {
        throw new InvalidKeyException("can't recognise key type in DSA based signer");
      }
    }

    digest.reset();
    signer.init(false, param);
  }
  protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    CipherParameters param = ECUtil.generatePrivateKeyParameter(privateKey);

    digest.reset();

    if (appRandom != null) {
      signer.init(true, new ParametersWithRandom(param, appRandom));
    } else {
      signer.init(true, param);
    }
  }
Example #3
0
  protected Key engineDoPhase(Key key, boolean lastPhase)
      throws InvalidKeyException, IllegalStateException {
    if (parameters == null) {
      throw new IllegalStateException(kaAlgorithm + " not initialised.");
    }

    if (!lastPhase) {
      throw new IllegalStateException(kaAlgorithm + " can only be between two parties.");
    }

    CipherParameters pubKey;
    // BEGIN android-removed
    // if (agreement instanceof ECMQVBasicAgreement)
    // {
    //     if (!(key instanceof MQVPublicKey))
    //     {
    //         throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
    //             + getSimpleName(MQVPublicKey.class) + " for doPhase");
    //     }
    //
    //     MQVPublicKey mqvPubKey = (MQVPublicKey)key;
    //     ECPublicKeyParameters staticKey = (ECPublicKeyParameters)
    //         ECUtil.generatePublicKeyParameter(mqvPubKey.getStaticKey());
    //     ECPublicKeyParameters ephemKey = (ECPublicKeyParameters)
    //         ECUtil.generatePublicKeyParameter(mqvPubKey.getEphemeralKey());
    //
    //     pubKey = new MQVPublicParameters(staticKey, ephemKey);
    //
    //     // TODO Validate that all the keys are using the same parameters?
    // }
    // else
    // END android-removed
    {
      if (!(key instanceof PublicKey)) {
        throw new InvalidKeyException(
            kaAlgorithm
                + " key agreement requires "
                + getSimpleName(ECPublicKey.class)
                + " for doPhase");
      }

      pubKey = ECUtil.generatePublicKeyParameter((PublicKey) key);

      // TODO Validate that all the keys are using the same parameters?
    }

    result = agreement.calculateAgreement(pubKey);

    return null;
  }
Example #4
0
  private void initFromKey(Key key) throws InvalidKeyException {
    // BEGIN android-removed
    // if (agreement instanceof ECMQVBasicAgreement)
    // {
    //     if (!(key instanceof MQVPrivateKey))
    //     {
    //         throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
    //             + getSimpleName(MQVPrivateKey.class) + " for initialisation");
    //     }
    //
    //     MQVPrivateKey mqvPrivKey = (MQVPrivateKey)key;
    //     ECPrivateKeyParameters staticPrivKey = (ECPrivateKeyParameters)
    //         ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey());
    //     ECPrivateKeyParameters ephemPrivKey = (ECPrivateKeyParameters)
    //         ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey());
    //
    //     ECPublicKeyParameters ephemPubKey = null;
    //     if (mqvPrivKey.getEphemeralPublicKey() != null)
    //     {
    //         ephemPubKey = (ECPublicKeyParameters)
    //             ECUtil.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey());
    //     }
    //
    //     MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey,
    // ephemPubKey);
    //     this.parameters = staticPrivKey.getParameters();
    //
    //     // TODO Validate that all the keys are using the same parameters?
    //
    //     agreement.init(localParams);
    // }
    // else
    // END android-removed
    {
      if (!(key instanceof PrivateKey)) {
        throw new InvalidKeyException(
            kaAlgorithm
                + " key agreement requires "
                + getSimpleName(ECPrivateKey.class)
                + " for initialisation");
      }

      ECPrivateKeyParameters privKey =
          (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
      this.parameters = privKey.getParameters();

      agreement.init(privKey);
    }
  }
Example #5
0
  protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    CipherParameters param;

    if (privateKey instanceof ECKey) {
      param = ECUtil.generatePrivateKeyParameter(privateKey);
    } else {
      param = GOST3410Util.generatePrivateKeyParameter(privateKey);
    }

    digest.reset();

    if (random != null) {
      signer.init(true, new ParametersWithRandom(param, random));
    } else {
      signer.init(true, param);
    }
  }
  protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    CipherParameters param = ECUtil.generatePublicKeyParameter(publicKey);

    digest.reset();
    signer.init(false, param);
  }
Example #7
0
  protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
      throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (!(key instanceof IESKey)) {
      throw new InvalidKeyException("must be passed IES key");
    }

    if (params == null && (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE)) {
      //
      // if nothing is specified we set up for a 128 bit mac, with
      // 128 bit derivation vectors.
      //
      byte[] d = new byte[16];
      byte[] e = new byte[16];

      if (random == null) {
        random = new SecureRandom();
      }

      random.nextBytes(d);
      random.nextBytes(e);

      params = new IESParameterSpec(d, e, 128);
    } else if (!(params instanceof IESParameterSpec)) {
      throw new InvalidAlgorithmParameterException("must be passed IES parameters");
    }

    IESKey ieKey = (IESKey) key;

    CipherParameters pubKey;
    CipherParameters privKey;

    if (ieKey.getPublic() instanceof ECPublicKey) {
      pubKey = ECUtil.generatePublicKeyParameter(ieKey.getPublic());
      privKey = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
    } else {
      pubKey = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
      privKey = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
    }

    this.engineParams = (IESParameterSpec) params;

    IESParameters p =
        new IESParameters(
            engineParams.getDerivationV(),
            engineParams.getEncodingV(),
            engineParams.getMacKeySize());

    this.state = opmode;

    buffer.reset();

    switch (opmode) {
      case Cipher.ENCRYPT_MODE:
      case Cipher.WRAP_MODE:
        cipher.init(true, privKey, pubKey, p);
        break;
      case Cipher.DECRYPT_MODE:
      case Cipher.UNWRAP_MODE:
        cipher.init(false, privKey, pubKey, p);
        break;
      default:
        System.out.println("eeek!");
    }
  }