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.
 
 
 
 
 

146 lines
3.7 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"));
  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"));
  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"));
  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. /* Specify the same value as you did when compiling decaf_crypto.c */
  76. #ifndef DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  77. #define DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT DECAF_FALSE
  78. #endif
  79. static void test_crypto() {
  80. SpongeRng rng(Block("test_crypto"));
  81. rng.stir(undef_block);
  82. #if DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  83. SpongeRng defrng(Block("test_crypto_defined"));
  84. #endif
  85. FixedArrayBuffer<Group::Point::SER_BYTES> shared;
  86. for (int i=0; i<NTESTS; i++) {
  87. PrivateKey<Group> sk1(rng);
  88. SecureBuffer sig = sk1.sign(undef_block);
  89. #if DECAF_CRYPTO_SHARED_SECRET_SHORT_CIRUIT
  90. PrivateKey<Group> sk2(defrng);
  91. (void)sk1.sharedSecretNoexcept(shared,sk2.pub(),i&1);
  92. #else
  93. PrivateKey<Group> sk3(rng);
  94. (void)sk1.sharedSecretNoexcept(shared,sk3.pub(),i&1);
  95. #endif
  96. }
  97. }
  98. }; /* template<GroupId GROUP> */
  99. int main(int argc, char **argv) {
  100. (void) argc; (void) argv;
  101. VALGRIND_MAKE_MEM_UNDEFINED(undef_str, strlen(undef_str));
  102. printf("Testing %s:\n",IsoEd25519::name());
  103. Tests<IsoEd25519>::test_arithmetic();
  104. Tests<IsoEd25519>::test_elligator();
  105. Tests<IsoEd25519>::test_ec();
  106. Tests<IsoEd25519>::test_crypto();
  107. printf("\n");
  108. printf("Testing %s:\n", Ed448Goldilocks::name());
  109. Tests<Ed448Goldilocks>::test_arithmetic();
  110. Tests<Ed448Goldilocks>::test_elligator();
  111. Tests<Ed448Goldilocks>::test_ec();
  112. Tests<Ed448Goldilocks>::test_crypto();
  113. return 0;
  114. }