^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Constant-time equality testing of memory regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * James Yonan <james@openvpn.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Daniel Borkmann <dborkman@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This file is provided under a dual BSD/GPLv2 license. When using or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * redistributing this file, you may do so under either license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * GPL LICENSE SUMMARY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright(c) 2013 OpenVPN Technologies, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * it under the terms of version 2 of the GNU General Public License as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * This program is distributed in the hope that it will be useful, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * The full GNU General Public License is included in this distribution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * in the file called LICENSE.GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * BSD LICENSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Copyright(c) 2013 OpenVPN Technologies, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * * Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * * Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * notice, this list of conditions and the following disclaimer in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * the documentation and/or other materials provided with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * * Neither the name of OpenVPN Technologies nor the names of its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * contributors may be used to endorse or promote products derived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * from this software without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #ifndef __HAVE_ARCH_CRYPTO_MEMNEQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* Generic path for arbitrary size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static inline unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) __crypto_memneq_generic(const void *a, const void *b, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned long neq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) while (size >= sizeof(unsigned long)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) neq |= *(unsigned long *)a ^ *(unsigned long *)b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) a += sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) b += sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) size -= sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #endif /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) while (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) neq |= *(unsigned char *)a ^ *(unsigned char *)b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) a += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) b += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) size -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return neq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* Loop-free fast-path for frequently used 16-byte size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static inline unsigned long __crypto_memneq_16(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned long neq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (sizeof(unsigned long) == 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) neq |= *(unsigned long *)(a) ^ *(unsigned long *)(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) neq |= *(unsigned long *)(a+8) ^ *(unsigned long *)(b+8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } else if (sizeof(unsigned int) == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) neq |= *(unsigned int *)(a) ^ *(unsigned int *)(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) neq |= *(unsigned int *)(a+4) ^ *(unsigned int *)(b+4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) neq |= *(unsigned int *)(a+8) ^ *(unsigned int *)(b+8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) neq |= *(unsigned int *)(a+12) ^ *(unsigned int *)(b+12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #endif /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) neq |= *(unsigned char *)(a) ^ *(unsigned char *)(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) neq |= *(unsigned char *)(a+1) ^ *(unsigned char *)(b+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) neq |= *(unsigned char *)(a+2) ^ *(unsigned char *)(b+2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) neq |= *(unsigned char *)(a+3) ^ *(unsigned char *)(b+3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) neq |= *(unsigned char *)(a+4) ^ *(unsigned char *)(b+4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) neq |= *(unsigned char *)(a+5) ^ *(unsigned char *)(b+5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) neq |= *(unsigned char *)(a+6) ^ *(unsigned char *)(b+6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) neq |= *(unsigned char *)(a+7) ^ *(unsigned char *)(b+7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) neq |= *(unsigned char *)(a+8) ^ *(unsigned char *)(b+8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) neq |= *(unsigned char *)(a+9) ^ *(unsigned char *)(b+9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) neq |= *(unsigned char *)(a+10) ^ *(unsigned char *)(b+10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) neq |= *(unsigned char *)(a+11) ^ *(unsigned char *)(b+11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) neq |= *(unsigned char *)(a+12) ^ *(unsigned char *)(b+12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) neq |= *(unsigned char *)(a+13) ^ *(unsigned char *)(b+13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) neq |= *(unsigned char *)(a+14) ^ *(unsigned char *)(b+14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) neq |= *(unsigned char *)(a+15) ^ *(unsigned char *)(b+15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) OPTIMIZER_HIDE_VAR(neq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return neq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Compare two areas of memory without leaking timing information,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * and with special optimizations for common sizes. Users should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * not call this function directly, but should instead use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * crypto_memneq defined in crypto/algapi.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) noinline unsigned long __crypto_memneq(const void *a, const void *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) switch (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return __crypto_memneq_16(a, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return __crypto_memneq_generic(a, b, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) EXPORT_SYMBOL(__crypto_memneq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #endif /* __HAVE_ARCH_CRYPTO_MEMNEQ */