Example #1
0
  private void initFromKey(Key key) throws InvalidKeyException {
    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 {
      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);
    }
  }
  public BigInteger calculateAgreement(CipherParameters pubKey) {
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey;
    ECPoint P = pub.getQ().multiply(key.getD());

    // if (p.isInfinity()) throw new RuntimeException("d*Q == infinity");

    return P.getX().toBigInteger();
  }