Key-Expansion(input: k[ ]; n; output: K[ ])

1. // n is the number of words in the key buffer k[ ]; (4  n  14)
2. // K[ ]is the expanded key array, consisting of 40 words
3. // T[ ]is a temporary array, consisting of 15 words
4. // B[ ]is a fixed table of four words

5. // Initialize B[ ]
6. B[ ]={0xa4a8d57b ; 0x5b5d193b ; 0xc8a8309b ; 0x73f9a978 }

7. // Initialize T[ ]with key data
8. T[0 ... n-1] = k[0 ... n-1], T[n] =n, T[n + 1...14] = 0

9. // Four iterations, computing 10 words of K[ ]in each
10. for j = 0to 3 do
11. for i = 0to 14 do // Linear transformation
12. T[i] =T[i] (+) ((T[i-7 mod 15] (+) T[i-2 mod 15]) <<< 3) (+) (4i + j)

13. repeat four times // Four stirring rounds
14. for i = 0to 14 do
15. T[i] =(T[i] +S[low 9 bits of T[i-1 mod 15]]) <<< 9
16. end-repeat

17. for i = 0to 9 do // store next 10 words into K[ ]
18. K[10j + i] =T[4i mod 15]
19. end-for

20. // Modify multiplication keys
21. for i = 5; 7; ...35 do
22. j =least two bits of K[i]
23. w = K[i] with both of the least two bits set to 1

24. // Generate a bit-mask M
25. M` = 1iff w` belongs to a sequence of ten consecutive 0s or 1s in w
26. and also 2 <=  l <= 30 and wl-1 = wl = wl+1

27. // Select a pattern from the fixed table and rotate it
28. r =least five bits of K[i-1] // Rotation amount
29. p = B[j] <<< r

30. // Modify K[i] with p under the control of the mask M
31. K[i] =w (+) (p ^ M)
32. end-for 