constant_time_test.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Utilities for constant-time cryptography.
  3. *
  4. * Author: Emilia Kasper (emilia@openssl.org)
  5. * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
  6. * (Google).
  7. * ====================================================================
  8. * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. All advertising materials mentioning features or use of this software
  19. * must display the following acknowledgement:
  20. * "This product includes cryptographic software written by
  21. * Eric Young (eay@cryptsoft.com)"
  22. * The word 'cryptographic' can be left out if the rouines from the library
  23. * being used are not cryptographic related :-).
  24. * 4. If you include any Windows specific code (or a derivative thereof) from
  25. * the apps directory (application code) you must include an acknowledgement:
  26. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. *
  40. * The licence and distribution terms for any publically available version or
  41. * derivative of this code cannot be changed. i.e. this code cannot simply be
  42. * copied and put under another distribution licence
  43. * [including the GNU Public Licence.]
  44. */
  45. #include "internal.h"
  46. #include <limits.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <limits>
  50. #include <gtest/gtest.h>
  51. #include <openssl/mem.h>
  52. #include <openssl/rand.h>
  53. static uint8_t FromBool8(bool b) {
  54. return b ? CONSTTIME_TRUE_8 : CONSTTIME_FALSE_8;
  55. }
  56. static crypto_word_t FromBoolW(bool b) {
  57. return b ? CONSTTIME_TRUE_W : CONSTTIME_FALSE_W;
  58. }
  59. static const uint8_t test_values_8[] = {0, 1, 2, 20, 32, 127, 128, 129, 255};
  60. static crypto_word_t test_values_w[] = {
  61. 0,
  62. 1,
  63. 1024,
  64. 12345,
  65. 32000,
  66. #if defined(OPENSSL_64_BIT)
  67. 0xffffffff / 2 - 1,
  68. 0xffffffff / 2,
  69. 0xffffffff / 2 + 1,
  70. 0xffffffff - 1,
  71. 0xffffffff,
  72. #endif
  73. std::numeric_limits<crypto_word_t>::max() / 2 - 1,
  74. std::numeric_limits<crypto_word_t>::max() / 2,
  75. std::numeric_limits<crypto_word_t>::max() / 2 + 1,
  76. std::numeric_limits<crypto_word_t>::max() - 1,
  77. std::numeric_limits<crypto_word_t>::max(),
  78. };
  79. static int signed_test_values[] = {
  80. 0, 1, -1, 1024, -1024, 12345, -12345,
  81. 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1, INT_MIN + 1};
  82. TEST(ConstantTimeTest, Test) {
  83. for (crypto_word_t a : test_values_w) {
  84. SCOPED_TRACE(a);
  85. EXPECT_EQ(FromBoolW(a == 0), constant_time_is_zero_w(a));
  86. EXPECT_EQ(FromBool8(a == 0), constant_time_is_zero_8(a));
  87. for (crypto_word_t b : test_values_w) {
  88. SCOPED_TRACE(b);
  89. EXPECT_EQ(FromBoolW(a < b), constant_time_lt_w(a, b));
  90. EXPECT_EQ(FromBool8(a < b), constant_time_lt_8(a, b));
  91. EXPECT_EQ(FromBoolW(a >= b), constant_time_ge_w(a, b));
  92. EXPECT_EQ(FromBool8(a >= b), constant_time_ge_8(a, b));
  93. EXPECT_EQ(FromBoolW(a == b), constant_time_eq_w(a, b));
  94. EXPECT_EQ(FromBool8(a == b), constant_time_eq_8(a, b));
  95. EXPECT_EQ(a, constant_time_select_w(CONSTTIME_TRUE_W, a, b));
  96. EXPECT_EQ(b, constant_time_select_w(CONSTTIME_FALSE_W, a, b));
  97. }
  98. }
  99. for (int a : signed_test_values) {
  100. SCOPED_TRACE(a);
  101. for (int b : signed_test_values) {
  102. SCOPED_TRACE(b);
  103. EXPECT_EQ(a, constant_time_select_int(CONSTTIME_TRUE_W, a, b));
  104. EXPECT_EQ(b, constant_time_select_int(CONSTTIME_FALSE_W, a, b));
  105. EXPECT_EQ(FromBoolW(a == b), constant_time_eq_int(a, b));
  106. EXPECT_EQ(FromBool8(a == b), constant_time_eq_int_8(a, b));
  107. }
  108. }
  109. for (uint8_t a : test_values_8) {
  110. SCOPED_TRACE(static_cast<int>(a));
  111. for (uint8_t b : test_values_8) {
  112. SCOPED_TRACE(static_cast<int>(b));
  113. EXPECT_EQ(a, constant_time_select_8(CONSTTIME_TRUE_8, a, b));
  114. EXPECT_EQ(b, constant_time_select_8(CONSTTIME_FALSE_8, a, b));
  115. }
  116. }
  117. }
  118. TEST(ConstantTimeTest, MemCmp) {
  119. uint8_t buf[256], copy[256];
  120. RAND_bytes(buf, sizeof(buf));
  121. OPENSSL_memcpy(copy, buf, sizeof(buf));
  122. EXPECT_EQ(0, CRYPTO_memcmp(buf, copy, sizeof(buf)));
  123. for (size_t i = 0; i < sizeof(buf); i++) {
  124. for (uint8_t bit = 1; bit != 0; bit <<= 1) {
  125. OPENSSL_memcpy(copy, buf, sizeof(buf));
  126. copy[i] ^= bit;
  127. EXPECT_NE(0, CRYPTO_memcmp(buf, copy, sizeof(buf)));
  128. }
  129. }
  130. }
  131. TEST(ConstantTimeTest, ValueBarrier) {
  132. for (int i = 0; i < 10; i++) {
  133. crypto_word_t word;
  134. RAND_bytes(reinterpret_cast<uint8_t *>(&word), sizeof(word));
  135. EXPECT_EQ(word, value_barrier_w(word));
  136. uint32_t u32;
  137. RAND_bytes(reinterpret_cast<uint8_t *>(&u32), sizeof(u32));
  138. EXPECT_EQ(u32, value_barrier_u32(u32));
  139. uint64_t u64;
  140. RAND_bytes(reinterpret_cast<uint8_t *>(&u64), sizeof(u64));
  141. EXPECT_EQ(u64, value_barrier_u64(u64));
  142. }
  143. }