From: Joe Perches <joe@perches.com>
To: Mikulas Patocka <mpatocka@redhat.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
Andy Shevchenko <andy@kernel.org>
Cc: dm-devel@redhat.com, linux-kernel@vger.kernel.org,
linux-crypto@vger.kernel.org,
Herbert Xu <herbert@gondor.apana.org.au>,
"David S. Miller" <davem@davemloft.net>,
Mike Snitzer <msnitzer@redhat.com>,
Mimi Zohar <zohar@linux.ibm.com>,
Milan Broz <gmazyland@gmail.com>
Subject: Re: [PATCH] hex2bin: make the function hex_to_bin constant-time
Date: Sun, 24 Apr 2022 14:30:15 -0700 [thread overview]
Message-ID: <637e370ff1d462cd97bb0dbd85d2aad7abc31d61.camel@perches.com> (raw)
In-Reply-To: <alpine.LRH.2.02.2204241648270.17244@file01.intranet.prod.int.rdu2.redhat.com>
On Sun, 2022-04-24 at 16:54 -0400, Mikulas Patocka wrote:
> This patch changes the function hex_to_bin so that it contains no branches
> and no memory accesses.
[]
> +++ linux-2.6/lib/hexdump.c 2022-04-24 18:51:20.000000000 +0200
[]
> + * the next line is similar to the previous one, but we need to decode both
> + * uppercase and lowercase letters, so we use (ch & 0xdf), which converts
> + * lowercase to uppercase
> */
> int hex_to_bin(char ch)
> {
> - if ((ch >= '0') && (ch <= '9'))
> - return ch - '0';
> - ch = tolower(ch);
> - if ((ch >= 'a') && (ch <= 'f'))
> - return ch - 'a' + 10;
> - return -1;
> + return -1 +
> + ((ch - '0' + 1) & (((ch - '9' - 1) & ('0' - 1 - ch)) >> 8)) +
> + (((ch & 0xdf) - 'A' + 11) & ((((ch & 0xdf) - 'F' - 1) & ('A' - 1 - (ch & 0xdf))) >> 8));
probably easier to read using a temporary for ch & 0xdf
int CH = ch & 0xdf;
return -1 +
((ch - '0' + 1) & (((ch - '9' - 1) & ('0' - 1 - ch)) >> 8)) +
((CH - 'A' + 11) & (((CH - 'F' - 1) & ('A' - 1 - CH)) >> 8));
next prev parent reply other threads:[~2022-04-24 21:30 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-24 20:54 [PATCH] hex2bin: make the function hex_to_bin constant-time Mikulas Patocka
2022-04-24 21:30 ` Joe Perches [this message]
2022-04-24 21:37 ` Linus Torvalds
2022-04-24 21:42 ` Linus Torvalds
2022-04-25 9:37 ` David Laight
2022-04-25 11:04 ` Mikulas Patocka
2022-04-25 12:59 ` David Laight
2022-04-25 13:33 ` Mikulas Patocka
2022-04-25 12:07 ` [PATCH v2] " Mikulas Patocka
2022-04-25 17:53 ` Linus Torvalds
2022-05-04 8:38 ` Stafford Horne
2022-05-04 8:57 ` Mikulas Patocka
2022-05-04 9:20 ` Andy Shevchenko
2022-05-04 9:47 ` Milan Broz
2022-05-04 9:50 ` Jason A. Donenfeld
2022-05-04 11:54 ` Mikulas Patocka
2022-05-04 9:42 ` Jason A. Donenfeld
2022-05-04 9:44 ` Jason A. Donenfeld
2022-05-04 9:57 ` Jason A. Donenfeld
2022-05-04 10:07 ` Andy Shevchenko
2022-05-04 10:15 ` Jason A. Donenfeld
2022-05-04 18:00 ` Linus Torvalds
2022-05-04 19:42 ` Jason A. Donenfeld
2022-05-04 19:51 ` Linus Torvalds
2022-05-04 20:00 ` Linus Torvalds
2022-05-04 20:12 ` Stafford Horne
2022-05-04 20:26 ` Linus Torvalds
2022-05-04 21:24 ` Linus Torvalds
2022-05-04 19:57 ` Stafford Horne
2022-05-04 20:10 ` Linus Torvalds
2022-05-04 20:38 ` Stafford Horne
2022-05-08 0:37 ` Stafford Horne
2022-05-11 12:17 ` Stafford Horne
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=637e370ff1d462cd97bb0dbd85d2aad7abc31d61.camel@perches.com \
--to=joe@perches.com \
--cc=andy@kernel.org \
--cc=davem@davemloft.net \
--cc=dm-devel@redhat.com \
--cc=gmazyland@gmail.com \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mpatocka@redhat.com \
--cc=msnitzer@redhat.com \
--cc=torvalds@linux-foundation.org \
--cc=zohar@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox