constant_time_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Utilities for constant-time cryptography.
  3. *
  4. * Author: Emilia Kasper ([email protected])
  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 ([email protected])"
  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 ([email protected])"
  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. int bssl_constant_time_test_main(void);
  47. static int test_binary_op_w(crypto_word (*op)(crypto_word a, crypto_word b),
  48. crypto_word a, crypto_word b, int is_true) {
  49. crypto_word c = op(a, b);
  50. if (is_true && c != CONSTTIME_TRUE_W) {
  51. return 1;
  52. } else if (!is_true && c != CONSTTIME_FALSE_W) {
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. static int test_is_zero_w(crypto_word a) {
  58. crypto_word c = constant_time_is_zero_w(a);
  59. if (a == 0 && c != CONSTTIME_TRUE_W) {
  60. return 1;
  61. } else if (a != 0 && c != CONSTTIME_FALSE_W) {
  62. return 1;
  63. }
  64. c = constant_time_is_nonzero_w(a);
  65. if (a == 0 && c != CONSTTIME_FALSE_W) {
  66. return 1;
  67. } else if (a != 0 && c != CONSTTIME_TRUE_W) {
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. static int test_select_w(crypto_word a, crypto_word b) {
  73. crypto_word selected = constant_time_select_w(CONSTTIME_TRUE_W, a, b);
  74. if (selected != a) {
  75. return 1;
  76. }
  77. selected = constant_time_select_w(CONSTTIME_FALSE_W, a, b);
  78. if (selected != b) {
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. static crypto_word test_values_s[] = {
  84. 0,
  85. 1,
  86. 1024,
  87. 12345,
  88. 32000,
  89. #if defined(OPENSSL_64_BIT)
  90. 0xffffffff / 2 - 1,
  91. 0xffffffff / 2,
  92. 0xffffffff / 2 + 1,
  93. 0xffffffff - 1,
  94. 0xffffffff,
  95. #endif
  96. SIZE_MAX / 2 - 1,
  97. SIZE_MAX / 2,
  98. SIZE_MAX / 2 + 1,
  99. SIZE_MAX - 1,
  100. SIZE_MAX
  101. };
  102. int bssl_constant_time_test_main(void) {
  103. int num_failed = 0;
  104. for (size_t i = 0;
  105. i < sizeof(test_values_s) / sizeof(test_values_s[0]); ++i) {
  106. crypto_word a = test_values_s[i];
  107. num_failed += test_is_zero_w(a);
  108. for (size_t j = 0;
  109. j < sizeof(test_values_s) / sizeof(test_values_s[0]); ++j) {
  110. crypto_word b = test_values_s[j];
  111. num_failed += test_binary_op_w(&constant_time_eq_w, a, b, a == b);
  112. num_failed += test_binary_op_w(&constant_time_eq_w, b, a, b == a);
  113. num_failed += test_select_w(a, b);
  114. }
  115. }
  116. return num_failed == 0;
  117. }