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.
 
 
 
 
 

388 lines
12 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/shake.hxx>
  13. #include <decaf/crypto.h>
  14. #include <decaf/crypto.hxx>
  15. #include <stdio.h>
  16. using namespace decaf;
  17. static bool passing = true;
  18. static const long NTESTS = 10000;
  19. class Test {
  20. public:
  21. bool passing_now;
  22. Test(const char *test) {
  23. passing_now = true;
  24. printf("%s...", test);
  25. if (strlen(test) < 27) printf("%*s",int(27-strlen(test)),"");
  26. fflush(stdout);
  27. }
  28. ~Test() {
  29. if (std::uncaught_exception()) {
  30. fail();
  31. printf(" due to uncaught exception.\n");
  32. }
  33. if (passing_now) printf("[PASS]\n");
  34. }
  35. void fail() {
  36. if (!passing_now) return;
  37. passing_now = passing = false;
  38. printf("[FAIL]\n");
  39. }
  40. };
  41. template<typename Group> struct Tests {
  42. typedef typename Group::Scalar Scalar;
  43. typedef typename Group::Point Point;
  44. typedef typename Group::Precomputed Precomputed;
  45. static void print(const char *name, const Scalar &x) {
  46. unsigned char buffer[Scalar::SER_BYTES];
  47. x.serializeInto(buffer);
  48. printf(" %s = 0x", name);
  49. for (int i=sizeof(buffer)-1; i>=0; i--) {
  50. printf("%02x", buffer[i]);
  51. }
  52. printf("\n");
  53. }
  54. static void hexprint(const char *name, const SecureBuffer &buffer) {
  55. printf(" %s = 0x", name);
  56. for (int i=buffer.size()-1; i>=0; i--) {
  57. printf("%02x", buffer[i]);
  58. }
  59. printf("\n");
  60. }
  61. static void print(const char *name, const Point &x) {
  62. unsigned char buffer[Point::SER_BYTES];
  63. x.serializeInto(buffer);
  64. printf(" %s = 0x", name);
  65. for (int i=Point::SER_BYTES-1; i>=0; i--) {
  66. printf("%02x", buffer[i]);
  67. }
  68. printf("\n");
  69. }
  70. static bool arith_check(
  71. Test &test,
  72. const Scalar &x,
  73. const Scalar &y,
  74. const Scalar &z,
  75. const Scalar &l,
  76. const Scalar &r,
  77. const char *name
  78. ) {
  79. if (l == r) return true;
  80. test.fail();
  81. printf(" %s", name);
  82. print("x", x);
  83. print("y", y);
  84. print("z", z);
  85. print("lhs", l);
  86. print("rhs", r);
  87. return false;
  88. }
  89. static bool point_check(
  90. Test &test,
  91. const Point &p,
  92. const Point &q,
  93. const Point &R,
  94. const Scalar &x,
  95. const Scalar &y,
  96. const Point &l,
  97. const Point &r,
  98. const char *name
  99. ) {
  100. bool good = l==r;
  101. if (!p.validate()) { good = false; printf(" p invalid\n"); }
  102. if (!q.validate()) { good = false; printf(" q invalid\n"); }
  103. if (!r.validate()) { good = false; printf(" r invalid\n"); }
  104. if (!l.validate()) { good = false; printf(" l invalid\n"); }
  105. if (good) return true;
  106. test.fail();
  107. printf(" %s", name);
  108. print("x", x);
  109. print("y", y);
  110. print("p", p);
  111. print("q", q);
  112. print("r", R);
  113. print("lhs", r);
  114. print("rhs", l);
  115. return false;
  116. }
  117. static void test_arithmetic() {
  118. SpongeRng rng(Block("test_arithmetic"));
  119. Test test("Arithmetic");
  120. Scalar x(0),y(0),z(0);
  121. arith_check(test,x,y,z,INT_MAX,(decaf_word_t)INT_MAX,"cast from max");
  122. arith_check(test,x,y,z,INT_MIN,-Scalar(1+(decaf_word_t)INT_MAX),"cast from min");
  123. for (int i=0; i<NTESTS*10 && test.passing_now; i++) {
  124. /* TODO: pathological cases */
  125. size_t sob = DECAF_255_SCALAR_BYTES + 8 - (i%16);
  126. Scalar x(rng.read(sob));
  127. Scalar y(rng.read(sob));
  128. Scalar z(rng.read(sob));
  129. arith_check(test,x,y,z,x+y,y+x,"commute add");
  130. arith_check(test,x,y,z,x,x+0,"ident add");
  131. arith_check(test,x,y,z,x,x-0,"ident sub");
  132. arith_check(test,x,y,z,x+(y+z),(x+y)+z,"assoc add");
  133. arith_check(test,x,y,z,x*(y+z),x*y + x*z,"distributive mul/add");
  134. arith_check(test,x,y,z,x*(y-z),x*y - x*z,"distributive mul/add");
  135. arith_check(test,x,y,z,x*(y*z),(x*y)*z,"assoc mul");
  136. arith_check(test,x,y,z,x*y,y*x,"commute mul");
  137. arith_check(test,x,y,z,x,x*1,"ident mul");
  138. arith_check(test,x,y,z,0,x*0,"mul by 0");
  139. arith_check(test,x,y,z,-x,x*-1,"mul by -1");
  140. arith_check(test,x,y,z,x+x,x*2,"mul by 2");
  141. if (i%20) continue;
  142. if (y!=0) arith_check(test,x,y,z,x*y/y,x,"invert");
  143. // TODO: negative test, but this throws an exception
  144. //arith_check(test,x,y,z,x/0,0,"invert0");
  145. }
  146. }
  147. static void test_elligator() {
  148. SpongeRng rng(Block("test_elligator"));
  149. Test test("Elligator");
  150. const int NHINTS = Group::REMOVED_COFACTOR * 2;
  151. SecureBuffer *alts[NHINTS];
  152. bool successes[NHINTS];
  153. SecureBuffer *alts2[NHINTS];
  154. bool successes2[NHINTS];
  155. for (int i=0; i<NTESTS/10 && (test.passing_now || i < 100); i++) {
  156. size_t len = (i % (2*Point::HASH_BYTES + 3));
  157. SecureBuffer b1(len);
  158. if (i!=Point::HASH_BYTES) rng.read(b1); /* special test case */
  159. if (i==1) b1[0] = 1; /* special case test */
  160. if (len >= Point::HASH_BYTES) b1[Point::HASH_BYTES-1] &= 0x7F; // FIXME MAGIC
  161. Point s = Point::from_hash(b1), ss=s;
  162. for (int j=0; j<(i&3); j++) ss = ss.debugging_torque();
  163. ss = ss.debugging_pscale(rng);
  164. bool good = false;
  165. for (int j=0; j<NHINTS; j++) {
  166. alts[j] = new SecureBuffer(len);
  167. alts2[j] = new SecureBuffer(len);
  168. if (len > Point::HASH_BYTES)
  169. memcpy(&(*alts[j])[Point::HASH_BYTES], &b1[Point::HASH_BYTES], len-Point::HASH_BYTES);
  170. if (len > Point::HASH_BYTES)
  171. memcpy(&(*alts2[j])[Point::HASH_BYTES], &b1[Point::HASH_BYTES], len-Point::HASH_BYTES);
  172. successes[j] = s.invert_elligator(*alts[j], j);
  173. successes2[j] = ss.invert_elligator(*alts2[j],j);
  174. if (successes[j] != successes2[j]
  175. || (successes[j] && successes2[j] && *alts[j] != *alts2[j])
  176. ) {
  177. test.fail();
  178. printf(" Unscalable Elligator inversion: i=%d, hint=%d, s=%d,%d\n",i,j,
  179. -int(successes[j]),-int(successes2[j]));
  180. hexprint("x",b1);
  181. hexprint("X",*alts[j]);
  182. hexprint("X",*alts2[j]);
  183. }
  184. if (successes[j]) {
  185. good = good || (b1 == *alts[j]);
  186. for (int k=0; k<j; k++) {
  187. if (successes[k] && *alts[j] == *alts[k]) {
  188. test.fail();
  189. printf(" Duplicate Elligator inversion: i=%d, hints=%d, %d\n",i,j,k);
  190. hexprint("x",b1);
  191. hexprint("X",*alts[j]);
  192. }
  193. }
  194. if (s != Point::from_hash(*alts[j])) {
  195. test.fail();
  196. printf(" Fail Elligator inversion round-trip: i=%d, hint=%d %s\n",i,j,
  197. (s==-Point::from_hash(*alts[j])) ? "[output was -input]": "");
  198. hexprint("x",b1);
  199. hexprint("X",*alts[j]);
  200. }
  201. /*
  202. if (i == Point::HASH_BYTES) {
  203. printf("Identity, hint = %d\n", j);
  204. hexprint("einv(0)",*alts[j]);
  205. }
  206. */
  207. }
  208. }
  209. if (!good) {
  210. test.fail();
  211. printf(" %s Elligator inversion: i=%d\n",good ? "Passed" : "Failed", i);
  212. hexprint("B", b1);
  213. for (int j=0; j<NHINTS; j++) {
  214. printf(" %d: %s%s", j, successes[j] ? "succ" : "fail\n", (successes[j] && *alts[j] == b1) ? " [x]" : "");
  215. if (successes[j]) {
  216. hexprint("b", *alts[j]);
  217. }
  218. }
  219. printf("\n");
  220. }
  221. for (int j=0; j<NHINTS; j++) {
  222. delete alts[j];
  223. alts[j] = NULL;
  224. delete alts2[j];
  225. alts2[j] = NULL;
  226. }
  227. Point t(rng);
  228. point_check(test,t,t,t,0,0,t,Point::from_hash(t.steg_encode(rng)),"steg round-trip");
  229. }
  230. }
  231. static void test_ec() {
  232. SpongeRng rng(Block("test_ec"));
  233. Test test("EC");
  234. Point id = Point::identity(), base = Point::base();
  235. point_check(test,id,id,id,0,0,Point::from_hash(""),id,"fh0");
  236. //point_check(test,id,id,id,0,0,Point::from_hash("\x01"),id,"fh1"); FIXME
  237. for (int i=0; i<NTESTS && test.passing_now; i++) {
  238. /* TODO: pathological cases */
  239. Scalar x(rng);
  240. Scalar y(rng);
  241. Point p(rng);
  242. Point q(rng);
  243. SecureBuffer buffer(2*Point::HASH_BYTES);
  244. rng.read(buffer);
  245. Point r = Point::from_hash(buffer);
  246. point_check(test,p,q,r,0,0,p,Point(p.serialize()),"round-trip");
  247. Point pp = p.debugging_torque().debugging_pscale(rng);
  248. if (!memeq(pp.serialize(),p.serialize())) {
  249. test.fail();
  250. printf("Fail torque seq test\n");
  251. }
  252. point_check(test,p,q,r,0,0,p,pp,"torque eq");
  253. point_check(test,p,q,r,0,0,p+q,q+p,"commute add");
  254. point_check(test,p,q,r,0,0,(p-q)+q,p,"correct sub");
  255. point_check(test,p,q,r,0,0,p+(q+r),(p+q)+r,"assoc add");
  256. point_check(test,p,q,r,0,0,p.times_two(),p+p,"dbl add");
  257. if (i%10) continue;
  258. point_check(test,p,q,r,x,0,x*(p+q),x*p+x*q,"distr mul");
  259. point_check(test,p,q,r,x,y,(x*y)*p,x*(y*p),"assoc mul");
  260. point_check(test,p,q,r,x,y,x*p+y*q,Point::double_scalarmul(x,p,y,q),"ds mul");
  261. point_check(test,base,q,r,x,y,x*base+y*q,q.non_secret_combo_with_base(y,x),"ds vt mul");
  262. point_check(test,p,q,r,x,0,Precomputed(p)*x,p*x,"precomp mul");
  263. point_check(test,p,q,r,0,0,r,
  264. Point::from_hash(Buffer(buffer).slice(0,Point::HASH_BYTES))
  265. + Point::from_hash(Buffer(buffer).slice(Point::HASH_BYTES,Point::HASH_BYTES)),
  266. "unih = hash+add"
  267. );
  268. point_check(test,p,q,r,x,0,Point(x.direct_scalarmul(p.serialize())),x*p,"direct mul");
  269. }
  270. }
  271. static void test_crypto() {
  272. Test test("Sample crypto");
  273. SpongeRng rng(Block("test_decaf_crypto"));
  274. for (int i=0; i<NTESTS && test.passing_now; i++) {
  275. PrivateKey<Group> priv1(rng), priv2(rng);
  276. PublicKey<Group> pub1(priv1), pub2(priv2);
  277. SecureBuffer message = rng.read(i);
  278. SecureBuffer sig(priv1.sign(message));
  279. pub1.verify(message, sig);
  280. }
  281. }
  282. }; // template<GroupId GROUP>
  283. // FIXME cross-field
  284. static void test_decaf() {
  285. Test test("Sample crypto");
  286. SpongeRng rng(Block("test_decaf"));
  287. decaf_255_symmetric_key_t proto1,proto2;
  288. decaf_255_private_key_t s1,s2;
  289. decaf_255_public_key_t p1,p2;
  290. decaf_255_signature_t sig;
  291. unsigned char shared1[1234],shared2[1234];
  292. const char *message = "Hello, world!";
  293. for (int i=0; i<NTESTS && test.passing_now; i++) {
  294. rng.read(Buffer(proto1,sizeof(proto1)));
  295. rng.read(Buffer(proto2,sizeof(proto2)));
  296. decaf_255_derive_private_key(s1,proto1);
  297. decaf_255_private_to_public(p1,s1);
  298. decaf_255_derive_private_key(s2,proto2);
  299. decaf_255_private_to_public(p2,s2);
  300. if (!decaf_255_shared_secret (shared1,sizeof(shared1),s1,p2)) {
  301. test.fail(); printf("Fail ss12\n");
  302. }
  303. if (!decaf_255_shared_secret (shared2,sizeof(shared2),s2,p1)) {
  304. test.fail(); printf("Fail ss21\n");
  305. }
  306. if (memcmp(shared1,shared2,sizeof(shared1))) {
  307. test.fail(); printf("Fail ss21 == ss12\n");
  308. }
  309. decaf_255_sign (sig,s1,(const unsigned char *)message,strlen(message));
  310. if (!decaf_255_verify (sig,p1,(const unsigned char *)message,strlen(message))) {
  311. test.fail(); printf("Fail sig ver\n");
  312. }
  313. }
  314. }
  315. int main(int argc, char **argv) {
  316. (void) argc; (void) argv;
  317. printf("Testing %s:\n",IsoEd25519::name());
  318. Tests<IsoEd25519>::test_arithmetic();
  319. Tests<IsoEd25519>::test_elligator();
  320. Tests<IsoEd25519>::test_ec();
  321. Tests<IsoEd25519>::test_crypto();
  322. test_decaf();
  323. printf("\n");
  324. printf("Testing %s:\n", Ed448Goldilocks::name());
  325. Tests<Ed448Goldilocks>::test_arithmetic();
  326. Tests<Ed448Goldilocks>::test_elligator();
  327. Tests<Ed448Goldilocks>::test_ec();
  328. Tests<Ed448Goldilocks>::test_crypto();
  329. if (passing) printf("Passed all tests.\n");
  330. return passing ? 0 : 1;
  331. }