tailieunhanh - cryptography for developers PHẦN 5

Đây là chức năng MixColumn chúng ta đã thấy trước đó, ngoại trừ nó đã được sửa đổi để làm việc trên tất cả 16 byte của nhà nước. Như đã lưu ý, chức năng này cũng tăng gấp đôi đệm (sao chép để tmp []) và có thể được tối ưu hóa để tránh điều này. Chúng tôi cũng sử dụng một mảng xt [] để giữ các bản sao của xtime () đầu ra. | 158 Chapter 4 Advanced Encryption Standard 064 col 2 tmp 2 065 col 3 tmp 3 066 067 This is the MixColumn function we saw previously except it has now been modified to work on all 16 bytes of the state. As previously noted this function is also doubled buffered copying to tmp and can be optimized to avoid this. We are also using an array xt to hold copies of the xtime output. Since it is used twice caching it saves time. However we do not actually need the array. If we first add all inputs then the xtime results we only need a single byte of extra storage. 069 ShiftRows Shifts the entire block 070 static void ShiftRows unsigned char col 071 072 unsigned char t 073 074 2nd row 075 t col 1 col 1 col 5 col 5 col 9 076 col 9 col 13 col 13 t 077 078 3rd row 079 t col 2 col 2 col 10 col 10 t 080 t col 6 col 6 col 14 col 14 t 081 082 4th row 083 t col 15 col 15 col 11 col 11 col 7 084 col 7 col 3 col 3 t 085 This function implements the ShiftRows function. It uses a single temporary byte t to swap around values in the rows. The second and fourth rows are implemented using essentially a shift register while the third row is a pair of swaps. 087 SubBytes 088 static void SubBytes unsigned char col 089 090 int x 091 for x 0 x 16 x 092 col x sbox col x 093 094 This function implements the SubBytes function. Fairly straightforward not much to optimize here. 096 AddRoundKey 097 static void AddRoundKey unsigned char col 098 unsigned char key int round 099 100 int x 101 for x 0 x 16 x 102 col x A key round 4 x 103 104 Advanced Encryption Standard Chapter 4 159 This functions implements AddRoundKey function. It reads the round key from a single array of bytes which is at most 15 16 240 bytes in size. We shift the round number by four bits to the left to emulate a multiplication by 16. This function can be optimized on platforms with words larger than eight bits by XORing multiple key bytes at a is an optimization we shall see in the 32-bit code. 106 .