test_crypto_ope.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CRYPTO_OPE_PRIVATE
  7. #include "lib/crypt_ops/crypto_ope.h"
  8. #include "common/util_format.h"
  9. #include "test/test.h"
  10. static void
  11. test_crypto_ope_consistency(void *arg)
  12. {
  13. (void)arg;
  14. crypto_ope_t *ope = NULL;
  15. crypto_cipher_t *aes = NULL;
  16. const int TEST_VALS[] = { 5, 500, 1023, 1024, 1025, 2046, 2047, 2048, 2049,
  17. 10000, OPE_INPUT_MAX };
  18. unsigned i;
  19. const uint8_t key[32] = "A fixed key, chosen arbitrarily.";
  20. ope = crypto_ope_new(key);
  21. tt_assert(ope);
  22. uint64_t last_val = 0;
  23. for (i = 0; i < ARRAY_LENGTH(TEST_VALS); ++i) {
  24. aes = ope_get_cipher(ope, 0);
  25. int val = TEST_VALS[i];
  26. uint64_t v1 = crypto_ope_encrypt(ope, val);
  27. uint64_t v2 = sum_values_from_cipher(aes, val);
  28. tt_u64_op(v1, OP_EQ, v2);
  29. tt_u64_op(v2, OP_GT, last_val);
  30. last_val = v2;
  31. crypto_cipher_free(aes);
  32. }
  33. done:
  34. crypto_cipher_free(aes);
  35. crypto_ope_free(ope);
  36. }
  37. static void
  38. test_crypto_ope_oob(void *arg)
  39. {
  40. (void)arg;
  41. crypto_ope_t *ope = NULL;
  42. const uint8_t key[32] = "A fixed key, chosen arbitrarily.";
  43. ope = crypto_ope_new(key);
  44. tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,INT_MIN));
  45. tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,-100));
  46. tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,0));
  47. tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,1));
  48. tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,7000));
  49. tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,OPE_INPUT_MAX));
  50. tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,OPE_INPUT_MAX+1));
  51. tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,INT_MAX));
  52. done:
  53. crypto_ope_free(ope);
  54. }
  55. static const char OPE_TEST_KEY[] =
  56. "19e05891d55232c08c2cad91d612fdb9cbd6691949a0742434a76c80bc6992fe";
  57. /* generated by a separate python implementation. */
  58. static const struct {
  59. int v;
  60. uint64_t r;
  61. } OPE_TEST_VECTORS[] = {
  62. { 121132, UINT64_C(3971694514) },
  63. { 82283, UINT64_C(2695743564) },
  64. { 72661, UINT64_C(2381548866) },
  65. { 72941, UINT64_C(2390408421) },
  66. { 123122, UINT64_C(4036781069) },
  67. { 12154, UINT64_C(402067100) },
  68. { 121574, UINT64_C(3986197593) },
  69. { 11391, UINT64_C(376696838) },
  70. { 65845, UINT64_C(2161801517) },
  71. { 86301, UINT64_C(2828270975) },
  72. { 61284, UINT64_C(2013616892) },
  73. { 70505, UINT64_C(2313368870) },
  74. { 30438, UINT64_C(1001394664) },
  75. { 60150, UINT64_C(1977329668) },
  76. { 114800, UINT64_C(3764946628) },
  77. { 109403, UINT64_C(3585352477) },
  78. { 21893, UINT64_C(721388468) },
  79. { 123569, UINT64_C(4051780471) },
  80. { 95617, UINT64_C(3134921876) },
  81. { 48561, UINT64_C(1597596985) },
  82. { 53334, UINT64_C(1753691710) },
  83. { 92746, UINT64_C(3040874493) },
  84. { 7110, UINT64_C(234966492) },
  85. { 9612, UINT64_C(318326551) },
  86. { 106958, UINT64_C(3506124249) },
  87. { 46889, UINT64_C(1542219146) },
  88. { 87790, UINT64_C(2877361609) },
  89. { 68878, UINT64_C(2260369112) },
  90. { 47917, UINT64_C(1576681737) },
  91. { 121128, UINT64_C(3971553290) },
  92. { 108602, UINT64_C(3559176081) },
  93. { 28217, UINT64_C(929692460) },
  94. { 69498, UINT64_C(2280554161) },
  95. { 63870, UINT64_C(2098322675) },
  96. { 57542, UINT64_C(1891698992) },
  97. { 122148, UINT64_C(4004515805) },
  98. { 46254, UINT64_C(1521227949) },
  99. { 42850, UINT64_C(1408996941) },
  100. { 92661, UINT64_C(3037901517) },
  101. { 57720, UINT64_C(1897369989) },
  102. };
  103. static void
  104. test_crypto_ope_vectors(void *arg)
  105. {
  106. (void)arg;
  107. uint8_t key[32];
  108. crypto_ope_t *ope = NULL, *ope2 = NULL;
  109. base16_decode((char*)key, 32, OPE_TEST_KEY, strlen(OPE_TEST_KEY));
  110. ope = crypto_ope_new(key);
  111. key[8] += 1;
  112. ope2 = crypto_ope_new(key);
  113. unsigned i;
  114. for (i = 0; i < ARRAY_LENGTH(OPE_TEST_VECTORS); ++i) {
  115. int val = OPE_TEST_VECTORS[i].v;
  116. uint64_t res = OPE_TEST_VECTORS[i].r;
  117. tt_u64_op(crypto_ope_encrypt(ope, val), OP_EQ, res);
  118. tt_u64_op(crypto_ope_encrypt(ope2, val), OP_NE, res);
  119. }
  120. done:
  121. crypto_ope_free(ope);
  122. crypto_ope_free(ope2);
  123. }
  124. struct testcase_t crypto_ope_tests[] = {
  125. { "consistency", test_crypto_ope_consistency, 0, NULL, NULL },
  126. { "oob", test_crypto_ope_oob, 0, NULL, NULL },
  127. { "vectors", test_crypto_ope_vectors, 0, NULL, NULL },
  128. END_OF_TESTCASES
  129. };