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.
 
 
 
 
 

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