mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Cc: barebox@lists.infradead.org, Andre <armccurdy@gmail.com>
Subject: Re: [PATCH 2/2] sha1/sha256: use be32_to_cpu and cpu_to_be32
Date: Fri, 24 Sep 2010 14:56:15 +0200	[thread overview]
Message-ID: <20100924125615.GT23406@pengutronix.de> (raw)
In-Reply-To: <20100924125254.GS23406@pengutronix.de>

On Fri, Sep 24, 2010 at 02:52:55PM +0200, Sascha Hauer wrote:
> On Fri, Sep 24, 2010 at 01:43:50PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> > On 13:15 Fri 24 Sep     , Sascha Hauer wrote:
> > > On Fri, Sep 24, 2010 at 01:00:45PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> > > > > >>>>-	    | ( (uint32_t) (b)[(i) + 3]       );	\
> > > > > >>>>-}
> > > > > >>>>-#endif
> > > > > >>>>-
> > > > > >>>>-#ifndef PUT_UINT32_BE
> > > > > >>>>-#define PUT_UINT32_BE(n,b,i) {				\
> > > > > >>>>-	(b)[(i)    ] = (unsigned char) ( (n)>>   24 );	\
> > > > > >>>>-	(b)[(i) + 1] = (unsigned char) ( (n)>>   16 );	\
> > > > > >>>>-	(b)[(i) + 2] = (unsigned char) ( (n)>>    8 );	\
> > > > > >>>>-	(b)[(i) + 3] = (unsigned char) ( (n)       );	\
> > > > > >>>>-}
> > > > > >>>>-#endif
> > > > > >>>>+#define GET_UINT32_BE(n,b,i) (n) = be32_to_cpu(((uint32_t*)(b))[i / 4])
> > > > > >>>>+#define PUT_UINT32_BE(n,b,i) ((uint32_t*)(b))[i / 4] = cpu_to_be32(n)
> > > > > >>>>
> > > > > >>>>
> > > > > >>>
> > > > > >>>The previous macros served two purposes: endian swapping and performing
> > > > > >>>the memory accesses byte-by-byte. New versions are unsafe for CPUs which
> > > > > >>>do not support misaligned 32bit memory accesses.
> > > > > >>
> > > > > >>Indeed. We have get_unaligned_be32() / put_unaligned_be32(). These should be
> > > > > >>the correct functions, right?
> > > > > >
> > > > > >no-nned IIRC as be32_to_cpu and cpu_to_be32 already handle this
> > > > > >depending on the arch
> > > > > >
> > > > > 
> > > > > I think get_unaligned_be32() / put_unaligned_be32() are correct in
> > > > > this case. be32_to_cpu / cpu_to_be32 perform endian swapping (if
> > > > > required) with source and destination both being 32bit variables,
> > > > > not memory locations ?
> > > > no the arch have to handle this
> > > 
> > > No. As Andre mentioned cpu_to_be32 operates on variables, not on
> > > pointers. with ((uint32_t*)(b))[i / 4] you cast b to a pointer to
> > > uint32_t which you dereference with [i / 4]. This has nothing to do
> > > with cpu_to_be32 and will crash if the architecture does not allow
> > > unaligned accesses.
> > except cpu_to_be 32 do a swapb so it's safe
> 
> cpu_to_be32 does a __swap32 and not a swapb.
> 
> And I can only repeat myself: The problem is *not* cpu_to_be32 but the
> argument you pass *to* cpu_to_be32. Let me put it in other words:
> 
> x = be32_to_cpu(((uint32_t*)(b))[i / 4])
> 
> can be written as
> 
> u32 y = ((uint32_t*)(b))[i / 4];
> x = be32_to_cpu(y);
> 
> which can also be written as:
> 
> u32 *z = (uint32_t *)b;
> u32 y = z[i / 4];
> x = be32_to_cpu(y);
> 
> And this will crash in the first line if b is not aligned.

I mean in the second line because that's where the pointer is
dereferenced.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

      reply	other threads:[~2010-09-24 12:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-21 13:28 [PATCH 1/2] sha1: use unit32_t and uint8_t Jean-Christophe PLAGNIOL-VILLARD
2010-09-21 13:28 ` [PATCH 2/2] sha1/sha256: use be32_to_cpu and cpu_to_be32 Jean-Christophe PLAGNIOL-VILLARD
2010-09-24  7:00   ` Andre
2010-09-24  7:26     ` Jean-Christophe PLAGNIOL-VILLARD
2010-09-24  7:43     ` Sascha Hauer
2010-09-24  8:34       ` Jean-Christophe PLAGNIOL-VILLARD
2010-09-24 10:16         ` Andre
2010-09-24 11:00           ` Jean-Christophe PLAGNIOL-VILLARD
2010-09-24 11:15             ` Sascha Hauer
2010-09-24 11:43               ` Jean-Christophe PLAGNIOL-VILLARD
2010-09-24 12:52                 ` Sascha Hauer
2010-09-24 12:56                   ` Sascha Hauer [this message]

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=20100924125615.GT23406@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=armccurdy@gmail.com \
    --cc=barebox@lists.infradead.org \
    --cc=plagnioj@jcrosoft.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