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.
 
 
 
 
 

161 lines
4.2 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. static const long NTESTS = 10;
  19. const char *undef_str = "Valgrind thinks this string is undefined.";
  20. const Block undef_block(undef_str);
  21. static inline void ignore(decaf_error_t x) {
  22. (void)x;
  23. }
  24. template<typename Group> struct Tests {
  25. typedef typename Group::Scalar Scalar;
  26. typedef typename Group::Point Point;
  27. typedef typename Group::Precomputed Precomputed;
  28. static void test_arithmetic() {
  29. SpongeRng rng(Block("test_arithmetic"),SpongeRng::DETERMINISTIC);
  30. rng.stir(undef_block);
  31. Scalar x(rng),y(rng),z;
  32. uint8_t ser[Group::Scalar::SER_BYTES];
  33. for (int i=0; i<NTESTS; i++) {
  34. (void)(x+y);
  35. (void)(x-y);
  36. (void)(x*y);
  37. ignore(x.inverse_noexcept(y));
  38. (void)(x==y);
  39. (void)(z=y);
  40. x.serialize_into(ser);
  41. x = y;
  42. }
  43. }
  44. static void test_elligator() {
  45. SpongeRng rng(Block("test_elligator"),SpongeRng::DETERMINISTIC);
  46. rng.stir(undef_block);
  47. FixedArrayBuffer<Group::Point::HASH_BYTES> inv;
  48. for (int i=0; i<NTESTS; i++) {
  49. Point x(rng), y(rng,false);
  50. ignore((x+y).invert_elligator(inv,i));
  51. }
  52. }
  53. static void test_ec() {
  54. SpongeRng rng(Block("test_ec"),SpongeRng::DETERMINISTIC);
  55. rng.stir(undef_block);
  56. uint8_t ser[Group::Point::SER_BYTES];
  57. for (int i=0; i<NTESTS; i++) {
  58. Scalar y(rng),z(rng);
  59. Point p(rng),q(rng),r;
  60. p.serialize_into(ser);
  61. ignore(Group::Point::decode(p,FixedBlock<Group::Point::SER_BYTES>(ser)));
  62. (void)(p*y);
  63. (void)(p+q);
  64. (void)(p-q);
  65. (void)(-p);
  66. (void)(p.times_two());
  67. (void)(p==q);
  68. (void)(p.debugging_torque());
  69. /* (void)(p.non_secret_combo_with_base(y,z)); */ /* Should fail */
  70. (void)(Precomputed(p)*y);
  71. p.dual_scalarmul(q,r,y,z);
  72. Group::Point::double_scalarmul(p,y,q,z);
  73. }
  74. }
  75. static void test_cfrg() {
  76. SpongeRng rng(Block("test_cfrg"),SpongeRng::DETERMINISTIC);
  77. rng.stir(undef_block);
  78. for (int i=0; i<NTESTS; i++) {
  79. FixedArrayBuffer<Group::DhLadder::PUBLIC_BYTES> pub(rng);
  80. FixedArrayBuffer<Group::DhLadder::PRIVATE_BYTES> priv(rng);
  81. Group::DhLadder::generate_key(priv);
  82. ignore(Group::DhLadder::shared_secret_noexcept(pub,pub,priv));
  83. }
  84. }
  85. /* Specify the same value as you did when compiling decaf_crypto.c */
  86. #ifndef DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  87. #define DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT DECAF_FALSE
  88. #endif
  89. static void test_crypto() {
  90. SpongeRng rng(Block("test_crypto"),SpongeRng::DETERMINISTIC);
  91. rng.stir(undef_block);
  92. #if DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  93. SpongeRng defrng(Block("test_crypto_defined"));
  94. #endif
  95. FixedArrayBuffer<Group::Point::SER_BYTES> shared;
  96. for (int i=0; i<NTESTS; i++) {
  97. PrivateKey<Group> sk1(rng);
  98. SecureBuffer sig = sk1.sign(undef_block);
  99. #if DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  100. PrivateKey<Group> sk2(defrng);
  101. (void)sk1.sharedSecretNoexcept(shared,sk2.pub(),i&1);
  102. #else
  103. PrivateKey<Group> sk3(rng);
  104. (void)sk1.sharedSecretNoexcept(shared,sk3.pub(),i&1);
  105. #endif
  106. }
  107. }
  108. }; /* template<GroupId GROUP> */
  109. int main(int argc, char **argv) {
  110. (void) argc; (void) argv;
  111. VALGRIND_MAKE_MEM_UNDEFINED(undef_str, strlen(undef_str));
  112. printf("Testing %s:\n",IsoEd25519::name());
  113. Tests<IsoEd25519>::test_arithmetic();
  114. Tests<IsoEd25519>::test_elligator();
  115. Tests<IsoEd25519>::test_ec();
  116. Tests<IsoEd25519>::test_cfrg();
  117. Tests<IsoEd25519>::test_crypto();
  118. printf("\n");
  119. printf("Testing %s:\n", Ed448Goldilocks::name());
  120. Tests<Ed448Goldilocks>::test_arithmetic();
  121. Tests<Ed448Goldilocks>::test_elligator();
  122. Tests<Ed448Goldilocks>::test_ec();
  123. Tests<Ed448Goldilocks>::test_cfrg();
  124. Tests<Ed448Goldilocks>::test_crypto();
  125. return 0;
  126. }