at commit 205cccd0
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Crypto library utility functions |
| 4 | * |
| 5 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 6 | */ |
| 7 | |
| 8 | #include <crypto/utils.h> |
| 9 | #include <linux/export.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/unaligned.h> |
| 12 | |
| 13 | /* |
| 14 | * XOR @len bytes from @src1 and @src2 together, writing the result to @dst |
| 15 | * (which may alias one of the sources). Don't call this directly; call |
| 16 | * crypto_xor() or crypto_xor_cpy() instead. |
| 17 | */ |
| 18 | void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len) |
| 19 | { |
| 20 | int relalign = 0; |
| 21 | |
| 22 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { |
| 23 | int size = sizeof(unsigned long); |
| 24 | int d = (((unsigned long)dst ^ (unsigned long)src1) | |
| 25 | ((unsigned long)dst ^ (unsigned long)src2)) & |
| 26 | (size - 1); |
| 27 | |
| 28 | relalign = d ? 1 << __ffs(d) : size; |
| 29 | |
| 30 | /* |
| 31 | * If we care about alignment, process as many bytes as |
| 32 | * needed to advance dst and src to values whose alignments |
| 33 | * equal their relative alignment. This will allow us to |
| 34 | * process the remainder of the input using optimal strides. |
| 35 | */ |
| 36 | while (((unsigned long)dst & (relalign - 1)) && len > 0) { |
| 37 | *dst++ = *src1++ ^ *src2++; |
| 38 | len--; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) { |
| 43 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { |
| 44 | u64 l = get_unaligned((u64 *)src1) ^ |
| 45 | get_unaligned((u64 *)src2); |
| 46 | put_unaligned(l, (u64 *)dst); |
| 47 | } else { |
| 48 | *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2; |
| 49 | } |
| 50 | dst += 8; |
| 51 | src1 += 8; |
| 52 | src2 += 8; |
| 53 | len -= 8; |
| 54 | } |
| 55 | |
| 56 | while (len >= 4 && !(relalign & 3)) { |
| 57 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { |
| 58 | u32 l = get_unaligned((u32 *)src1) ^ |
| 59 | get_unaligned((u32 *)src2); |
| 60 | put_unaligned(l, (u32 *)dst); |
| 61 | } else { |
| 62 | *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2; |
| 63 | } |
| 64 | dst += 4; |
| 65 | src1 += 4; |
| 66 | src2 += 4; |
| 67 | len -= 4; |
| 68 | } |
| 69 | |
| 70 | while (len >= 2 && !(relalign & 1)) { |
| 71 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { |
| 72 | u16 l = get_unaligned((u16 *)src1) ^ |
| 73 | get_unaligned((u16 *)src2); |
| 74 | put_unaligned(l, (u16 *)dst); |
| 75 | } else { |
| 76 | *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2; |
| 77 | } |
| 78 | dst += 2; |
| 79 | src1 += 2; |
| 80 | src2 += 2; |
| 81 | len -= 2; |
| 82 | } |
| 83 | |
| 84 | while (len--) |
| 85 | *dst++ = *src1++ ^ *src2++; |
| 86 | } |
| 87 | EXPORT_SYMBOL_GPL(__crypto_xor); |
| 88 | |
| 89 | MODULE_DESCRIPTION("Crypto library utility functions"); |
| 90 | MODULE_LICENSE("GPL"); |