memneq.c 6.4 KB

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