diff --git a/poppler/Decrypt.cc b/poppler/Decrypt.cc index ba44f95..8fcc398 100644 --- a/poppler/Decrypt.cc +++ b/poppler/Decrypt.cc @@ -725,57 +725,83 @@ static inline void invShiftRows(Guchar *state) { state[15] = t; } +static Guchar mul02[256]; +static Guchar mul03[256]; +static Guchar mul09[256]; +static Guchar mul0b[256]; +static Guchar mul0d[256]; +static Guchar mul0e[256]; + // {02} \cdot s -static inline Guchar mul02(Guchar s) { - return (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); +static void mul02_init() { + for(int s=0; s<256; s++) { + mul02[s] = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + } } // {03} \cdot s -static inline Guchar mul03(Guchar s) { - Guchar s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - return s ^ s2; +static void mul03_init() { + for(int s=0; s<256; s++) { + Guchar s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + mul03[s] = s ^ s2; + } } // {09} \cdot s -static inline Guchar mul09(Guchar s) { - Guchar s2, s4, s8; - - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s ^ s8; +static void mul09_init() { + for(int s=0; s<256; s++) { + Guchar s2, s4, s8; + s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + mul09[s] = s ^ s8; + } } // {0b} \cdot s -static inline Guchar mul0b(Guchar s) { - Guchar s2, s4, s8; - - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s ^ s2 ^ s8; +static void mul0b_init() { + for(int s=0; s<256; s++) { + Guchar s2, s4, s8; + s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + mul0b[s] = s ^ s2 ^ s8; + } } // {0d} \cdot s -static inline Guchar mul0d(Guchar s) { - Guchar s2, s4, s8; - - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s ^ s4 ^ s8; +static void mul0d_init() { + for(int s=0; s<256; s++) { + Guchar s2, s4, s8; + s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + mul0d[s] = s ^ s4 ^ s8; + } } // {0e} \cdot s -static inline Guchar mul0e(Guchar s) { - Guchar s2, s4, s8; - - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s2 ^ s4 ^ s8; +static void mul0e_init() { + for(int s=0; s<256; s++) { + Guchar s2, s4, s8; + s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + mul0e[s] = s2 ^ s4 ^ s8; + } } +struct MulTableInit { + MulTableInit() { + mul02_init(); + mul03_init(); + mul09_init(); + mul0b_init(); + mul0d_init(); + mul0e_init(); + } +} mulTableInit; + static inline void mixColumns(Guchar *state) { int c; Guchar s0, s1, s2, s3; @@ -785,10 +811,10 @@ static inline void mixColumns(Guchar *state) { s1 = state[4+c]; s2 = state[8+c]; s3 = state[12+c]; - state[c] = mul02(s0) ^ mul03(s1) ^ s2 ^ s3; - state[4+c] = s0 ^ mul02(s1) ^ mul03(s2) ^ s3; - state[8+c] = s0 ^ s1 ^ mul02(s2) ^ mul03(s3); - state[12+c] = mul03(s0) ^ s1 ^ s2 ^ mul02(s3); + state[c] = mul02[s0] ^ mul03[s1] ^ s2 ^ s3; + state[4+c] = s0 ^ mul02[s1] ^ mul03[s2] ^ s3; + state[8+c] = s0 ^ s1 ^ mul02[s2] ^ mul03[s3]; + state[12+c] = mul03[s0] ^ s1 ^ s2 ^ mul02[s3]; } } @@ -801,10 +827,10 @@ static inline void invMixColumns(Guchar *state) { s1 = state[4+c]; s2 = state[8+c]; s3 = state[12+c]; - state[c] = mul0e(s0) ^ mul0b(s1) ^ mul0d(s2) ^ mul09(s3); - state[4+c] = mul09(s0) ^ mul0e(s1) ^ mul0b(s2) ^ mul0d(s3); - state[8+c] = mul0d(s0) ^ mul09(s1) ^ mul0e(s2) ^ mul0b(s3); - state[12+c] = mul0b(s0) ^ mul0d(s1) ^ mul09(s2) ^ mul0e(s3); + state[c] = mul0e[s0] ^ mul0b[s1] ^ mul0d[s2] ^ mul09[s3]; + state[4+c] = mul09[s0] ^ mul0e[s1] ^ mul0b[s2] ^ mul0d[s3]; + state[8+c] = mul0d[s0] ^ mul09[s1] ^ mul0e[s2] ^ mul0b[s3]; + state[12+c] = mul0b[s0] ^ mul0d[s1] ^ mul09[s2] ^ mul0e[s3]; } } @@ -817,10 +843,10 @@ static inline void invMixColumnsW(Guint *w) { s1 = w[c] >> 16; s2 = w[c] >> 8; s3 = w[c]; - w[c] = ((mul0e(s0) ^ mul0b(s1) ^ mul0d(s2) ^ mul09(s3)) << 24) - | ((mul09(s0) ^ mul0e(s1) ^ mul0b(s2) ^ mul0d(s3)) << 16) - | ((mul0d(s0) ^ mul09(s1) ^ mul0e(s2) ^ mul0b(s3)) << 8) - | (mul0b(s0) ^ mul0d(s1) ^ mul09(s2) ^ mul0e(s3)); + w[c] = ((mul0e[s0] ^ mul0b[s1] ^ mul0d[s2] ^ mul09[s3]) << 24) + | ((mul09[s0] ^ mul0e[s1] ^ mul0b[s2] ^ mul0d[s3]) << 16) + | ((mul0d[s0] ^ mul09[s1] ^ mul0e[s2] ^ mul0b[s3]) << 8) + | (mul0b[s0] ^ mul0d[s1] ^ mul09[s2] ^ mul0e[s3]); } }