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.
 
 
 
 
 

436 lines
14 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_255.h>
  14. #include <decaf/crypto_448.h>
  15. #include <decaf/crypto.hxx>
  16. #include <stdio.h>
  17. using namespace decaf;
  18. static bool passing = true;
  19. static const long NTESTS = 10000;
  20. class Test {
  21. public:
  22. bool passing_now;
  23. Test(const char *test) {
  24. passing_now = true;
  25. printf("%s...", test);
  26. if (strlen(test) < 27) printf("%*s",int(27-strlen(test)),"");
  27. fflush(stdout);
  28. }
  29. ~Test() {
  30. if (std::uncaught_exception()) {
  31. fail();
  32. printf(" due to uncaught exception.\n");
  33. }
  34. if (passing_now) printf("[PASS]\n");
  35. }
  36. void fail() {
  37. if (!passing_now) return;
  38. passing_now = passing = false;
  39. printf("[FAIL]\n");
  40. }
  41. };
  42. template<typename Group> struct Tests {
  43. typedef typename Group::Scalar Scalar;
  44. typedef typename Group::Point Point;
  45. typedef typename Group::Precomputed Precomputed;
  46. static void print(const char *name, const Scalar &x) {
  47. unsigned char buffer[Scalar::SER_BYTES];
  48. x.serializeInto(buffer);
  49. printf(" %s = 0x", name);
  50. for (int i=sizeof(buffer)-1; i>=0; i--) {
  51. printf("%02x", buffer[i]);
  52. }
  53. printf("\n");
  54. }
  55. static void hexprint(const char *name, const SecureBuffer &buffer) {
  56. printf(" %s = 0x", name);
  57. for (int i=buffer.size()-1; i>=0; i--) {
  58. printf("%02x", buffer[i]);
  59. }
  60. printf("\n");
  61. }
  62. static void print(const char *name, const Point &x) {
  63. unsigned char buffer[Point::SER_BYTES];
  64. x.serializeInto(buffer);
  65. printf(" %s = 0x", name);
  66. for (int i=Point::SER_BYTES-1; i>=0; i--) {
  67. printf("%02x", buffer[i]);
  68. }
  69. printf("\n");
  70. }
  71. static bool arith_check(
  72. Test &test,
  73. const Scalar &x,
  74. const Scalar &y,
  75. const Scalar &z,
  76. const Scalar &l,
  77. const Scalar &r,
  78. const char *name
  79. ) {
  80. if (l == r) return true;
  81. test.fail();
  82. printf(" %s", name);
  83. print("x", x);
  84. print("y", y);
  85. print("z", z);
  86. print("lhs", l);
  87. print("rhs", r);
  88. return false;
  89. }
  90. static bool point_check(
  91. Test &test,
  92. const Point &p,
  93. const Point &q,
  94. const Point &R,
  95. const Scalar &x,
  96. const Scalar &y,
  97. const Point &l,
  98. const Point &r,
  99. const char *name
  100. ) {
  101. bool good = l==r;
  102. if (!p.validate()) { good = false; printf(" p invalid\n"); }
  103. if (!q.validate()) { good = false; printf(" q invalid\n"); }
  104. if (!r.validate()) { good = false; printf(" r invalid\n"); }
  105. if (!l.validate()) { good = false; printf(" l invalid\n"); }
  106. if (good) return true;
  107. test.fail();
  108. printf(" %s", name);
  109. print("x", x);
  110. print("y", y);
  111. print("p", p);
  112. print("q", q);
  113. print("r", R);
  114. print("lhs", r);
  115. print("rhs", l);
  116. return false;
  117. }
  118. static void test_arithmetic() {
  119. SpongeRng rng(Block("test_arithmetic"));
  120. Test test("Arithmetic");
  121. Scalar x(0),y(0),z(0);
  122. arith_check(test,x,y,z,INT_MAX,(decaf_word_t)INT_MAX,"cast from max");
  123. arith_check(test,x,y,z,INT_MIN,-Scalar(1+(decaf_word_t)INT_MAX),"cast from min");
  124. for (int i=0; i<NTESTS*10 && test.passing_now; i++) {
  125. /* TODO: pathological cases */
  126. size_t sob = DECAF_255_SCALAR_BYTES + 8 - (i%16);
  127. Scalar x(rng.read(sob));
  128. Scalar y(rng.read(sob));
  129. Scalar z(rng.read(sob));
  130. arith_check(test,x,y,z,x+y,y+x,"commute add");
  131. arith_check(test,x,y,z,x,x+0,"ident add");
  132. arith_check(test,x,y,z,x,x-0,"ident sub");
  133. arith_check(test,x,y,z,x+(y+z),(x+y)+z,"assoc 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 - x*z,"distributive mul/add");
  136. arith_check(test,x,y,z,x*(y*z),(x*y)*z,"assoc mul");
  137. arith_check(test,x,y,z,x*y,y*x,"commute mul");
  138. arith_check(test,x,y,z,x,x*1,"ident mul");
  139. arith_check(test,x,y,z,0,x*0,"mul by 0");
  140. arith_check(test,x,y,z,-x,x*-1,"mul by -1");
  141. arith_check(test,x,y,z,x+x,x*2,"mul by 2");
  142. if (i%20) continue;
  143. if (y!=0) arith_check(test,x,y,z,x*y/y,x,"invert");
  144. try {
  145. y = x/0;
  146. test.fail();
  147. printf(" Inverted zero!");
  148. print("x", x);
  149. print("y", y);
  150. } catch(CryptoException) {}
  151. }
  152. }
  153. static void test_elligator() {
  154. SpongeRng rng(Block("test_elligator"));
  155. Test test("Elligator");
  156. const int NHINTS = Group::REMOVED_COFACTOR * 2;
  157. SecureBuffer *alts[NHINTS];
  158. bool successes[NHINTS];
  159. SecureBuffer *alts2[NHINTS];
  160. bool successes2[NHINTS];
  161. for (int i=0; i<NTESTS/10 && (test.passing_now || i < 100); i++) {
  162. size_t len = (i % (2*Point::HASH_BYTES + 3));
  163. SecureBuffer b1(len);
  164. if (i!=Point::HASH_BYTES) rng.read(b1); /* special test case */
  165. if (i==1) b1[0] = 1; /* special case test */
  166. if (len >= Point::HASH_BYTES) b1[Point::HASH_BYTES-1] &= 0x7F; // FIXME MAGIC
  167. Point s = Point::from_hash(b1), ss=s;
  168. for (int j=0; j<(i&3); j++) ss = ss.debugging_torque();
  169. ss = ss.debugging_pscale(rng);
  170. bool good = false;
  171. for (int j=0; j<NHINTS; j++) {
  172. alts[j] = new SecureBuffer(len);
  173. alts2[j] = new SecureBuffer(len);
  174. if (len > Point::HASH_BYTES)
  175. memcpy(&(*alts[j])[Point::HASH_BYTES], &b1[Point::HASH_BYTES], len-Point::HASH_BYTES);
  176. if (len > Point::HASH_BYTES)
  177. memcpy(&(*alts2[j])[Point::HASH_BYTES], &b1[Point::HASH_BYTES], len-Point::HASH_BYTES);
  178. successes[j] = decaf_successful( s.invert_elligator(*alts[j], j));
  179. successes2[j] = decaf_successful(ss.invert_elligator(*alts2[j],j));
  180. if (successes[j] != successes2[j]
  181. || (successes[j] && successes2[j] && *alts[j] != *alts2[j])
  182. ) {
  183. test.fail();
  184. printf(" Unscalable Elligator inversion: i=%d, hint=%d, s=%d,%d\n",i,j,
  185. -int(successes[j]),-int(successes2[j]));
  186. hexprint("x",b1);
  187. hexprint("X",*alts[j]);
  188. hexprint("X",*alts2[j]);
  189. }
  190. if (successes[j]) {
  191. good = good || (b1 == *alts[j]);
  192. for (int k=0; k<j; k++) {
  193. if (successes[k] && *alts[j] == *alts[k]) {
  194. test.fail();
  195. printf(" Duplicate Elligator inversion: i=%d, hints=%d, %d\n",i,j,k);
  196. hexprint("x",b1);
  197. hexprint("X",*alts[j]);
  198. }
  199. }
  200. if (s != Point::from_hash(*alts[j])) {
  201. test.fail();
  202. printf(" Fail Elligator inversion round-trip: i=%d, hint=%d %s\n",i,j,
  203. (s==-Point::from_hash(*alts[j])) ? "[output was -input]": "");
  204. hexprint("x",b1);
  205. hexprint("X",*alts[j]);
  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. if (Group::FIELD_MODULUS_TYPE == 3) {
  237. /* When p == 3 mod 4, the QNR is -1, so u*1^2 = -1 also produces the
  238. * identity.
  239. */
  240. point_check(test,id,id,id,0,0,Point::from_hash("\x01"),id,"fh1");
  241. }
  242. for (int i=0; i<NTESTS && test.passing_now; i++) {
  243. /* TODO: pathological cases */
  244. Scalar x(rng);
  245. Scalar y(rng);
  246. Point p(rng);
  247. Point q(rng);
  248. Point d1, d2;
  249. SecureBuffer buffer(2*Point::HASH_BYTES);
  250. rng.read(buffer);
  251. Point r = Point::from_hash(buffer);
  252. point_check(test,p,q,r,0,0,p,Point(p.serialize()),"round-trip");
  253. Point pp = p.debugging_torque().debugging_pscale(rng);
  254. if (!memeq(pp.serialize(),p.serialize())) {
  255. test.fail();
  256. printf("Fail torque seq test\n");
  257. }
  258. point_check(test,p,q,r,0,0,p,pp,"torque eq");
  259. point_check(test,p,q,r,0,0,p+q,q+p,"commute add");
  260. point_check(test,p,q,r,0,0,(p-q)+q,p,"correct sub");
  261. point_check(test,p,q,r,0,0,p+(q+r),(p+q)+r,"assoc add");
  262. point_check(test,p,q,r,0,0,p.times_two(),p+p,"dbl add");
  263. if (i%10) continue;
  264. point_check(test,p,q,r,x,0,x*(p+q),x*p+x*q,"distr mul");
  265. point_check(test,p,q,r,x,y,(x*y)*p,x*(y*p),"assoc mul");
  266. point_check(test,p,q,r,x,y,x*p+y*q,Point::double_scalarmul(x,p,y,q),"double mul");
  267. p.dual_scalarmul(d1,d2,x,y);
  268. point_check(test,p,q,r,x,y,x*p,d1,"dual mul 1");
  269. point_check(test,p,q,r,x,y,y*p,d2,"dual mul 2");
  270. point_check(test,base,q,r,x,y,x*base+y*q,q.non_secret_combo_with_base(y,x),"ds vt mul");
  271. point_check(test,p,q,r,x,0,Precomputed(p)*x,p*x,"precomp mul");
  272. point_check(test,p,q,r,0,0,r,
  273. Point::from_hash(Buffer(buffer).slice(0,Point::HASH_BYTES))
  274. + Point::from_hash(Buffer(buffer).slice(Point::HASH_BYTES,Point::HASH_BYTES)),
  275. "unih = hash+add"
  276. );
  277. point_check(test,p,q,r,x,0,Point(x.direct_scalarmul(p.serialize())),x*p,"direct mul");
  278. }
  279. }
  280. static void test_crypto() {
  281. Test test("Sample crypto");
  282. SpongeRng rng(Block("test_decaf_crypto"));
  283. for (int i=0; i<NTESTS && test.passing_now; i++) {
  284. PrivateKey<Group> priv1(rng), priv2(rng);
  285. PublicKey<Group> pub1(priv1), pub2(priv2);
  286. SecureBuffer message = rng.read(i);
  287. SecureBuffer sig(priv1.sign(message));
  288. pub1.verify(message, sig);
  289. }
  290. }
  291. }; // template<GroupId GROUP>
  292. static void test_decaf_255() {
  293. Test test("Sample crypto");
  294. SpongeRng rng(Block("test_decaf"));
  295. decaf_255_symmetric_key_t proto1,proto2;
  296. decaf_255_private_key_t s1,s2;
  297. decaf_255_public_key_t p1,p2;
  298. decaf_255_signature_t sig;
  299. unsigned char shared1[1234],shared2[1234];
  300. const char *message = "Hello, world!";
  301. for (int i=0; i<NTESTS && test.passing_now; i++) {
  302. rng.read(Buffer(proto1,sizeof(proto1)));
  303. rng.read(Buffer(proto2,sizeof(proto2)));
  304. decaf_255_derive_private_key(s1,proto1);
  305. decaf_255_private_to_public(p1,s1);
  306. decaf_255_derive_private_key(s2,proto2);
  307. decaf_255_private_to_public(p2,s2);
  308. if (DECAF_SUCCESS != decaf_255_shared_secret (shared1,sizeof(shared1),s1,p2,0)) {
  309. test.fail(); printf("Fail ss12\n");
  310. }
  311. if (DECAF_SUCCESS != decaf_255_shared_secret (shared2,sizeof(shared2),s2,p1,1)) {
  312. test.fail(); printf("Fail ss21\n");
  313. }
  314. if (memcmp(shared1,shared2,sizeof(shared1))) {
  315. test.fail(); printf("Fail ss21 == ss12\n");
  316. }
  317. decaf_255_sign (sig,s1,(const unsigned char *)message,strlen(message));
  318. if (DECAF_SUCCESS != decaf_255_verify (sig,p1,(const unsigned char *)message,strlen(message))) {
  319. test.fail(); printf("Fail sig ver\n");
  320. }
  321. }
  322. }
  323. /* TODO: don't copy-paste */
  324. static void test_decaf_448() {
  325. Test test("Sample crypto");
  326. SpongeRng rng(Block("test_decaf"));
  327. decaf_448_symmetric_key_t proto1,proto2;
  328. decaf_448_private_key_t s1,s2;
  329. decaf_448_public_key_t p1,p2;
  330. decaf_448_signature_t sig;
  331. unsigned char shared1[1234],shared2[1234];
  332. const char *message = "Hello, world!";
  333. for (int i=0; i<NTESTS && test.passing_now; i++) {
  334. rng.read(Buffer(proto1,sizeof(proto1)));
  335. rng.read(Buffer(proto2,sizeof(proto2)));
  336. decaf_448_derive_private_key(s1,proto1);
  337. decaf_448_private_to_public(p1,s1);
  338. decaf_448_derive_private_key(s2,proto2);
  339. decaf_448_private_to_public(p2,s2);
  340. if (DECAF_SUCCESS != decaf_448_shared_secret (shared1,sizeof(shared1),s1,p2,0)) {
  341. test.fail(); printf("Fail ss12\n");
  342. }
  343. if (DECAF_SUCCESS != decaf_448_shared_secret (shared2,sizeof(shared2),s2,p1,1)) {
  344. test.fail(); printf("Fail ss21\n");
  345. }
  346. if (memcmp(shared1,shared2,sizeof(shared1))) {
  347. test.fail(); printf("Fail ss21 == ss12\n");
  348. }
  349. decaf_448_sign (sig,s1,(const unsigned char *)message,strlen(message));
  350. if (DECAF_SUCCESS != decaf_448_verify (sig,p1,(const unsigned char *)message,strlen(message))) {
  351. test.fail(); printf("Fail sig ver\n");
  352. }
  353. }
  354. }
  355. int main(int argc, char **argv) {
  356. (void) argc; (void) argv;
  357. printf("Testing %s:\n",IsoEd25519::name());
  358. Tests<IsoEd25519>::test_arithmetic();
  359. Tests<IsoEd25519>::test_elligator();
  360. Tests<IsoEd25519>::test_ec();
  361. Tests<IsoEd25519>::test_crypto();
  362. test_decaf_255();
  363. printf("\n");
  364. printf("Testing %s:\n", Ed448Goldilocks::name());
  365. Tests<Ed448Goldilocks>::test_arithmetic();
  366. Tests<Ed448Goldilocks>::test_elligator();
  367. Tests<Ed448Goldilocks>::test_ec();
  368. Tests<Ed448Goldilocks>::test_crypto();
  369. test_decaf_448();
  370. if (passing) printf("Passed all tests.\n");
  371. return passing ? 0 : 1;
  372. }