Skip to contentSkip to conversation

crypto: rmd320 - use swap macro in rmd320_transform

PATCH In review Gustavo A. R. Silva 3 participants 5 messages Jul 18, 2018 – Jul 27, 2018 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: rmd320 - use swap macro in rmd320_transform

Make use of the swap macro and remove unnecessary variable *tmp*. This makes the code easier to read and maintain.

This code was detected with the help of Coccinelle.

---
 crypto/rmd320.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
crypto/rmd320.c +6 -6
index ab3cf93..3ae1df5 100644
@@ -53,7 +53,7 @@ struct rmd320_ctx {
 
 static void rmd320_transform(u32 *state, const __le32 *in)
 {
-	u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
+	u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
 
 	/* Initialize left lane */
 	aa = state[0];
@@ -106,7 +106,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12],  6);
 
 	/* Swap contents of "a" registers */
-	tmp = aa; aa = aaa; aaa = tmp;
+	swap(aa, aaa);
 
 	/* round 2: left lane" */
 	ROUND(ee, aa, bb, cc, dd, F2, K2, in[7],   7);
@@ -145,7 +145,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2],  11);
 
 	/* Swap contents of "b" registers */
-	tmp = bb; bb = bbb; bbb = tmp;
+	swap(bb, bbb);
 
 	/* round 3: left lane" */
 	ROUND(dd, ee, aa, bb, cc, F3, K3, in[3],  11);
@@ -184,7 +184,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13],  5);
 
 	/* Swap contents of "c" registers */
-	tmp = cc; cc = ccc; ccc = tmp;
+	swap(cc, ccc);
 
 	/* round 4: left lane" */
 	ROUND(cc, dd, ee, aa, bb, F4, K4, in[1],  11);
@@ -223,7 +223,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14],  8);
 
 	/* Swap contents of "d" registers */
-	tmp = dd; dd = ddd; ddd = tmp;
+	swap(dd, ddd);
 
 	/* round 5: left lane" */
 	ROUND(bb, cc, dd, ee, aa, F5, K5, in[4],   9);
@@ -262,7 +262,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
 
 	/* Swap contents of "e" registers */
-	tmp = ee; ee = eee; eee = tmp;
+	swap(ee, eee);
 
 	/* combine results */
 	state[0] += aa;
Signature · 1 line
2.7.4
#1 lore raw

make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform)

Quoted 1 line · Gustavo A. R. Silva

swap

Perhaps the swap macro should verify that the swap(a, b) arguments are the same type.

Something like the patch below, but this patch causes a compilation failure on at least a couple cases that aren't obviously correct in

net/netfilter/ipset/ip_set_bitmap_port.c
where a u16 is swapped with a u32.

and

net/netfilter/ipset/ip_set_hash_netport.c
where a single bit bitfield is swapped with a u8

---
 include/linux/kernel.h | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
include/linux/kernel.h +8 -3
index d6aac75b51ba..506b59e0da24 100644
@@ -969,14 +969,19 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  */
 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
 
-
 /**
  * swap - swap values of @a and @b
  * @a: first value
  * @b: second value
+ * @a and @b must be the same type
  */
-#define swap(a, b) \
-	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#define swap(a, b)							\
+do {									\
+	typeof(a) __tmp = (a);						\
+	BUILD_BUG_ON(!__same_type(typeof(a), typeof(b)));		\
+	(a) = (b);							\
+	(b) = __tmp;							\
+} while (0)
 
 /* This counts to 12. Any more, it will return 13th argument. */
 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
#2 lore raw

Re: make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform)

Hi Joe,

I like the idea. I'll take a look at the cases you mention and see if I can make them work with your version of swap.

BTW, I wonder if it would be more convenient to trigger a build warning instead of an error.

What do you think?

Thanks
--
Gustavo

Quoted 49 lines · Joe Perches
Quoted 1 line · Gustavo A. R. Silva

swap

Perhaps the swap macro should verify that the swap(a, b) arguments are the same type.

Something like the patch below, but this patch causes a compilation failure on at least a couple cases that aren't obviously correct in

net/netfilter/ipset/ip_set_bitmap_port.c
where a u16 is swapped with a u32.

and

net/netfilter/ipset/ip_set_hash_netport.c
where a single bit bitfield is swapped with a u8

---
 include/linux/kernel.h | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
include/linux/kernel.h +8 -3
index d6aac75b51ba..506b59e0da24 100644
@@ -969,14 +969,19 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  */
 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
 
-
 /**
  * swap - swap values of @a and @b
  * @a: first value
  * @b: second value
+ * @a and @b must be the same type
  */
-#define swap(a, b) \
-	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#define swap(a, b)							\
+do {									\
+	typeof(a) __tmp = (a);						\
+	BUILD_BUG_ON(!__same_type(typeof(a), typeof(b)));		\
+	(a) = (b);							\
+	(b) = __tmp;							\
+} while (0)
 
 /* This counts to 12. Any more, it will return 13th argument. */
 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
#3 lore raw

Re: make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform)

Quoted 9 lines · Gustavo A. R. Silva

Hi Joe,

I like the idea. I'll take a look at the cases you mention and see if I can make them work with your version of swap.

BTW, I wonder if it would be more convenient to trigger a build warning instead of an error.

What do you think?

I can't think of a way to have a #pragma (or _Pragma equivalent) emit a warning on a condition.

maintainer
#4 lore raw
In reply to Gustavo A. R. Silva
Quoted 6 lines · Gustavo A. R. Silva

Make use of the swap macro and remove unnecessary variable *tmp*. This makes the code easier to read and maintain.

This code was detected with the help of Coccinelle.

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt