package org.bouncycastle.crypto.tls;

import java.security.SecureRandom;

import org.bouncycastle.crypto.prng.DigestRandomGenerator;
import org.bouncycastle.crypto.prng.RandomGenerator;
import org.bouncycastle.util.Times;

abstract class AbstractTlsContext
    implements TlsContext
{
    private static long counter = Times.nanoTime();

    private synchronized static long nextCounterValue()
    {
        return ++counter;
    }

    private RandomGenerator nonceRandom;
    private SecureRandom secureRandom;
    private SecurityParameters securityParameters;

    private ProtocolVersion clientVersion = null;
    private ProtocolVersion serverVersion = null;
    private TlsSession session = null;
    private Object userObject = null;

    AbstractTlsContext(SecureRandom secureRandom, SecurityParameters securityParameters)
    {
        secureRandom.setSeed(nextCounterValue());
        secureRandom.setSeed(Times.nanoTime());

        this.nonceRandom = new DigestRandomGenerator(TlsUtils.createHash(HashAlgorithm.sha256));
        this.nonceRandom.addSeedMaterial(secureRandom.generateSeed(32));

        this.secureRandom = secureRandom;
        this.securityParameters = securityParameters;
    }

    public RandomGenerator getNonceRandomGenerator()
    {
        return nonceRandom;
    }

    public SecureRandom getSecureRandom()
    {
        return secureRandom;
    }

    public SecurityParameters getSecurityParameters()
    {
        return securityParameters;
    }

    public ProtocolVersion getClientVersion()
    {
        return clientVersion;
    }

    void setClientVersion(ProtocolVersion clientVersion)
    {
        this.clientVersion = clientVersion;
    }

    public ProtocolVersion getServerVersion()
    {
        return serverVersion;
    }

    void setServerVersion(ProtocolVersion serverVersion)
    {
        this.serverVersion = serverVersion;
    }

    public TlsSession getResumableSession()
    {
        return session;
    }

    void setResumableSession(TlsSession session)
    {
        this.session = session;
    }

    public Object getUserObject()
    {
        return userObject;
    }

    public void setUserObject(Object userObject)
    {
        this.userObject = userObject;
    }

    public byte[] exportKeyingMaterial(String asciiLabel, byte[] context_value, int length)
    {
        if (context_value != null && !TlsUtils.isValidUint16(context_value.length))
        {
            throw new IllegalArgumentException("'context_value' must have length less than 2^16 (or be null)");
        }

        SecurityParameters sp = getSecurityParameters();
        byte[] cr = sp.getClientRandom(), sr = sp.getServerRandom();

        int seedLength = cr.length + sr.length;
        if (context_value != null)
        {
            seedLength += (2 + context_value.length);
        }

        byte[] seed = new byte[seedLength];
        int seedPos = 0;

        System.arraycopy(cr, 0, seed, seedPos, cr.length);
        seedPos += cr.length;
        System.arraycopy(sr, 0, seed, seedPos, sr.length);
        seedPos += sr.length;
        if (context_value != null)
        {
            TlsUtils.writeUint16(context_value.length, seed, seedPos);
            seedPos += 2;
            System.arraycopy(context_value, 0, seed, seedPos, context_value.length);
            seedPos += context_value.length;
        }

        if (seedPos != seedLength)
        {
            throw new IllegalStateException("error in calculation of seed for export");
        }

        return TlsUtils.PRF(this, sp.getMasterSecret(), asciiLabel, seed, length);
    }
}

@@ -67,16 +67,55 @@
 andom;%0A%0A
+import org.bouncycastle.crypto.Digest;%0A
 import o
@@ -882,48 +882,111 @@
     
-secureRandom.setSeed(nextCounterValue())
+Digest d = TlsUtils.createHash(HashAlgorithm.sha256);%0A        byte%5B%5D seed = new byte%5Bd.getDigestSize()%5D
 ;%0A  
@@ -1008,32 +1008,22 @@
 dom.
-setSeed(Times.nanoTime()
+nextBytes(seed
 );%0A%0A
@@ -1079,48 +1079,65 @@
 tor(
-TlsUtils.createHash(HashAlgorithm.sha256
+d);%0A        nonceRandom.addSeedMaterial(nextCounterValue(
 ));%0A
@@ -1136,37 +1136,32 @@
 lue());%0A        
-this.
 nonceRandom.addS
@@ -1176,37 +1176,67 @@
 ial(
-secureRandom.generateSeed(32)
+Times.nanoTime());%0A        nonceRandom.addSeedMaterial(seed
 );%0A%0A

Rework the nonce-random initialisation and avoid generateSeed