You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

156 lines
4.0 KiB

  1. /**
  2. * @file test_decaf.cxx
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @brief C++ tests, because that's easier.
  10. */
  11. #include <decaf.hxx>
  12. #include <decaf/spongerng.hxx>
  13. #include <decaf/crypto.h>
  14. #include <decaf/crypto.hxx>
  15. #include <stdio.h>
  16. #include <valgrind/memcheck.h>
  17. using namespace decaf;
  18. using namespace decaf::TOY;
  19. static const long NTESTS = 10;
  20. const char *undef_str = "Valgrind thinks this string is undefined.";
  21. const Block undef_block(undef_str);
  22. static inline void ignore_result(decaf_error_t x) {
  23. (void)x;
  24. }
  25. template<typename Group> struct Tests {
  26. typedef typename Group::Scalar Scalar;
  27. typedef typename Group::Point Point;
  28. typedef typename Group::Precomputed Precomputed;
  29. static void test_arithmetic() {
  30. SpongeRng rng(Block("test_arithmetic"),SpongeRng::DETERMINISTIC);
  31. rng.stir(undef_block);
  32. Scalar x(rng),y(rng),z;
  33. uint8_t ser[Group::Scalar::SER_BYTES];
  34. for (int i=0; i<NTESTS; i++) {
  35. (void)(x+y);
  36. (void)(x-y);
  37. (void)(x*y);
  38. ignore_result(x.inverse_noexcept(y));
  39. (void)(x==y);
  40. (void)(z=y);
  41. x.serialize_into(ser);
  42. x = y;
  43. }
  44. }
  45. static void test_elligator() {
  46. SpongeRng rng(Block("test_elligator"),SpongeRng::DETERMINISTIC);
  47. rng.stir(undef_block);
  48. FixedArrayBuffer<Group::Point::HASH_BYTES> inv;
  49. for (int i=0; i<NTESTS; i++) {
  50. Point x(rng), y(rng,false);
  51. ignore_result((x+y).invert_elligator(inv,i));
  52. }
  53. }
  54. static void test_ec() {
  55. SpongeRng rng(Block("test_ec"),SpongeRng::DETERMINISTIC);
  56. rng.stir(undef_block);
  57. uint8_t ser[Group::Point::SER_BYTES];
  58. for (int i=0; i<NTESTS; i++) {
  59. Scalar y(rng),z(rng);
  60. Point p(rng),q(rng),r;
  61. p.serialize_into(ser);
  62. ignore_result(p.decode(FixedBlock<Group::Point::SER_BYTES>(ser)));
  63. (void)(p*y);
  64. (void)(p+q);
  65. (void)(p-q);
  66. (void)(-p);
  67. (void)(p.times_two());
  68. (void)(p==q);
  69. (void)(p.debugging_torque());
  70. /* (void)(p.non_secret_combo_with_base(y,z)); */ /* Should fail */
  71. (void)(Precomputed(p)*y);
  72. p.dual_scalarmul(q,r,y,z);
  73. Group::Point::double_scalarmul(p,y,q,z);
  74. }
  75. }
  76. static void test_cfrg() {
  77. SpongeRng rng(Block("test_cfrg"),SpongeRng::DETERMINISTIC);
  78. rng.stir(undef_block);
  79. for (int i=0; i<NTESTS; i++) {
  80. FixedArrayBuffer<Group::DhLadder::PUBLIC_BYTES> pub(rng);
  81. FixedArrayBuffer<Group::DhLadder::PRIVATE_BYTES> priv(rng);
  82. Group::DhLadder::derive_public_key(priv);
  83. ignore_result(Group::DhLadder::shared_secret_noexcept(pub,pub,priv));
  84. }
  85. }
  86. /* Specify the same value as you did when compiling decaf_crypto.c */
  87. #ifndef DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  88. #define DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT DECAF_FALSE
  89. #endif
  90. static void test_crypto() {
  91. SpongeRng rng(Block("test_crypto"),SpongeRng::DETERMINISTIC);
  92. rng.stir(undef_block);
  93. #if DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  94. SpongeRng defrng(Block("test_crypto_defined"));
  95. #endif
  96. FixedArrayBuffer<Group::Point::SER_BYTES> shared;
  97. for (int i=0; i<NTESTS; i++) {
  98. PrivateKey<Group> sk1(rng);
  99. SecureBuffer sig = sk1.sign(undef_block);
  100. #if DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  101. PrivateKey<Group> sk2(defrng);
  102. ignore_result(sk1.shared_secret_noexcept(shared,sk2.pub(),i&1));
  103. #else
  104. PrivateKey<Group> sk3(rng);
  105. ignore_result(sk1.shared_secret_noexcept(shared,sk3.pub(),i&1));
  106. #endif
  107. }
  108. }
  109. static void run() {
  110. printf("Testing %s:\n",Group::name());
  111. test_arithmetic();
  112. test_elligator();
  113. test_ec();
  114. test_cfrg();
  115. test_crypto();
  116. printf("\n");
  117. }
  118. }; /* template<GroupId GROUP> struct Tests */
  119. int main(int argc, char **argv) {
  120. (void) argc; (void) argv;
  121. VALGRIND_MAKE_MEM_UNDEFINED(undef_str, strlen(undef_str));
  122. run_for_all_curves<Tests>();
  123. return 0;
  124. }