Skip to content

Commit e5be157

Browse files
Mikulas Patockatorvalds
authored andcommitted
hex2bin: make the function hex_to_bin constant-time
The function hex2bin is used to load cryptographic keys into device mapper targets dm-crypt and dm-integrity. It should take constant time independent on the processed data, so that concurrently running unprivileged code can't infer any information about the keys via microarchitectural convert channels. This patch changes the function hex_to_bin so that it contains no branches and no memory accesses. Note that this shouldn't cause performance degradation because the size of the new function is the same as the size of the old function (on x86-64) - and the new function causes no branch misprediction penalties. I compile-tested this function with gcc on aarch64 alpha arm hppa hppa64 i386 ia64 m68k mips32 mips64 powerpc powerpc64 riscv sh4 s390x sparc32 sparc64 x86_64 and with clang on aarch64 arm hexagon i386 mips32 mips64 powerpc powerpc64 s390x sparc32 sparc64 x86_64 to verify that there are no branches in the generated code. Signed-off-by: Mikulas Patocka <[email protected]> Cc: [email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 211ed54 commit e5be157

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

include/linux/kernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static inline char *hex_byte_pack_upper(char *buf, u8 byte)
285285
return buf;
286286
}
287287

288-
extern int hex_to_bin(char ch);
288+
extern int hex_to_bin(unsigned char ch);
289289
extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
290290
extern char *bin2hex(char *dst, const void *src, size_t count);
291291

lib/hexdump.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,33 @@ EXPORT_SYMBOL(hex_asc_upper);
2222
*
2323
* hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
2424
* input.
25+
*
26+
* This function is used to load cryptographic keys, so it is coded in such a
27+
* way that there are no conditions or memory accesses that depend on data.
28+
*
29+
* Explanation of the logic:
30+
* (ch - '9' - 1) is negative if ch <= '9'
31+
* ('0' - 1 - ch) is negative if ch >= '0'
32+
* we "and" these two values, so the result is negative if ch is in the range
33+
* '0' ... '9'
34+
* we are only interested in the sign, so we do a shift ">> 8"; note that right
35+
* shift of a negative value is implementation-defined, so we cast the
36+
* value to (unsigned) before the shift --- we have 0xffffff if ch is in
37+
* the range '0' ... '9', 0 otherwise
38+
* we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is
39+
* in the range '0' ... '9', 0 otherwise
40+
* we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0'
41+
* ... '9', -1 otherwise
42+
* the next line is similar to the previous one, but we need to decode both
43+
* uppercase and lowercase letters, so we use (ch & 0xdf), which converts
44+
* lowercase to uppercase
2545
*/
26-
int hex_to_bin(char ch)
46+
int hex_to_bin(unsigned char ch)
2747
{
28-
if ((ch >= '0') && (ch <= '9'))
29-
return ch - '0';
30-
ch = tolower(ch);
31-
if ((ch >= 'a') && (ch <= 'f'))
32-
return ch - 'a' + 10;
33-
return -1;
48+
unsigned char cu = ch & 0xdf;
49+
return -1 +
50+
((ch - '0' + 1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
51+
((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
3452
}
3553
EXPORT_SYMBOL(hex_to_bin);
3654

0 commit comments

Comments
 (0)