test_crypto_ope.c 4.4 KB

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