// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package com.google.crypto.tink.subtle;

import com.google.crypto.tink.util.Bytes;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;

/**
 * HKDF-based ECIES-KEM (key encapsulation mechanism) for ECIES sender.
 *
 * @since 1.0.0
 */
public final class EciesHkdfSenderKem {
  private ECPublicKey recipientPublicKey;

  /** A container for key parts generated by the KEM. */
  public static final class KemKey {
    private final Bytes kemBytes;
    private final Bytes symmetricKey;

    public KemKey(final byte[] kemBytes, final byte[] symmetricKey) {
      this.kemBytes = Bytes.copyFrom(kemBytes);
      this.symmetricKey = Bytes.copyFrom(symmetricKey);
    }

    public byte[] getKemBytes() {
      if (kemBytes == null) {
        return null;
      } else {
        return kemBytes.toByteArray();
      }
    }

    public byte[] getSymmetricKey() {
      if (symmetricKey == null) {
        return null;
      } else {
        return symmetricKey.toByteArray();
      }
    }
  }

  public EciesHkdfSenderKem(final ECPublicKey recipientPublicKey) {
    this.recipientPublicKey = recipientPublicKey;
  }

  public KemKey generateKey(
      String hmacAlgo,
      final byte[] hkdfSalt,
      final byte[] hkdfInfo,
      int keySizeInBytes,
      EllipticCurves.PointFormatType pointFormat)
      throws GeneralSecurityException {
    KeyPair ephemeralKeyPair = EllipticCurves.generateKeyPair(recipientPublicKey.getParams());
    ECPublicKey ephemeralPublicKey = (ECPublicKey) ephemeralKeyPair.getPublic();
    ECPrivateKey ephemeralPrivateKey = (ECPrivateKey) ephemeralKeyPair.getPrivate();
    byte[] sharedSecret = EllipticCurves.computeSharedSecret(
        ephemeralPrivateKey, recipientPublicKey);
    byte[] kemBytes =
        EllipticCurves.pointEncode(
            ephemeralPublicKey.getParams().getCurve(), pointFormat, ephemeralPublicKey.getW());
    byte[] symmetricKey =
        Hkdf.computeEciesHkdfSymmetricKey(
            kemBytes, sharedSecret, hmacAlgo, hkdfSalt, hkdfInfo, keySizeInBytes);
    return new KemKey(kemBytes, symmetricKey);
  }
}

@@ -1067,16 +1067,22 @@
 private 
+final 
 ECPublic
@@ -1339,24 +1339,246 @@
 etricKey) %7B%0A
+      if (kemBytes == null) %7B%0A        throw new NullPointerException(%22KemBytes must be non-null%22);%0A      %7D%0A      if (symmetricKey == null) %7B%0A        throw new NullPointerException(%22symmetricKey must be non-null%22);%0A      %7D%0A
       this.k
@@ -1714,76 +1714,8 @@
 ) %7B%0A
-      if (kemBytes == null) %7B%0A        return null;%0A      %7D else %7B%0A  
     
@@ -1743,32 +1743,24 @@
 yteArray();%0A
-      %7D%0A
     %7D%0A%0A    p
@@ -1796,80 +1796,8 @@
 ) %7B%0A
-      if (symmetricKey == null) %7B%0A        return null;%0A      %7D else %7B%0A  
     
@@ -1833,24 +1833,16 @@
 rray();%0A
-      %7D%0A
     %7D%0A  

Throw nullpointer exceptions if a KemKey is created without kemBytes or symmetricKey.