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.
 
 
 
 
 

246 lines
7.3 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 "shake.hxx"
  13. #include "decaf_crypto.h"
  14. #include <stdio.h>
  15. static bool passing = true;
  16. static const long NTESTS = 10000;
  17. class Test {
  18. public:
  19. bool passing_now;
  20. Test(const char *test) {
  21. passing_now = true;
  22. printf("%s...", test);
  23. if (strlen(test) < 27) printf("%*s",int(27-strlen(test)),"");
  24. fflush(stdout);
  25. }
  26. ~Test() {
  27. if (std::uncaught_exception()) {
  28. fail();
  29. printf(" due to uncaught exception.\n");
  30. }
  31. if (passing_now) printf("[PASS]\n");
  32. }
  33. void fail() {
  34. if (!passing_now) return;
  35. passing_now = passing = false;
  36. printf("[FAIL]\n");
  37. }
  38. };
  39. template<typename Group> struct Tests {
  40. typedef typename Group::Scalar Scalar;
  41. typedef typename Group::Point Point;
  42. typedef typename Group::Precomputed Precomputed;
  43. static void print(const char *name, const Scalar &x) {
  44. unsigned char buffer[Scalar::SER_BYTES];
  45. x.encode(buffer);
  46. printf(" %s = 0x", name);
  47. for (int i=sizeof(buffer)-1; i>=0; i--) {
  48. printf("%02x", buffer[i]);
  49. }
  50. printf("\n");
  51. }
  52. static void print(const char *name, const Point &x) {
  53. unsigned char buffer[Point::SER_BYTES];
  54. x.encode(buffer);
  55. printf(" %s = 0x", name);
  56. for (int i=sizeof(buffer)-1; i>=0; i--) {
  57. printf("%02x", buffer[i]);
  58. }
  59. printf("\n");
  60. }
  61. static bool arith_check(
  62. Test &test,
  63. const Scalar &x,
  64. const Scalar &y,
  65. const Scalar &z,
  66. const Scalar &r,
  67. const Scalar &l,
  68. const char *name
  69. ) {
  70. if (l == r) return true;
  71. test.fail();
  72. printf(" %s", name);
  73. print("x", x);
  74. print("y", y);
  75. print("z", z);
  76. print("lhs", r);
  77. print("rhs", l);
  78. return false;
  79. }
  80. static bool point_check(
  81. Test &test,
  82. const Point &p,
  83. const Point &q,
  84. const Point &R,
  85. const Scalar &x,
  86. const Scalar &y,
  87. const Point &l,
  88. const Point &r,
  89. const char *name
  90. ) {
  91. bool good = l==r;
  92. if (!p.validate()) { good = false; printf(" p invalid\n"); }
  93. if (!q.validate()) { good = false; printf(" q invalid\n"); }
  94. if (!r.validate()) { good = false; printf(" r invalid\n"); }
  95. if (!l.validate()) { good = false; printf(" l invalid\n"); }
  96. if (good) return true;
  97. test.fail();
  98. printf(" %s", name);
  99. print("x", x);
  100. print("y", y);
  101. print("p", p);
  102. print("q", q);
  103. print("r", R);
  104. print("lhs", r);
  105. print("rhs", l);
  106. return false;
  107. }
  108. static void test_arithmetic() {
  109. decaf::SpongeRng rng(decaf::Block("test_arithmetic"));
  110. Test test("Arithmetic");
  111. Scalar x(0),y(0),z(0);
  112. arith_check(test,x,y,z,INT_MAX,(decaf_word_t)INT_MAX,"cast from max");
  113. arith_check(test,x,y,z,INT_MIN,-Scalar(1+(decaf_word_t)INT_MAX),"cast from min");
  114. for (int i=0; i<NTESTS*10 && test.passing_now; i++) {
  115. /* TODO: pathological cases */
  116. size_t sob = DECAF_448_SCALAR_BYTES + 8 - (i%16);
  117. Scalar x(rng.read(sob));
  118. Scalar y(rng.read(sob));
  119. Scalar z(rng.read(sob));
  120. arith_check(test,x,y,z,x+y,y+x,"commute add");
  121. arith_check(test,x,y,z,x,x+0,"ident add");
  122. arith_check(test,x,y,z,x,x-0,"ident sub");
  123. arith_check(test,x,y,z,x+(y+z),(x+y)+z,"assoc add");
  124. arith_check(test,x,y,z,x*(y+z),x*y + x*z,"distributive mul/add");
  125. arith_check(test,x,y,z,x*(y-z),x*y - x*z,"distributive mul/add");
  126. arith_check(test,x,y,z,x*(y*z),(x*y)*z,"assoc mul");
  127. arith_check(test,x,y,z,x*y,y*x,"commute mul");
  128. arith_check(test,x,y,z,x,x*1,"ident mul");
  129. arith_check(test,x,y,z,0,x*0,"mul by 0");
  130. arith_check(test,x,y,z,-x,x*-1,"mul by -1");
  131. arith_check(test,x,y,z,x+x,x*2,"mul by 2");
  132. if (i%20) continue;
  133. if (y!=0) arith_check(test,x,y,z,x*y/y,x,"invert");
  134. arith_check(test,x,y,z,x/0,0,"invert0");
  135. }
  136. }
  137. static void test_ec() {
  138. decaf::SpongeRng rng(decaf::Block("test_ec"));
  139. Test test("EC");
  140. Point id = Point::identity(), base = Point::base();
  141. point_check(test,id,id,id,0,0,Point::from_hash(""),id,"fh0");
  142. point_check(test,id,id,id,0,0,Point::from_hash("\x01"),id,"fh1");
  143. for (int i=0; i<NTESTS && test.passing_now; i++) {
  144. /* TODO: pathological cases */
  145. Scalar x(rng);
  146. Scalar y(rng);
  147. Point p(rng);
  148. Point q(rng);
  149. decaf::SecureBuffer buffer(2*Point::HASH_BYTES);
  150. rng.read(buffer);
  151. Point r = Point::from_hash(buffer);
  152. point_check(test,p,q,r,0,0,p,Point((decaf::SecureBuffer)p),"round-trip");
  153. point_check(test,p,q,r,0,0,p+q,q+p,"commute add");
  154. point_check(test,p,q,r,0,0,p+(q+r),(p+q)+r,"assoc add");
  155. point_check(test,p,q,r,0,0,p.times_two(),p+p,"dbl add");
  156. if (i%10) continue;
  157. point_check(test,p,q,r,x,0,x*(p+q),x*p+x*q,"distr mul");
  158. point_check(test,p,q,r,x,y,(x*y)*p,x*(y*p),"assoc mul");
  159. point_check(test,p,q,r,x,y,x*p+y*q,Point::double_scalarmul(x,p,y,q),"ds mul");
  160. point_check(test,base,q,r,x,y,x*base+y*q,q.non_secret_combo_with_base(y,x),"ds vt mul");
  161. point_check(test,p,q,r,x,0,Precomputed(p)*x,p*x,"precomp mul");
  162. point_check(test,p,q,r,0,0,r,
  163. Point::from_hash(buffer.slice(0,Point::HASH_BYTES))
  164. + Point::from_hash(buffer.slice(Point::HASH_BYTES,Point::HASH_BYTES)),
  165. "unih = hash+add"
  166. );
  167. point_check(test,p,q,r,x,0,Point(x.direct_scalarmul(decaf::SecureBuffer(p))),x*p,"direct mul");
  168. }
  169. }
  170. }; // template<decaf::GroupId GROUP>
  171. static void test_decaf() {
  172. Test test("Sample crypto");
  173. decaf::SpongeRng rng(decaf::Block("test_decaf"));
  174. decaf_448_symmetric_key_t proto1,proto2;
  175. decaf_448_private_key_t s1,s2;
  176. decaf_448_public_key_t p1,p2;
  177. decaf_448_signature_t sig;
  178. unsigned char shared1[1234],shared2[1234];
  179. const char *message = "Hello, world!";
  180. for (int i=0; i<NTESTS && test.passing_now; i++) {
  181. rng.read(decaf::TmpBuffer(proto1,sizeof(proto1)));
  182. rng.read(decaf::TmpBuffer(proto2,sizeof(proto2)));
  183. decaf_448_derive_private_key(s1,proto1);
  184. decaf_448_private_to_public(p1,s1);
  185. decaf_448_derive_private_key(s2,proto2);
  186. decaf_448_private_to_public(p2,s2);
  187. if (!decaf_448_shared_secret (shared1,sizeof(shared1),s1,p2)) {
  188. test.fail(); printf("Fail ss12\n");
  189. }
  190. if (!decaf_448_shared_secret (shared2,sizeof(shared2),s2,p1)) {
  191. test.fail(); printf("Fail ss21\n");
  192. }
  193. if (memcmp(shared1,shared2,sizeof(shared1))) {
  194. test.fail(); printf("Fail ss21 == ss12\n");
  195. }
  196. decaf_448_sign (sig,s1,(const unsigned char *)message,strlen(message));
  197. if (!decaf_448_verify (sig,p1,(const unsigned char *)message,strlen(message))) {
  198. test.fail(); printf("Fail sig ver\n");
  199. }
  200. }
  201. }
  202. int main(int argc, char **argv) {
  203. (void) argc; (void) argv;
  204. Tests<decaf::Ed448>::test_arithmetic();
  205. Tests<decaf::Ed448>::test_ec();
  206. test_decaf();
  207. if (passing) printf("Passed all tests.\n");
  208. return passing ? 0 : 1;
  209. }