diff mbox series

[v3,2/7] tcgbios: Use assembly for 32 bit rotr in sha256

Message ID 20210709025313.674287-3-stefanb@linux.vnet.ibm.com
State Superseded
Headers show
Series tcgbios: Use the proper hashes for the TPM 2 PCR banks | expand

Commit Message

Stefan Berger July 9, 2021, 2:53 a.m. UTC
From: Stefan Berger <stefanb@linux.ibm.com>

Use assembly for the 32 bit rotr in the sha256 implementation.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 lib/libtpm/sha256.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

Comments

Alexey Kardashevskiy July 9, 2021, 5:35 a.m. UTC | #1
On 09/07/2021 12:53, Stefan Berger wrote:
> From: Stefan Berger <stefanb@linux.ibm.com>
> 
> Use assembly for the 32 bit rotr in the sha256 implementation.

Why bother with asm here?


> 
> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> ---
>   lib/libtpm/sha256.c | 14 ++++++++++----
>   1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/libtpm/sha256.c b/lib/libtpm/sha256.c
> index fb67e19..292e54e 100644
> --- a/lib/libtpm/sha256.c
> +++ b/lib/libtpm/sha256.c
> @@ -22,10 +22,16 @@ typedef struct _sha256_ctx {
>   	uint32_t h[8];
>   } sha256_ctx;
>   
> -static inline uint32_t rotr(uint32_t x, uint8_t n)
> -{
> -	return (x >> n) | (x << (32 - n));
> -}
> +#define rotr(VAL, N)				\
> +({						\
> +	uint32_t res;				\
> +	__asm__ (				\
> +		"rotrwi %0, %1, %2\n\t"		\
> +		: "=r" (res)			\
> +		: "r" (VAL), "i" (N)		\
> +	);					\
> +	res; 					\
> +})
>   
>   static inline uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
>   {
>
Stefan Berger July 9, 2021, 1:52 p.m. UTC | #2
On 7/9/21 1:35 AM, Alexey Kardashevskiy wrote:
>
>
> On 09/07/2021 12:53, Stefan Berger wrote:
>> From: Stefan Berger <stefanb@linux.ibm.com>
>>
>> Use assembly for the 32 bit rotr in the sha256 implementation.
>
> Why bother with asm here?

I used assembly now for rotation for all the other ones and so I though 
I'd convert this one as well.
Alexey Kardashevskiy July 9, 2021, 1:56 p.m. UTC | #3
On 09/07/2021 23:52, Stefan Berger wrote:
> 
> On 7/9/21 1:35 AM, Alexey Kardashevskiy wrote:
>>
>>
>> On 09/07/2021 12:53, Stefan Berger wrote:
>>> From: Stefan Berger <stefanb@linux.ibm.com>
>>>
>>> Use assembly for the 32 bit rotr in the sha256 implementation.
>>
>> Why bother with asm here?
> 
> I used assembly now for rotation for all the other ones and so I though 
> I'd convert this one as well.


Then add this to the commit log please, it is not that self-explanatory 
and may give wrong impression that this is somehow performance critical 
or something :) Thanks,
diff mbox series

Patch

diff --git a/lib/libtpm/sha256.c b/lib/libtpm/sha256.c
index fb67e19..292e54e 100644
--- a/lib/libtpm/sha256.c
+++ b/lib/libtpm/sha256.c
@@ -22,10 +22,16 @@  typedef struct _sha256_ctx {
 	uint32_t h[8];
 } sha256_ctx;
 
-static inline uint32_t rotr(uint32_t x, uint8_t n)
-{
-	return (x >> n) | (x << (32 - n));
-}
+#define rotr(VAL, N)				\
+({						\
+	uint32_t res;				\
+	__asm__ (				\
+		"rotrwi %0, %1, %2\n\t"		\
+		: "=r" (res)			\
+		: "r" (VAL), "i" (N)		\
+	);					\
+	res; 					\
+})
 
 static inline uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
 {