예제 #1
0
  protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException {
    if (!(key instanceof ECPrivateKey)) {
      throw new InvalidKeyException("ECKeyAgreement requires ECPrivateKey");
    }

    privKey = ECUtil.generatePrivateKeyParameter((PrivateKey) key);

    agreement.init(privKey);
  }
예제 #2
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;
  }
예제 #3
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);
    }
  }
예제 #4
0
  protected Key engineDoPhase(Key key, boolean lastPhase)
      throws InvalidKeyException, IllegalStateException {
    if (privKey == null) {
      throw new IllegalStateException("EC Diffie-Hellman not initialised.");
    }

    if (!lastPhase) {
      throw new IllegalStateException("EC Diffie-Hellman can only be between two parties.");
    }

    if (!(key instanceof ECPublicKey)) {
      throw new InvalidKeyException("EC Key Agreement doPhase requires ECPublicKey");
    }

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

    result = agreement.calculateAgreement(pubKey);

    return null;
  }