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.
 
 
 
 
 

683 lines
22 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/eddsa.hxx>
  14. #include <decaf/shake.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. static uint64_t leint(const SecureBuffer &xx) {
  42. uint64_t out = 0;
  43. for (unsigned int i=0; i<xx.size() && i<sizeof(out); i++) {
  44. out |= uint64_t(xx[i]) << (8*i);
  45. }
  46. return out;
  47. }
  48. template<typename Group> struct Tests {
  49. typedef typename Group::Scalar Scalar;
  50. typedef typename Group::Point Point;
  51. typedef typename Group::DhLadder DhLadder;
  52. typedef typename Group::Precomputed Precomputed;
  53. static void print(const char *name, const Scalar &x) {
  54. unsigned char buffer[Scalar::SER_BYTES];
  55. x.serialize_into(buffer);
  56. printf(" %s = 0x", name);
  57. for (int i=sizeof(buffer)-1; i>=0; i--) {
  58. printf("%02x", buffer[i]);
  59. }
  60. printf("\n");
  61. }
  62. static void hexprint(const char *name, const SecureBuffer &buffer) {
  63. printf(" %s = 0x", name);
  64. for (int i=buffer.size()-1; i>=0; i--) {
  65. printf("%02x", buffer[i]);
  66. }
  67. printf("\n");
  68. }
  69. static void print(const char *name, const Point &x) {
  70. unsigned char buffer[Point::SER_BYTES];
  71. x.serialize_into(buffer);
  72. printf(" %s = 0x", name);
  73. for (int i=Point::SER_BYTES-1; i>=0; i--) {
  74. printf("%02x", buffer[i]);
  75. }
  76. printf("\n");
  77. }
  78. static bool arith_check(
  79. Test &test,
  80. const Scalar &x,
  81. const Scalar &y,
  82. const Scalar &z,
  83. const Scalar &l,
  84. const Scalar &r,
  85. const char *name
  86. ) {
  87. if (l == r) return true;
  88. test.fail();
  89. printf(" %s", name);
  90. print("x", x);
  91. print("y", y);
  92. print("z", z);
  93. print("lhs", l);
  94. print("rhs", r);
  95. return false;
  96. }
  97. static bool point_check(
  98. Test &test,
  99. const Point &p,
  100. const Point &q,
  101. const Point &R,
  102. const Scalar &x,
  103. const Scalar &y,
  104. const Point &l,
  105. const Point &r,
  106. const char *name
  107. ) {
  108. bool good = l==r;
  109. if (!p.validate()) { good = false; printf(" p invalid\n"); }
  110. if (!q.validate()) { good = false; printf(" q invalid\n"); }
  111. if (!r.validate()) { good = false; printf(" r invalid\n"); }
  112. if (!l.validate()) { good = false; printf(" l invalid\n"); }
  113. if (good) return true;
  114. test.fail();
  115. printf(" %s", name);
  116. print("x", x);
  117. print("y", y);
  118. print("p", p);
  119. print("q", q);
  120. print("r", R);
  121. print("lhs", r);
  122. print("rhs", l);
  123. return false;
  124. }
  125. static void test_arithmetic() {
  126. SpongeRng rng(Block("test_arithmetic"),SpongeRng::DETERMINISTIC);
  127. Test test("Arithmetic");
  128. Scalar x(0),y(0),z(0);
  129. arith_check(test,x,y,z,INT_MAX,(decaf_word_t)INT_MAX,"cast from max");
  130. arith_check(test,x,y,z,INT_MIN,-Scalar(1+(decaf_word_t)INT_MAX),"cast from min");
  131. for (int i=0; i<NTESTS*10 && test.passing_now; i++) {
  132. size_t sob = i % (2*Group::Scalar::SER_BYTES);
  133. SecureBuffer xx = rng.read(sob), yy = rng.read(sob), zz = rng.read(sob);
  134. Scalar x(xx);
  135. Scalar y(yy);
  136. Scalar z(zz);
  137. arith_check(test,x,y,z,x+y,y+x,"commute add");
  138. arith_check(test,x,y,z,x,x+0,"ident add");
  139. arith_check(test,x,y,z,x,x-0,"ident sub");
  140. arith_check(test,x,y,z,x+-x,0,"inverse add");
  141. arith_check(test,x,y,z,x-x,0,"inverse sub");
  142. arith_check(test,x,y,z,x-(x+1),-1,"inverse add2");
  143. arith_check(test,x,y,z,x+(y+z),(x+y)+z,"assoc add");
  144. arith_check(test,x,y,z,x*(y+z),x*y + x*z,"distributive mul/add");
  145. arith_check(test,x,y,z,x*(y-z),x*y - x*z,"distributive mul/add");
  146. arith_check(test,x,y,z,x*(y*z),(x*y)*z,"assoc mul");
  147. arith_check(test,x,y,z,x*y,y*x,"commute mul");
  148. arith_check(test,x,y,z,x,x*1,"ident mul");
  149. arith_check(test,x,y,z,0,x*0,"mul by 0");
  150. arith_check(test,x,y,z,-x,x*-1,"mul by -1");
  151. arith_check(test,x,y,z,x+x,x*2,"mul by 2");
  152. arith_check(test,x,y,z,-(x*y),(-x)*y,"neg prop mul");
  153. arith_check(test,x,y,z,x*y,(-x)*(-y),"double neg prop mul");
  154. arith_check(test,x,y,z,-(x+y),(-x)+(-y),"neg prop add");
  155. arith_check(test,x,y,z,x-y,(x)+(-y),"add neg sub");
  156. arith_check(test,x,y,z,(-x)-y,-(x+y),"neg add");
  157. if (sob <= 4) {
  158. uint64_t xi = leint(xx), yi = leint(yy);
  159. arith_check(test,x,y,z,x,xi,"parse consistency");
  160. arith_check(test,x,y,z,x+y,xi+yi,"add consistency");
  161. arith_check(test,x,y,z,x*y,xi*yi,"mul consistency");
  162. }
  163. if (i%20) continue;
  164. if (y!=0) arith_check(test,x,y,z,x*y/y,x,"invert");
  165. try {
  166. y = x/0;
  167. test.fail();
  168. printf(" Inverted zero!");
  169. print("x", x);
  170. print("y", y);
  171. } catch(CryptoException) {}
  172. }
  173. }
  174. static const Block sqrt_minus_one;
  175. static const Block minus_sqrt_minus_one;
  176. static const Block elli_patho; /* sqrt(1/(u(1-d))) */
  177. static void test_elligator() {
  178. SpongeRng rng(Block("test_elligator"),SpongeRng::DETERMINISTIC);
  179. Test test("Elligator");
  180. const int NHINTS = 1<<Point::INVERT_ELLIGATOR_WHICH_BITS;
  181. SecureBuffer *alts[NHINTS];
  182. bool successes[NHINTS];
  183. SecureBuffer *alts2[NHINTS];
  184. bool successes2[NHINTS];
  185. for (unsigned int i=0; i<NTESTS/10 && (i<10 || test.passing_now); i++) {
  186. size_t len = (i % (2*Point::HASH_BYTES + 3));
  187. SecureBuffer b1(len);
  188. if (i!=Point::HASH_BYTES) rng.read(b1); /* special test case */
  189. /* Pathological cases */
  190. if (i==1) b1[0] = 1;
  191. if (i==2 && sqrt_minus_one.size()) b1 = sqrt_minus_one;
  192. if (i==3 && minus_sqrt_minus_one.size()) b1 = minus_sqrt_minus_one;
  193. if (i==4 && elli_patho.size()) b1 = elli_patho;
  194. len = b1.size();
  195. Point s = Point::from_hash(b1), ss=s;
  196. for (unsigned int j=0; j<(i&3); j++) ss = ss.debugging_torque();
  197. ss = ss.debugging_pscale(rng);
  198. bool good = false;
  199. for (int j=0; j<NHINTS; j++) {
  200. alts[j] = new SecureBuffer(len);
  201. alts2[j] = new SecureBuffer(len);
  202. if (len > Point::HASH_BYTES)
  203. memcpy(&(*alts[j])[Point::HASH_BYTES], &b1[Point::HASH_BYTES], len-Point::HASH_BYTES);
  204. if (len > Point::HASH_BYTES)
  205. memcpy(&(*alts2[j])[Point::HASH_BYTES], &b1[Point::HASH_BYTES], len-Point::HASH_BYTES);
  206. successes[j] = decaf_successful( s.invert_elligator(*alts[j], j));
  207. successes2[j] = decaf_successful(ss.invert_elligator(*alts2[j],j));
  208. if (successes[j] != successes2[j]
  209. || (successes[j] && successes2[j] && *alts[j] != *alts2[j])
  210. ) {
  211. test.fail();
  212. printf(" Unscalable Elligator inversion: i=%d, hint=%d, s=%d,%d\n",i,j,
  213. -int(successes[j]),-int(successes2[j]));
  214. hexprint("x",b1);
  215. hexprint("X",*alts[j]);
  216. hexprint("X",*alts2[j]);
  217. }
  218. if (successes[j]) {
  219. good = good || (b1 == *alts[j]);
  220. for (int k=0; k<j; k++) {
  221. if (successes[k] && *alts[j] == *alts[k]) {
  222. test.fail();
  223. printf(" Duplicate Elligator inversion: i=%d, hints=%d, %d\n",i,j,k);
  224. hexprint("x",b1);
  225. hexprint("X",*alts[j]);
  226. }
  227. }
  228. if (s != Point::from_hash(*alts[j])) {
  229. test.fail();
  230. printf(" Fail Elligator inversion round-trip: i=%d, hint=%d %s\n",i,j,
  231. (s==-Point::from_hash(*alts[j])) ? "[output was -input]": "");
  232. hexprint("x",b1);
  233. hexprint("X",*alts[j]);
  234. }
  235. }
  236. }
  237. if (!good) {
  238. test.fail();
  239. printf(" %s Elligator inversion: i=%d\n",good ? "Passed" : "Failed", i);
  240. hexprint("B", b1);
  241. for (int j=0; j<NHINTS; j++) {
  242. printf(" %d: %s%s", j, successes[j] ? "succ" : "fail\n", (successes[j] && *alts[j] == b1) ? " [x]" : "");
  243. if (successes[j]) {
  244. hexprint("b", *alts[j]);
  245. }
  246. }
  247. printf("\n");
  248. }
  249. for (int j=0; j<NHINTS; j++) {
  250. delete alts[j];
  251. alts[j] = NULL;
  252. delete alts2[j];
  253. alts2[j] = NULL;
  254. }
  255. Point t(rng);
  256. point_check(test,t,t,t,0,0,t,Point::from_hash(t.steg_encode(rng)),"steg round-trip");
  257. }
  258. }
  259. static void test_ec() {
  260. SpongeRng rng(Block("test_ec"),SpongeRng::DETERMINISTIC);
  261. Test test("EC");
  262. Point id = Point::identity(), base = Point::base();
  263. point_check(test,id,id,id,0,0,Point::from_hash(""),id,"fh0");
  264. unsigned char enc[Point::SER_BYTES] = {0};
  265. if (Group::FIELD_MODULUS_TYPE == 3) {
  266. /* When p == 3 mod 4, the QNR is -1, so u*1^2 = -1 also produces the
  267. * identity.
  268. */
  269. point_check(test,id,id,id,0,0,Point::from_hash("\x01"),id,"fh1");
  270. }
  271. point_check(test,id,id,id,0,0,Point(FixedBlock<sizeof(enc)>(enc)),id,"decode [0]");
  272. try {
  273. enc[0] = 1;
  274. Point f((FixedBlock<sizeof(enc)>(enc)));
  275. test.fail();
  276. printf(" Allowed deserialize of [1]: %d", f==id);
  277. } catch (CryptoException) {
  278. /* ok */
  279. }
  280. if (sqrt_minus_one.size()) {
  281. try {
  282. Point f(sqrt_minus_one);
  283. test.fail();
  284. printf(" Allowed deserialize of [i]: %d", f==id);
  285. } catch (CryptoException) {
  286. /* ok */
  287. }
  288. }
  289. if (minus_sqrt_minus_one.size()) {
  290. try {
  291. Point f(minus_sqrt_minus_one);
  292. test.fail();
  293. printf(" Allowed deserialize of [-i]: %d", f==id);
  294. } catch (CryptoException) {
  295. /* ok */
  296. }
  297. }
  298. for (int i=0; i<NTESTS && test.passing_now; i++) {
  299. Scalar x(rng);
  300. Scalar y(rng);
  301. Point p(rng);
  302. Point q(rng);
  303. Point d1, d2;
  304. SecureBuffer buffer(2*Point::HASH_BYTES);
  305. rng.read(buffer);
  306. Point r = Point::from_hash(buffer);
  307. try {
  308. point_check(test,p,q,r,0,0,p,Point(p.serialize()),"round-trip");
  309. } catch (CryptoException) {
  310. test.fail();
  311. printf(" Round-trip raised CryptoException!\n");
  312. }
  313. Point pp = p.debugging_torque().debugging_pscale(rng);
  314. if (!memeq(pp.serialize(),p.serialize())) {
  315. test.fail();
  316. printf(" Fail torque seq test\n");
  317. }
  318. if (!memeq((p-pp).serialize(),id.serialize())) {
  319. test.fail();
  320. printf(" Fail torque id test\n");
  321. }
  322. if (!memeq((p-p).serialize(),id.serialize())) {
  323. test.fail();
  324. printf(" Fail id test\n");
  325. }
  326. point_check(test,p,q,r,0,0,p,pp,"torque eq");
  327. point_check(test,p,q,r,0,0,p+q,q+p,"commute add");
  328. point_check(test,p,q,r,0,0,(p-q)+q,p,"correct sub");
  329. point_check(test,p,q,r,0,0,p+(q+r),(p+q)+r,"assoc add");
  330. point_check(test,p,q,r,0,0,p.times_two(),p+p,"dbl add");
  331. if (i%10) continue;
  332. point_check(test,p,q,r,0,0,p.times_two(),p*Scalar(2),"add times two");
  333. point_check(test,p,q,r,x,0,x*(p+q),x*p+x*q,"distr mul");
  334. point_check(test,p,q,r,x,y,(x*y)*p,x*(y*p),"assoc mul");
  335. point_check(test,p,q,r,x,y,x*p+y*q,Point::double_scalarmul(x,p,y,q),"double mul");
  336. p.dual_scalarmul(d1,d2,x,y);
  337. point_check(test,p,q,r,x,y,x*p,d1,"dual mul 1");
  338. point_check(test,p,q,r,x,y,y*p,d2,"dual mul 2");
  339. point_check(test,base,q,r,x,y,x*base+y*q,q.non_secret_combo_with_base(y,x),"ds vt mul");
  340. point_check(test,p,q,r,x,0,Precomputed(p)*x,p*x,"precomp mul");
  341. point_check(test,p,q,r,0,0,r,
  342. Point::from_hash(Buffer(buffer).slice(0,Point::HASH_BYTES))
  343. + Point::from_hash(Buffer(buffer).slice(Point::HASH_BYTES,Point::HASH_BYTES)),
  344. "unih = hash+add"
  345. );
  346. try {
  347. point_check(test,p,q,r,x,0,Point(x.direct_scalarmul(p.serialize())),x*p,"direct mul");
  348. } catch (CryptoException) {
  349. printf(" Direct mul raised CryptoException!\n");
  350. test.fail();
  351. }
  352. q=p;
  353. for (int j=1; j<Group::REMOVED_COFACTOR; j<<=1) q = q.times_two();
  354. decaf_error_t error = r.decode_like_eddsa_and_ignore_cofactor_noexcept(
  355. p.mul_by_cofactor_and_encode_like_eddsa()
  356. );
  357. if (error != DECAF_SUCCESS) {
  358. test.fail();
  359. printf(" Decode like EdDSA failed.");
  360. }
  361. point_check(test,-q,q,r,i,0,q,r,"Encode like EdDSA round-trip");
  362. }
  363. }
  364. static const uint8_t rfc7748_1[DhLadder::PUBLIC_BYTES];
  365. static const uint8_t rfc7748_1000[DhLadder::PUBLIC_BYTES];
  366. static const uint8_t rfc7748_1000000[DhLadder::PUBLIC_BYTES];
  367. static void test_cfrg_crypto() {
  368. Test test("CFRG crypto");
  369. SpongeRng rng(Block("test_cfrg_crypto"),SpongeRng::DETERMINISTIC);
  370. for (int i=0; i<NTESTS && test.passing_now; i++) {
  371. FixedArrayBuffer<DhLadder::PUBLIC_BYTES> base(rng);
  372. FixedArrayBuffer<DhLadder::PRIVATE_BYTES> s1(rng), s2(rng);
  373. SecureBuffer p1 = DhLadder::shared_secret(base,s1);
  374. SecureBuffer p2 = DhLadder::shared_secret(base,s2);
  375. SecureBuffer ss1 = DhLadder::shared_secret(p2,s1);
  376. SecureBuffer ss2 = DhLadder::shared_secret(p1,s2);
  377. if (!memeq(ss1,ss2)) {
  378. test.fail();
  379. printf(" Shared secrets disagree on iteration %d.\n",i);
  380. }
  381. if (!memeq(
  382. DhLadder::shared_secret(DhLadder::base_point(),s1),
  383. DhLadder::derive_public_key(s1)
  384. )) {
  385. test.fail();
  386. printf(" Public keys disagree on iteration %d.\n",i);
  387. }
  388. }
  389. }
  390. static const bool eddsa_prehashed[];
  391. static const Block eddsa_sk[], eddsa_pk[], eddsa_message[], eddsa_context[], eddsa_sig[];
  392. static void test_cfrg_vectors() {
  393. Test test("CFRG test vectors");
  394. SecureBuffer k = DhLadder::base_point();
  395. SecureBuffer u = DhLadder::base_point();
  396. int the_ntests = (NTESTS < 1000000) ? 1000 : 1000000;
  397. /* EdDSA */
  398. for (unsigned int t=0; eddsa_sk[t].size(); t++) {
  399. typename EdDSA<Group>::PrivateKey priv(eddsa_sk[t]);
  400. SecureBuffer eddsa_pk2 = priv.pub().serialize();
  401. if (!memeq(SecureBuffer(eddsa_pk[t]), eddsa_pk2)) {
  402. test.fail();
  403. printf(" EdDSA PK vectors disagree.");
  404. printf("\n Correct: ");
  405. for (unsigned i=0; i<eddsa_pk[t].size(); i++) printf("%02x", eddsa_pk[t][i]);
  406. printf("\n Incorrect: ");
  407. for (unsigned i=0; i<eddsa_pk2.size(); i++) printf("%02x", eddsa_pk2[i]);
  408. printf("\n");
  409. }
  410. SecureBuffer sig;
  411. if (eddsa_prehashed[t]) {
  412. typename EdDSA<Group>::PrivateKeyPh priv2(eddsa_sk[t]);
  413. sig = priv2.sign_with_prehash(eddsa_message[t],eddsa_context[t]);
  414. } else {
  415. sig = priv.sign(eddsa_message[t],eddsa_context[t]);
  416. }
  417. if (!memeq(SecureBuffer(eddsa_sig[t]),sig)) {
  418. test.fail();
  419. printf(" EdDSA sig vectors disagree.");
  420. printf("\n Correct: ");
  421. for (unsigned i=0; i<eddsa_sig[t].size(); i++) printf("%02x", eddsa_sig[t][i]);
  422. printf("\n Incorrect: ");
  423. for (unsigned i=0; i<sig.size(); i++) printf("%02x", sig[i]);
  424. printf("\n");
  425. }
  426. }
  427. /* X25519/X448 */
  428. for (int i=0; i<the_ntests && test.passing_now; i++) {
  429. SecureBuffer n = DhLadder::shared_secret(u,k);
  430. u = k; k = n;
  431. if (i==1-1) {
  432. if (!memeq(k,SecureBuffer(FixedBlock<DhLadder::PUBLIC_BYTES>(rfc7748_1)))) {
  433. test.fail();
  434. printf(" Test vectors disagree at 1.");
  435. }
  436. } else if (i==1000-1) {
  437. if (!memeq(k,SecureBuffer(FixedBlock<DhLadder::PUBLIC_BYTES>(rfc7748_1000)))) {
  438. test.fail();
  439. printf(" Test vectors disagree at 1000.");
  440. }
  441. } else if (i==1000000-1) {
  442. if (!memeq(k,SecureBuffer(FixedBlock<DhLadder::PUBLIC_BYTES>(rfc7748_1000000)))) {
  443. test.fail();
  444. printf(" Test vectors disagree at 1000000.");
  445. }
  446. }
  447. }
  448. }
  449. static void test_eddsa() {
  450. Test test("EdDSA");
  451. SpongeRng rng(Block("test_eddsa"),SpongeRng::DETERMINISTIC);
  452. for (int i=0; i<NTESTS && test.passing_now; i++) {
  453. typename EdDSA<Group>::PrivateKey priv(rng);
  454. typename EdDSA<Group>::PublicKey pub(priv);
  455. SecureBuffer message(i);
  456. rng.read(message);
  457. SecureBuffer context(i%256);
  458. rng.read(context);
  459. SecureBuffer sig = priv.sign(message,context);
  460. try {
  461. pub.verify(sig,message,context);
  462. } catch(CryptoException) {
  463. test.fail();
  464. printf(" Signature validation failed on sig %d\n", i);
  465. }
  466. }
  467. }
  468. /* Thanks Johan Pascal */
  469. static void test_convert_eddsa_to_x() {
  470. Test test("ECDH using EdDSA keys");
  471. SpongeRng rng(Block("test_x_on_eddsa_key"),SpongeRng::DETERMINISTIC);
  472. for (int i=0; i<NTESTS && test.passing_now; i++) {
  473. /* generate 2 pairs of EdDSA keys */
  474. typename EdDSA<Group>::PrivateKey alice_priv(rng);
  475. typename EdDSA<Group>::PublicKey alice_pub(alice_priv);
  476. typename EdDSA<Group>::PrivateKey bob_priv(rng);
  477. typename EdDSA<Group>::PublicKey bob_pub(bob_priv);
  478. /* convert them to ECDH format
  479. * check public key value by computing it from direct conversion and regeneration from converted private)
  480. */
  481. SecureBuffer alice_priv_x = alice_priv.convert_to_x();
  482. SecureBuffer alice_pub_x_conversion = alice_pub.convert_to_x();
  483. SecureBuffer alice_pub_x_generated = DhLadder::derive_public_key(alice_priv_x);
  484. if (!memeq(alice_pub_x_conversion, alice_pub_x_generated)) {
  485. test.fail();
  486. printf(" Ed2X Public key convertion and regeneration from converted private key differs.\n");
  487. }
  488. SecureBuffer bob_priv_x = bob_priv.convert_to_x();
  489. SecureBuffer bob_pub_x_conversion = bob_pub.convert_to_x();
  490. SecureBuffer bob_pub_x_generated = DhLadder::derive_public_key(bob_priv_x);
  491. if (!memeq(bob_pub_x_conversion, bob_pub_x_generated)) {
  492. test.fail();
  493. printf(" Ed2X Public key convertion and regeneration from converted private key differs.\n");
  494. }
  495. /* compute shared secrets and check they match */
  496. SecureBuffer alice_shared = DhLadder::shared_secret(bob_pub_x_conversion, alice_priv_x);
  497. SecureBuffer bob_shared = DhLadder::shared_secret(alice_pub_x_conversion, bob_priv_x);
  498. if (!memeq(alice_shared, bob_shared)) {
  499. test.fail();
  500. printf(" ECDH shared secret mismatch.\n");
  501. }
  502. }
  503. }
  504. static void run() {
  505. printf("Testing %s:\n",Group::name());
  506. test_arithmetic();
  507. test_elligator();
  508. test_ec();
  509. test_eddsa();
  510. test_convert_eddsa_to_x();
  511. test_cfrg_crypto();
  512. test_cfrg_vectors();
  513. printf("\n");
  514. }
  515. }; /* template<GroupId GROUP> struct Tests */
  516. template<typename T>
  517. static void test_xof() {
  518. /* TODO: more testing of XOFs */
  519. Test test("XOF");
  520. SpongeRng rng(Block("test_xof"),SpongeRng::DETERMINISTIC);
  521. FixedArrayBuffer<1024> a,b,c;
  522. rng.read(c);
  523. T s1, s2;
  524. unsigned i;
  525. for (i=0; i<c.size(); i++) s1.update(c.slice(i,1));
  526. s2.update(c);
  527. for (i=0; i<a.size(); i++) s1.output(a.slice(i,1));
  528. s2.output(b);
  529. if (!a.contents_equal(b)) {
  530. test.fail();
  531. printf(" Buffers aren't equal!\n");
  532. }
  533. }
  534. static void test_rng() {
  535. Test test("RNG");
  536. SpongeRng rng_d1(Block("test_rng"),SpongeRng::DETERMINISTIC);
  537. SpongeRng rng_d2(Block("test_rng"),SpongeRng::DETERMINISTIC);
  538. SpongeRng rng_d3(Block("best_rng"),SpongeRng::DETERMINISTIC);
  539. SpongeRng rng_n1;
  540. SpongeRng rng_n2;
  541. SecureBuffer s1,s2,s3;
  542. for (int i=0; i<5; i++) {
  543. s1 = rng_d1.read(16<<i);
  544. s2 = rng_d2.read(16<<i);
  545. s3 = rng_d3.read(16<<i);
  546. if (s1 != s2) {
  547. test.fail();
  548. printf(" Deterministic RNG didn't match!\n");
  549. }
  550. if (s1 == s3) {
  551. test.fail();
  552. printf(" Deterministic matched with different data!\n");
  553. }
  554. rng_d1.stir("hello");
  555. rng_d2.stir("hello");
  556. rng_d3.stir("hello");
  557. s1 = rng_n1.read(16<<i);
  558. s2 = rng_n2.read(16<<i);
  559. if (s1 == s2) {
  560. test.fail();
  561. printf(" Nondeterministic RNG matched!\n");
  562. }
  563. }
  564. rng_d1.stir("hello");
  565. rng_d2.stir("jello");
  566. s1 = rng_d1.read(16);
  567. s2 = rng_d2.read(16);
  568. if (s1 == s2) {
  569. test.fail();
  570. printf(" Deterministic matched with different data!\n");
  571. }
  572. }
  573. #include "vectors.inc.cxx"
  574. int main(int argc, char **argv) {
  575. (void) argc; (void) argv;
  576. test_rng();
  577. test_xof<SHAKE<128> >();
  578. test_xof<SHAKE<256> >();
  579. printf("\n");
  580. run_for_all_curves<Tests>();
  581. if (passing) printf("Passed all tests.\n");
  582. return passing ? 0 : 1;
  583. }