The key-mixing function creates a new key for every packet transmitted. It was introduced for two reasons:

As a way to protect against RC4 weak key attacks. The recommended defense (discard the first 256 bytes of key stream) was not possible in existing deployed hardware.

As a way to incorporate the extra bits of the extended IV.

The approach is to combine the session key, the IV and the source MAC address together with a hash function to produce a mixed key. Including the source MAC address provides added protection against forgery and also separates out the key space of the two communicating devices that share the session key.

Performing a hash operation for every packet to be encrypted or decrypted is a major overhead and hard work for the low-power MAC processor typical in earlier Wi-Fi LAN systems. To ease the burden, the mixing has been divided into two phases. During phase 1, the session key and source address are hashed. The result remains constant during the session. In phase 2, performed for every packet, the IV is mixed with the result of the first phase to produce an encryption key. This key is then used to initialize the RC4 encryption hardware.

Note that even the second part of the key-mixing computation can be performed in advance because the IV increases monotonically. Therefore, a station knows which values of IV will be coming up shortly and could precompute a number of keys in advance. This step avoids the need for a real-time computation when a packet is received. The two phases of key mixing are shown in Figure 11.3. As you can see in this figure, 104 bits of mixed key material are needed to form the total RC4 key of 128 bits when the IV is added.

There is nothing very glamorous about the computations for the key mixing but let's quickly look at the algorithm.

The inputs to the computation are abbreviated:

TSC = TKIP Sequence Counter (48 bits)

TSCU = Upper 32 bits of TSC (32 bits)

TSCL = Lower 16 bits of TSC (16 bits)

TA = Transmitter address, the MAC address of the encrypting station (48 bits)

TK = The temporal session key (128 bits)

P1K = Output of the first phase (80 bits)

P2K = Output of the second phase (128 bits); this becomes the RC4Key.

The two phases can be written as the following functions:

P1K Phase1(TA, TSCU, TK)

P2K Phase2(P1K, TSCL, TK)

When the system starts up or a new key exchange occurs, the TSC is reset to 0. The system typically computes the value of P1K right away and stores it for use in generating P2K. P1K needs to be recomputed every time TSCU changes, which is every 65536 packets. There is no reason why the next value of P1K can't be computed in advance so there will be no delay when TCSU actually changes.

Both phase 1 and phase 2 require a byte substitution table or S-box. The substitution table is to the computer what logarithm tables used to be to schoolchildren before calculators.^{[3]} You take a value and look up a corresponding value in a table. The calculations have been done in advance to determine the correct values in the table. A typical substitution table for a byte value is 256 bytes long so there is one entry for each value of the byte.

^{[3]}This is true for the S-box of RC-4, which is generated by computation. However, unlike log-tables, S-boxes for other algorithms may contain values that are not mathematically generated.

However, key mixing uses 16-bit word values. A full substitution table for a 16-bit value would be 2^{16} or 65,336 words long?a total of 128Kbytes! However, this full table is needed only if you want to be able to have a reversible function and create a second table that will "undo" the substitution and get you back where you started. You do not need such a table for hashing functions; in fact, this type of reversal is something you want to prevent. Therefore, the key-mixing algorithm uses a partial substitution table with 512 word entries, which you can think of as two tables, each with 256 words. To make the substitution for a 16-bit word X, we use the upper byte of X as an index into the first table and the lower byte of X as an index into the second table. Then we exclusive OR the two words from the tables to produce a final 16-bit word substitution. This is denoted in the algorithm by the function:

i = S[j]

where i is the result of substituting for j.

The 512 values for the substitution table are listed in the standard. The same substation tables must be used by everyone for this approach to work.

The output of phase 1 is only 80 bits long (not 128), but it uses all 128 bits of the temporal key in the computation. The result is computed in an array of five 16-bit words called P1K_{0}, P1K_{1}, P1K_{2}, P1K_{3}, and P1K_{4}. The following terminology is used in the algorithm:

TSC

_{1}= bits 16?31 of the TSC (the middle 16 bits)TSC

_{2}= bits 32?47 of the TSC (the upper 16 bits)TA

_{n}= n^{th}byte of the encrypting station's MAC address(TA

_{0}= lowest byte, TA_{5}= highest byte)TK

_{n}= n^{th}byte of the temporal key(TK

_{0}= lowest byte, TK_{5}= highest byte)

the expression XY is used to denote combining two bytes into a 16-bit word so that:

XY = 256*X + Y

S[ ] denotes the result from the 16-bit substitution table:

PHASE1_STEP1 P1K_{0}= TSC_{1}P1K_{1}= TSC_{2}P1K_{2}= TA_{1}TA_{0}P1K_{3}= TA_{3}TA_{2}P1K_{4}= TA_{5}TA_{4}PHASE1_STEP2: FOR i = 0 to 3 BEGIN P1K_{0}= P1K_{0}+ S[ P1K_{4}(TK_{1}TK_{0}) ] P1K_{1}= P1K_{1}+ S[ P1K_{0}(TK_{5}TK_{4}) ] P1K_{2}= P1K_{2}+ S[ P1K_{1}(TK_{9}TK_{8}) ] P1K_{3}= P1K_{3}+ S[ P1K_{2}(TK_{13}TK_{12}) ] P1K_{4}= P1K_{4}+ S[ P1K_{3}(TK_{1}TK_{0}) ] + i P1K_{0}= P1K_{0}+ S[ P1K_{4}(TK_{3}TK_{2}) ] P1K_{1}= P1K_{1}+ S[ P1K_{0}(TK_{7}TK_{6}) ] P1K_{2}= P1K_{2}+ S[ P1K_{1}(TK_{11}TK_{10}) ] P1K_{3}= P1K_{3}+ S[ P1K_{2}(TK_{15}TK_{14}) ] P1K_{4}= P1K_{4}+ S[ P1K_{3}(TK_{3}TK_{2}) ] + 2*i + 1 END

Although this is quite a bit of computation?certainly more than in phase 2?the arithmetic comprises entirely shifts, adds, and exclusive OR operations.

On the face of it, phase 2 looks more complicated than phase 1. However, although there are more steps, there is no repeating loop in the computation. The result is computed in an array of six 16-bits words called: PPK_{0}, PPK_{1}, PPK_{2}, PPK_{3}, and PPK_{4}. The following terminology is used in the algorithm:

P1K

_{n}= output words from phase 1TSC

_{0}= bits 0 - 15 of the TSC (the lower 16 bits)TK

_{n}= n^{th}byte of the temporal key(TK

_{0}= lowest byte, TK_{5}= highest byte)

The expression XY is used to denote combining two bytes into a 16-bit word so that:

XY = 256*X + Y

The expression >>>(word) means that the 16-bit word is rotated one place right.

The expression (word) means that the 16-bit word is shifted one place right.

S[ ] denotes the result from the 16-bit substitution table.

RC4Key_{n} means the n^{th} byte of the RC4 key used for encryption.

PHASE2,STEP1: PPK_{0}= P1K_{0}PPK_{1}= P1K_{1}PPK_{2}= P1K_{2}PPK_{3}= P1K_{3}PPK_{4}= P1K_{4}PPK_{5}= P1K_{5}+ TSC_{0}PHASE2,STEP2: PPK_{0}= PPK_{0}+ S[ PPK_{5}(TK_{1}TK_{0}) ] PPK_{1}= PPK_{1}+ S[ PPK_{0}(TK_{3}TK_{2}) ] PPK_{2}= PPK_{2}+ S[ PPK_{1}(TK_{5}TK_{4}) ] PPK_{3}= PPK_{3}+ S[ PPK_{2}(TK_{7}TK_{6}) ] PPK_{4}= PPK_{4}+ S[ PPK_{3}(TK_{9}TK_{8}) ] PPK_{5}= PPK_{5}+ S[ PPK_{4}(TK_{11}TK_{10}) ] PPK_{0}= PPK_{0}+ >>>(PPK_{5}(TK_{13}TK_{12})) PPK_{1}= PPK_{1}+ >>>(PPK_{0}(TK_{15}TK_{14})) PPK_{2}= PPK_{2}+ >>>(PPK_{1}) PPK_{3}= PPK_{3}+ >>>(PPK_{2}) PPK_{4}= PPK_{4}+ >>>(PPK_{3}) PPK_{5}= PPK_{5}+ >>>(PPK_{4}) PHASE2,STEP3: RC4Key_{0}= UpperByte(TSC_{0}) RC4Key_{1}= (UpperByte (TSC_{0}) | 0x20) & 0x7F RC4Key_{2}= LowerByte(TSC_{0}) RC4Key_{3}= LowerByte ((PPK_{5}(TK_{1}TK_{0})) RC4Key_{4}= LowerByte (PPK_{0}) RC4Key_{5}= UpperByte (PPK_{0}) RC4Key_{6}= LowerByte (PPK_{1}) RC4Key_{7}= UpperByte (PPK_{1}) RC4Key_{8}= LowerByte (PPK_{2}) RC4Key_{9}= UpperByte (PPK_{2}) RC4Key_{10}= LowerByte (PPK_{3}) RC4Key_{11}= UpperByte (PPK_{3}) RC4Key_{12}= LowerByte (PPK_{4}) RC4Key_{13}= UpperByte (PPK_{4}) RC4Key_{14}= LowerByte (PPK_{5}) RC4Key_{15}= UpperByte (PPK_{5})

The final output of phase 2 is an array of 16 bytes containing the RC4 key to be used in encryption. This can be loaded into the legacy WEP encryption engine prior to processing the MPDU for transmission. The first 3 bytes of this key are transmitted as the WEP IV field (24 bits) and contain the lower 16 bits of the TKIP IV value and the TSC. The second byte of the WEP IV is a repeat of the first byte, except that bit 5 is forced to 1 and bit 4 is forced to 0. Forcing these bits prevents generation of the major class of weak keys. This byte is ignored on receipt.