# A possible error in the book

So I have been reading this book called "Implementing SSL/TLS using Cryptography and PKI" While going through it, this snippet of code got me baffled

    static void compute_key_schedule (
const unsigned char *key, int key_length, unsigned char w[ ][ 4 ] ) {
int i; int key_words = key_length >> 2;
unsigned char rcon = 0x01; // First, copy the key directly into the key schedule
memcpy( w, key, key_length );
for ( i = key_words; i < 4 * ( key_words + 7 ); i++ ) {
memcpy( w[ i ], w[ i - 1 ], 4 );
if ( !( i % key_words ) ) {
rot_word( w[ i ] );
sub_word( w[ i ] );
if ( !( i % 36 ) ) {
rcon = 0x1b;
}
w[ i ][ 0 ] ^= rcon;
rcon <<= 1;
}
else if ( ( key_words > 6 ) && ( ( i % key_words ) == 4 ) ) {
sub_word( w[ i ] );
}
w[ i ][ 0 ] ^= w[ i - key_words ][ 0 ];
w[ i ][ 1 ] ^= w[ i - key_words ][ 1 ];
w[ i ][ 2 ] ^= w[ i - key_words ][ 2 ];
w[ i ][ 3 ] ^= w[ i - key_words ][ 3 ];
}
}


The text says that in case of 128-bit key, round constant is shifted every 4 iterations (of forming single word of key for key schedule), making up total of 10 shifts, but at 8th shift, the round constant would be 0, and AES mandates that whenever it overflows, you should XOR it with 0x1b. So I get it that's why we did that if (i%36) rcon = 0x1b It makes sense, but if key_length is 192-bit, it would be problematic, since at iteration 36, it will pass the check if ( ! ( i % key_words)) and would proceed to change rcon to 0x1b, though there won't be any overflow at all! (I believe that key would be 0x10 at that time this shift will occur in case of 192-bit).

So it just doesn't makes sense to me and I wanted to know if there is something I am missing out on. At first I thought maybe it's necessary to rotate it every 4 iteration irrespective of key size but reading text and reading rest of code, I don't think that was an intention ever. Thanks a lot!

• @fgrieu I am not very good with terminology so I didn't consult the official standards documentations, I will surely do in future as I learn more and more. So, is if((!(i%36)) && (key_words == 4)) enough to get it running well? Mar 24, 2020 at 12:50

Yes changing if ( !( i % 36 ) ) into if((!(i%36)) && (key_words == 4)) as suggested in comment by the OP is enough to make it (and the rest of the code) pass the example of appendix A.2 and C.2 of FIPS 197 (as well as A.1, A.3, C.1, C.3).