diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2007-11-20 04:26:06 -0500 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2008-01-10 16:16:17 -0500 |
commit | 7613636def82092a5c7b6322078a2af832410417 (patch) | |
tree | a05fec8d522e78d21b7b45f9ac330bcd1372e1af /crypto | |
parent | fcd06755936d2209b69650d2a7cc99cbcd3ccc67 (diff) |
[CRYPTO] api: Add crypto_inc and crypto_xor
With the addition of more stream ciphers we need to curb the proliferation
of ad-hoc xor functions. This patch creates a generic pair of functions,
crypto_inc and crypto_xor which does big-endian increment and exclusive or,
respectively.
For optimum performance, they both use u32 operations so alignment must be
as that of u32 even though the arguments are of type u8 *.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/algapi.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/crypto/algapi.c b/crypto/algapi.c index 8383282de1dd..08eca6da1d53 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c | |||
@@ -605,6 +605,53 @@ int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm) | |||
605 | } | 605 | } |
606 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); | 606 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); |
607 | 607 | ||
608 | static inline void crypto_inc_byte(u8 *a, unsigned int size) | ||
609 | { | ||
610 | u8 *b = (a + size); | ||
611 | u8 c; | ||
612 | |||
613 | for (; size; size--) { | ||
614 | c = *--b + 1; | ||
615 | *b = c; | ||
616 | if (c) | ||
617 | break; | ||
618 | } | ||
619 | } | ||
620 | |||
621 | void crypto_inc(u8 *a, unsigned int size) | ||
622 | { | ||
623 | __be32 *b = (__be32 *)(a + size); | ||
624 | u32 c; | ||
625 | |||
626 | for (; size >= 4; size -= 4) { | ||
627 | c = be32_to_cpu(*--b) + 1; | ||
628 | *b = cpu_to_be32(c); | ||
629 | if (c) | ||
630 | return; | ||
631 | } | ||
632 | |||
633 | crypto_inc_byte(a, size); | ||
634 | } | ||
635 | EXPORT_SYMBOL_GPL(crypto_inc); | ||
636 | |||
637 | static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size) | ||
638 | { | ||
639 | for (; size; size--) | ||
640 | *a++ ^= *b++; | ||
641 | } | ||
642 | |||
643 | void crypto_xor(u8 *dst, const u8 *src, unsigned int size) | ||
644 | { | ||
645 | u32 *a = (u32 *)dst; | ||
646 | u32 *b = (u32 *)src; | ||
647 | |||
648 | for (; size >= 4; size -= 4) | ||
649 | *a++ ^= *b++; | ||
650 | |||
651 | crypto_xor_byte((u8 *)a, (u8 *)b, size); | ||
652 | } | ||
653 | EXPORT_SYMBOL_GPL(crypto_xor); | ||
654 | |||
608 | static int __init crypto_algapi_init(void) | 655 | static int __init crypto_algapi_init(void) |
609 | { | 656 | { |
610 | crypto_init_proc(); | 657 | crypto_init_proc(); |