2007-11-19 18:34:51

by Tan Swee Heng

[permalink] [raw]
Subject: Moving xor_* functions into common header file

Hi,

'grep "static void xor_" crypto/*' shows that cbc.c, ctr.c, pcbc.c and
xcbc.c share similar xor_* functions. It seems a good idea to
encourage code reuse by putting them into a common header (iirc
Evgeniy suggested it when reviewing one of my earlier mail). It will
also be useful to me when porting other eSTREAM ciphers. So I would
like to move them into include/crypto/xor_n.h and make them all
"static inline". Any objections?

For xor_byte and xor_quad, the implementation in ctr.c differs from
that in cbc.c and pcbc.c. In cbc.c and pcbc.c we have:

static void xor_byte(u8 *a, const u8 *b, unsigned int bs)
{
do {
*a++ ^= *b++;
} while (--bs);
}

static void xor_quad(u8 *dst, const u8 *src, unsigned int bs)
{
u32 *a = (u32 *)dst;
u32 *b = (u32 *)src;

do {
*a++ ^= *b++;
} while ((bs -= 4));
}

while in ctr.c it is:

static void xor_byte(u8 *a, const u8 *b, unsigned int bs)
{
for (; bs; bs--)
*a++ ^= *b++;
}

static void xor_quad(u8 *dst, const u8 *src, unsigned int bs)
{
u32 *a = (u32 *)dst;
u32 *b = (u32 *)src;

for (; bs >= 4; bs -= 4)
*a++ ^= *b++;

xor_byte((u8 *)a, (u8 *)b, bs);
}

The former saves one check by assuming bs > 0. For cbc.c and pcbc.c,
we can assume bs > 0 since xor() uses the non-negative blocksize. For
ctr.c, we check bs == 0 as the call to xor_quad uses min(nbytes,
bsize). Given that we need both semantics, I would like to propose
renaming the former as xor_byte_quick() and xor_quad_block() in the
common header. Would that be fine?

Swee Heng


2007-11-20 07:33:39

by Herbert Xu

[permalink] [raw]
Subject: Re: Moving xor_* functions into common header file

On Tue, Nov 20, 2007 at 02:34:48AM +0800, Tan Swee Heng wrote:
> Hi,
>
> 'grep "static void xor_" crypto/*' shows that cbc.c, ctr.c, pcbc.c and
> xcbc.c share similar xor_* functions. It seems a good idea to
> encourage code reuse by putting them into a common header (iirc
> Evgeniy suggested it when reviewing one of my earlier mail). It will
> also be useful to me when porting other eSTREAM ciphers. So I would
> like to move them into include/crypto/xor_n.h and make them all
> "static inline". Any objections?

Do you actually need to xor anything that's not a multiple of
8 bytes?

I'm currently working on a patch to just restrict the block
size for non-stream ciphers to multiples of 8 since that's what
we have anyway. That allows us to just use bitmap_xor.

Cheers,
--
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

2007-11-20 08:52:24

by Herbert Xu

[permalink] [raw]
Subject: Re: Moving xor_* functions into common header file

On Tue, Nov 20, 2007 at 03:33:35PM +0800, Herbert Xu wrote:
>
> I'm currently working on a patch to just restrict the block
> size for non-stream ciphers to multiples of 8 since that's what
> we have anyway. That allows us to just use bitmap_xor.

Nevermind, obviously stream ciphers do need xors to the byte.
So I'm spinning up a patch to put this into algapi.c and hence
available for all crypto algorithms.

Cheers,
--
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