package com.nintendo.ecqv;

import com.nintendo.hsminterfaces.RandomInterface;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.math.ec.*;
import java.math.BigInteger;

public class Client {
    private RandomInterface m_Random;
    private ECParameterSpec m_EcParams;
    private boolean         m_isUsePointCompression;

    private BigInteger      m_PrivateKey;
    private ECPoint         m_PublicKey;
    private boolean         m_isPublicKeyValid = false;

    private BigInteger      m_ku;
    private ECPoint         m_Ru;

    private Certificate     m_ECQV_Certificate;

    public Client(RandomInterface randomGenerator,
                  ECParameterSpec ecParams,
                  boolean         isUsePointCompression)
    {
        m_Random                = randomGenerator;
        m_EcParams              = ecParams;
        m_isUsePointCompression = isUsePointCompression;
    }

    public void RequestCertificate()
    {
        KeyPair keyPair = KeyPair.GenerateKeyPair(m_Random, m_EcParams);
        m_ku = keyPair.GetPrivateKey();
        m_Ru = keyPair.GetPublicKey();
    }

    public boolean ReceiveCertificate(ECPoint caPublicKey, Certificate ECQV_Certificate)
    {
        m_ECQV_Certificate = ECQV_Certificate;
        m_PublicKey        = ExtractPublicKey(caPublicKey, m_ECQV_Certificate.GetCertificatePoint() );

        BigInteger e = Hash.PointToIntegerHash(m_ECQV_Certificate.GetCertificatePoint(), m_EcParams.getN() );

        // Compute the private key dU = r + ekU (mod n)
        BigInteger eKu = e.multiply(m_ku).mod(m_EcParams.getN() );
        BigInteger dU  = eKu.add(m_ECQV_Certificate.GetR() ).mod(m_EcParams.getN() );
        m_PrivateKey   = dU.add(BigInteger.ZERO); // Copy of immutable BigInteger

        // Compute Qu = du*G
        ECPoint Qu = m_EcParams.getG().multiply(dU);
        assert(Qu.isValid() );

        // Validate Qu == m_publicKey
        if (Qu.equals(m_PublicKey) )
            m_isPublicKeyValid = true;
        else
            m_isPublicKeyValid = false;

        return m_isPublicKeyValid;
    }

    private ECPoint ExtractPublicKey(ECPoint caPublicKey, ECPoint cert)
    {
        if (m_isUsePointCompression)
        {
            // Normalize the point before getting its integer affine coordinates
            cert = cert.normalize();

            BigInteger x = cert.getXCoord().toBigInteger();
            BigInteger y = ECMath.ComputeY(x, m_EcParams);
            cert = m_EcParams.getCurve().createPoint(x, y);
            assert(cert.isValid() );
        }

        // compute e = Hn(CertU)
        BigInteger e = Hash.PointToIntegerHash(cert, m_EcParams.getN() );

        // Qu = ePu + Qca
        ECPoint ePu = cert.multiply(e);
        assert(ePu.isValid() );

        ECPoint pOutClientPublicKey = ePu.add(caPublicKey);
        assert(ePu.isValid() );

        return pOutClientPublicKey;
    }

    public ECPoint GetRu()
    {
        return m_Ru;
    }

    public ECPoint GetPublicKey()
    {
        return m_PublicKey;
    }

    public BigInteger GetPrivateKey()
    {
        return m_PrivateKey;
    }

    public Certificate GetCertificate()
    {
        return m_ECQV_Certificate;
    }
}
