Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: lib/gf128mul - make gf128mul_lle time invariant

The gf128mul library has different variants with different
memory/performance tradeoffs, where the faster ones use 4k or 64k lookup
tables precomputed at runtime, which are based on one of the
multiplication factors, which is commonly the key for keyed hash
algorithms such as GHASH.

The slowest variant is gf128_mul_lle() [and its bbe/ble counterparts],
which does not use precomputed lookup tables, but it still relies on a
single u16[256] lookup table which is input independent. The use of such
a table may cause the execution time of gf128_mul_lle() to correlate
with the value of the inputs, which is generally something that must be
avoided for cryptographic algorithms. On top of that, the function uses
a sequence of if () statements that conditionally invoke be128_xor()
based on which bits are set in the second argument of the function,
which is usually a pointer to the multiplication factor that represents
the key.

In order to remove the correlation between the execution time of
gf128_mul_lle() and the value of its inputs, let's address the
identified shortcomings:
- add a time invariant version of gf128mul_x8_lle() that replaces the
table lookup with the expression that is used at compile time to
populate the lookup table;
- make the invocations of be128_xor() unconditional, but pass a zero
vector as the third argument if the associated bit in the key is
cleared.

The resulting code is likely to be significantly slower. However, given
that this is the slowest version already, making it even slower in order
to make it more secure is assumed to be justified.

The bbe and ble counterparts could receive the same treatment, but the
former is never used anywhere in the kernel, and the latter is only
used in the driver for a asynchronous crypto h/w accelerator (Chelsio),
where timing variances are unlikely to matter.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Ard Biesheuvel and committed by
Herbert Xu
b67ce439 61c581a4

+39 -19
+39 -19
lib/crypto/gf128mul.c
··· 146 146 x->a = cpu_to_be64((a >> 8) ^ (_tt << 48)); 147 147 } 148 148 149 + /* time invariant version of gf128mul_x8_lle */ 150 + static void gf128mul_x8_lle_ti(be128 *x) 151 + { 152 + u64 a = be64_to_cpu(x->a); 153 + u64 b = be64_to_cpu(x->b); 154 + u64 _tt = xda_le(b & 0xff); /* avoid table lookup */ 155 + 156 + x->b = cpu_to_be64((b >> 8) | (a << 56)); 157 + x->a = cpu_to_be64((a >> 8) ^ (_tt << 48)); 158 + } 159 + 149 160 static void gf128mul_x8_bbe(be128 *x) 150 161 { 151 162 u64 a = be64_to_cpu(x->a); ··· 180 169 181 170 void gf128mul_lle(be128 *r, const be128 *b) 182 171 { 183 - be128 p[8]; 172 + /* 173 + * The p array should be aligned to twice the size of its element type, 174 + * so that every even/odd pair is guaranteed to share a cacheline 175 + * (assuming a cacheline size of 32 bytes or more, which is by far the 176 + * most common). This ensures that each be128_xor() call in the loop 177 + * takes the same amount of time regardless of the value of 'ch', which 178 + * is derived from function parameter 'b', which is commonly used as a 179 + * key, e.g., for GHASH. The odd array elements are all set to zero, 180 + * making each be128_xor() a NOP if its associated bit in 'ch' is not 181 + * set, and this is equivalent to calling be128_xor() conditionally. 182 + * This approach aims to avoid leaking information about such keys 183 + * through execution time variances. 184 + * 185 + * Unfortunately, __aligned(16) or higher does not work on x86 for 186 + * variables on the stack so we need to perform the alignment by hand. 187 + */ 188 + be128 array[16 + 3] = {}; 189 + be128 *p = PTR_ALIGN(&array[0], 2 * sizeof(be128)); 184 190 int i; 185 191 186 192 p[0] = *r; 187 193 for (i = 0; i < 7; ++i) 188 - gf128mul_x_lle(&p[i + 1], &p[i]); 194 + gf128mul_x_lle(&p[2 * i + 2], &p[2 * i]); 189 195 190 196 memset(r, 0, sizeof(*r)); 191 197 for (i = 0;;) { 192 198 u8 ch = ((u8 *)b)[15 - i]; 193 199 194 - if (ch & 0x80) 195 - be128_xor(r, r, &p[0]); 196 - if (ch & 0x40) 197 - be128_xor(r, r, &p[1]); 198 - if (ch & 0x20) 199 - be128_xor(r, r, &p[2]); 200 - if (ch & 0x10) 201 - be128_xor(r, r, &p[3]); 202 - if (ch & 0x08) 203 - be128_xor(r, r, &p[4]); 204 - if (ch & 0x04) 205 - be128_xor(r, r, &p[5]); 206 - if (ch & 0x02) 207 - be128_xor(r, r, &p[6]); 208 - if (ch & 0x01) 209 - be128_xor(r, r, &p[7]); 200 + be128_xor(r, r, &p[ 0 + !(ch & 0x80)]); 201 + be128_xor(r, r, &p[ 2 + !(ch & 0x40)]); 202 + be128_xor(r, r, &p[ 4 + !(ch & 0x20)]); 203 + be128_xor(r, r, &p[ 6 + !(ch & 0x10)]); 204 + be128_xor(r, r, &p[ 8 + !(ch & 0x08)]); 205 + be128_xor(r, r, &p[10 + !(ch & 0x04)]); 206 + be128_xor(r, r, &p[12 + !(ch & 0x02)]); 207 + be128_xor(r, r, &p[14 + !(ch & 0x01)]); 210 208 211 209 if (++i >= 16) 212 210 break; 213 211 214 - gf128mul_x8_lle(r); 212 + gf128mul_x8_lle_ti(r); /* use the time invariant version */ 215 213 } 216 214 } 217 215 EXPORT_SYMBOL(gf128mul_lle);