2008-05-20 19:49:55

by Adrian-Ken Rueegsegger

[permalink] [raw]
Subject: [PATCH 0/3] [CRYPTO] ripemd: Fix endian issues

These patches fix the endian issues reported by Sebastian Siewior for
the three remaining RIPEMD modules rmd160, rmd256 and rmd320.

crypto/rmd160.c | 37 +++++++++----------------------------
crypto/rmd256.c | 37 +++++++++----------------------------
crypto/rmd320.c | 37 +++++++++----------------------------
3 files changed, 27 insertions(+), 84 deletions(-)




2008-05-20 19:49:57

by Adrian-Ken Rueegsegger

[permalink] [raw]
Subject: [PATCH 2/3] [CRYPTO] rmd256: Fix endian problems

This patch fixes endian issues making rmd256 work
properly on big-endian machines.

Signed-off-by: Adrian-Ken Rueegsegger <[email protected]>
---
crypto/rmd256.c | 37 +++++++++----------------------------
1 files changed, 9 insertions(+), 28 deletions(-)

diff --git a/crypto/rmd256.c b/crypto/rmd256.c
index 060ee81..88f2203 100644
--- a/crypto/rmd256.c
+++ b/crypto/rmd256.c
@@ -44,7 +44,7 @@ struct rmd256_ctx {
#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */

#define ROUND(a, b, c, d, f, k, x, s) { \
- (a) += f((b), (c), (d)) + (x) + (k); \
+ (a) += f((b), (c), (d)) + le32_to_cpu(x) + (k); \
(a) = rol32((a), (s)); \
}

@@ -233,28 +233,6 @@ static void rmd256_transform(u32 *state, u32 const *in)
return;
}

-static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
-{
- while (words--) {
- le32_to_cpus(buf);
- buf++;
- }
-}
-
-static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
-{
- while (words--) {
- cpu_to_le32s(buf);
- buf++;
- }
-}
-
-static inline void rmd256_transform_helper(struct rmd256_ctx *ctx)
-{
- le32_to_cpu_array(ctx->buffer, sizeof(ctx->buffer) / sizeof(u32));
- rmd256_transform(ctx->state, ctx->buffer);
-}
-
static void rmd256_init(struct crypto_tfm *tfm)
{
struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
@@ -291,13 +269,13 @@ static void rmd256_update(struct crypto_tfm *tfm, const u8 *data,
memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
data, avail);

- rmd256_transform_helper(rctx);
+ rmd256_transform(rctx->state, rctx->buffer);
data += avail;
len -= avail;

while (len >= sizeof(rctx->buffer)) {
memcpy(rctx->buffer, data, sizeof(rctx->buffer));
- rmd256_transform_helper(rctx);
+ rmd256_transform(rctx->state, rctx->buffer);
data += sizeof(rctx->buffer);
len -= sizeof(rctx->buffer);
}
@@ -309,10 +287,12 @@ static void rmd256_update(struct crypto_tfm *tfm, const u8 *data,
static void rmd256_final(struct crypto_tfm *tfm, u8 *out)
{
struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
- u32 index, padlen;
+ u32 i, index, padlen;
u64 bits;
+ u32 *dst = (u32 *)out;
static const u8 padding[64] = { 0x80, };
- bits = rctx->byte_count << 3;
+
+ bits = cpu_to_le64(rctx->byte_count << 3);

/* Pad out to 56 mod 64 */
index = rctx->byte_count & 0x3f;
@@ -323,7 +303,8 @@ static void rmd256_final(struct crypto_tfm *tfm, u8 *out)
rmd256_update(tfm, (const u8 *)&bits, sizeof(bits));

/* Store state in digest */
- memcpy(out, rctx->state, sizeof(rctx->state));
+ for (i = 0; i < 8; i++)
+ dst[i] = cpu_to_le32(rctx->state[i]);

/* Wipe context */
memset(rctx, 0, sizeof(*rctx));
--
1.5.2.5


2008-05-20 19:49:56

by Adrian-Ken Rueegsegger

[permalink] [raw]
Subject: [PATCH 1/3] [CRYPTO] rmd160: Fix endian problems

This patch fixes endian issues making rmd160 work
properly on big-endian machines.

Signed-off-by: Adrian-Ken Rueegsegger <[email protected]>
---
crypto/rmd160.c | 37 +++++++++----------------------------
1 files changed, 9 insertions(+), 28 deletions(-)

diff --git a/crypto/rmd160.c b/crypto/rmd160.c
index 80d647a..136e31f 100644
--- a/crypto/rmd160.c
+++ b/crypto/rmd160.c
@@ -47,7 +47,7 @@ struct rmd160_ctx {
#define F5(x, y, z) (x ^ (y | ~z))

#define ROUND(a, b, c, d, e, f, k, x, s) { \
- (a) += f((b), (c), (d)) + (x) + (k); \
+ (a) += f((b), (c), (d)) + le32_to_cpu(x) + (k); \
(a) = rol32((a), (s)) + (e); \
(c) = rol32((c), 10); \
}
@@ -261,28 +261,6 @@ static void rmd160_transform(u32 *state, u32 const *in)
return;
}

-static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
-{
- while (words--) {
- le32_to_cpus(buf);
- buf++;
- }
-}
-
-static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
-{
- while (words--) {
- cpu_to_le32s(buf);
- buf++;
- }
-}
-
-static inline void rmd160_transform_helper(struct rmd160_ctx *ctx)
-{
- le32_to_cpu_array(ctx->buffer, sizeof(ctx->buffer) / sizeof(u32));
- rmd160_transform(ctx->state, ctx->buffer);
-}
-
static void rmd160_init(struct crypto_tfm *tfm)
{
struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
@@ -316,13 +294,13 @@ static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
data, avail);

- rmd160_transform_helper(rctx);
+ rmd160_transform(rctx->state, rctx->buffer);
data += avail;
len -= avail;

while (len >= sizeof(rctx->buffer)) {
memcpy(rctx->buffer, data, sizeof(rctx->buffer));
- rmd160_transform_helper(rctx);
+ rmd160_transform(rctx->state, rctx->buffer);
data += sizeof(rctx->buffer);
len -= sizeof(rctx->buffer);
}
@@ -334,10 +312,12 @@ static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
{
struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
- u32 index, padlen;
+ u32 i, index, padlen;
u64 bits;
+ u32 *dst = (u32 *)out;
static const u8 padding[64] = { 0x80, };
- bits = rctx->byte_count << 3;
+
+ bits = cpu_to_le64(rctx->byte_count << 3);

/* Pad out to 56 mod 64 */
index = rctx->byte_count & 0x3f;
@@ -348,7 +328,8 @@ static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
rmd160_update(tfm, (const u8 *)&bits, sizeof(bits));

/* Store state in digest */
- memcpy(out, rctx->state, sizeof(rctx->state));
+ for (i = 0; i < 5; i++)
+ dst[i] = cpu_to_le32(rctx->state[i]);

/* Wipe context */
memset(rctx, 0, sizeof(*rctx));
--
1.5.2.5


2008-05-20 19:49:58

by Adrian-Ken Rueegsegger

[permalink] [raw]
Subject: [PATCH 3/3] [CRYPTO] rmd320: Fix endian problems

This patch fixes endian issues making rmd320 work
properly on big-endian machines.

Signed-off-by: Adrian-Ken Rueegsegger <[email protected]>
---
crypto/rmd320.c | 37 +++++++++----------------------------
1 files changed, 9 insertions(+), 28 deletions(-)

diff --git a/crypto/rmd320.c b/crypto/rmd320.c
index b39c054..5b172f8 100644
--- a/crypto/rmd320.c
+++ b/crypto/rmd320.c
@@ -47,7 +47,7 @@ struct rmd320_ctx {
#define F5(x, y, z) (x ^ (y | ~z))

#define ROUND(a, b, c, d, e, f, k, x, s) { \
- (a) += f((b), (c), (d)) + (x) + (k); \
+ (a) += f((b), (c), (d)) + le32_to_cpu(x) + (k); \
(a) = rol32((a), (s)) + (e); \
(c) = rol32((c), 10); \
}
@@ -280,28 +280,6 @@ static void rmd320_transform(u32 *state, u32 const *in)
return;
}

-static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
-{
- while (words--) {
- le32_to_cpus(buf);
- buf++;
- }
-}
-
-static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
-{
- while (words--) {
- cpu_to_le32s(buf);
- buf++;
- }
-}
-
-static inline void rmd320_transform_helper(struct rmd320_ctx *ctx)
-{
- le32_to_cpu_array(ctx->buffer, sizeof(ctx->buffer) / sizeof(u32));
- rmd320_transform(ctx->state, ctx->buffer);
-}
-
static void rmd320_init(struct crypto_tfm *tfm)
{
struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
@@ -340,13 +318,13 @@ static void rmd320_update(struct crypto_tfm *tfm, const u8 *data,
memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
data, avail);

- rmd320_transform_helper(rctx);
+ rmd320_transform(rctx->state, rctx->buffer);
data += avail;
len -= avail;

while (len >= sizeof(rctx->buffer)) {
memcpy(rctx->buffer, data, sizeof(rctx->buffer));
- rmd320_transform_helper(rctx);
+ rmd320_transform(rctx->state, rctx->buffer);
data += sizeof(rctx->buffer);
len -= sizeof(rctx->buffer);
}
@@ -358,10 +336,12 @@ static void rmd320_update(struct crypto_tfm *tfm, const u8 *data,
static void rmd320_final(struct crypto_tfm *tfm, u8 *out)
{
struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
- u32 index, padlen;
+ u32 i, index, padlen;
u64 bits;
+ u32 *dst = (u32 *)out;
static const u8 padding[64] = { 0x80, };
- bits = rctx->byte_count << 3;
+
+ bits = cpu_to_le64(rctx->byte_count << 3);

/* Pad out to 56 mod 64 */
index = rctx->byte_count & 0x3f;
@@ -372,7 +352,8 @@ static void rmd320_final(struct crypto_tfm *tfm, u8 *out)
rmd320_update(tfm, (const u8 *)&bits, sizeof(bits));

/* Store state in digest */
- memcpy(out, rctx->state, sizeof(rctx->state));
+ for (i = 0; i < 10; i++)
+ dst[i] = cpu_to_le32(rctx->state[i]);

/* Wipe context */
memset(rctx, 0, sizeof(*rctx));
--
1.5.2.5


Subject: Re: [PATCH 0/3] [CRYPTO] ripemd: Fix endian issues

* Adrian-Ken Rueegsegger | 2008-05-20 21:49:49 [+0200]:

>These patches fix the endian issues reported by Sebastian Siewior for
>the three remaining RIPEMD modules rmd160, rmd256 and rmd320.
>
> crypto/rmd160.c | 37 +++++++++----------------------------
> crypto/rmd256.c | 37 +++++++++----------------------------
> crypto/rmd320.c | 37 +++++++++----------------------------
> 3 files changed, 27 insertions(+), 84 deletions(-)
For the whole series:
Acked-by: Sebastian Siewior <[email protected]>

Thanks for fixing this.
Sebastian

2008-05-27 16:24:18

by Herbert Xu

[permalink] [raw]
Subject: Re: [PATCH 0/3] [CRYPTO] ripemd: Fix endian issues

On Tue, May 20, 2008 at 09:49:49PM +0200, Adrian-Ken Rueegsegger wrote:
> These patches fix the endian issues reported by Sebastian Siewior for
> the three remaining RIPEMD modules rmd160, rmd256 and rmd320.
>
> crypto/rmd160.c | 37 +++++++++----------------------------
> crypto/rmd256.c | 37 +++++++++----------------------------
> crypto/rmd320.c | 37 +++++++++----------------------------
> 3 files changed, 27 insertions(+), 84 deletions(-)

All applied. Thanks a lot!
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt