Deriving the Bulk Encryption and MAC Keys (Diffie-Hellman)

The bulk encryption and MAC keys are mostly derived from the master key but often include a number of other items, depending on the protocol and cipher suite used.

The process is the same for both client and server:

  1. The protocol engine calls CryptSetKeyParam on the master key one or more times, such that the CSP has all of the information necessary to build the keys.
  2. A hash object is created from the master key via CryptCreateHash. This step is necessary because it is not possible to derive CryptoAPI keys directly from other keys.
  3. The two bulk encryption keys and the two MAC keys are created from the "master hash" object, via four calls to CryptDeriveKey.

Note  When performing SSL "reconnects," the protocol engine may perform the above procedure several times, using the same master key. This enables the client and server to have multiple (often simultaneous) connections, each using different bulk encryption and MAC keys, but without doing any Diffie-Hellman operations.

As with all CSPs, it is important that good thread-safe practices are used. Thread counts of several dozen are not unusual.

The protocol engine's source code is typically;

BOOL fClient = <are we a client?>;
CRYPT_DATA_BLOB Data;
HCRYPTHASH hMasterHash;

// Finish creating the master_secret.
switch(<protocol being used>)
{
    case <SSL 3.0>:
    case <TLS 1.0>:

        // Specify client_random.
        Data.pbData = pClientRandom;
        Data.cbData = cbClientRandom;
        CryptSetKeyParam(hMasterKey, KP_CLIENT_RANDOM, 
                         (PBYTE)&Data, 0);

        // Specify server_random.
        Data.pbData = pServerRandom;
        Data.cbData = cbServerRandom;
        CryptSetKeyParam(hMasterKey, KP_SERVER_RANDOM, 
                         (PBYTE)&Data, 0);
}

// Create the master hash object from the master key.
CryptCreateHash(hProv, CALG_SCHANNEL_MASTER_HASH,
                hMasterKey, 0, &hMasterHash);

// Derive read key from the master hash object.
CryptDeriveKey(hProv, 
               CALG_SCHANNEL_ENC_KEY, 
               hMasterHash,
               fClient ? CRYPT_SERVER : 0,
               &hReadKey);

// Derive write key from the master hash object.
CryptDeriveKey(hProv,
               CALG_SCHANNEL_ENC_KEY,
               hMasterHash,
               fClient ? 0 : CRYPT_SERVER,
               &hWriteKey);

// Derive read MAC from the master hash object.
CryptDeriveKey(hProv,
               CALG_SCHANNEL_MAC_KEY,
               hMasterHash,
               fClient ? CRYPT_SERVER : 0,
               &hReadMAC);

// Derive write MAC from the master hash object.
CryptDeriveKey(hProv,
               CALG_SCHANNEL_MAC_KEY,
               hMasterHash,
               fClient ? 0 : CRYPT_SERVER,
               &hWriteMAC);

CryptDestroyHash(hMasterHash);