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.
 
 
 
 
 

439 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++ benchmarks, because that's easier.
  10. */
  11. #include <decaf.hxx>
  12. #include <decaf/shake.hxx>
  13. #include <decaf/strobe.hxx>
  14. #include <decaf/spongerng.hxx>
  15. #include <decaf/crypto_255.h>
  16. #include <decaf/crypto_448.h>
  17. #include <decaf/crypto.hxx>
  18. #include <stdio.h>
  19. #include <sys/time.h>
  20. #include <assert.h>
  21. #include <stdint.h>
  22. #include <vector>
  23. #include <algorithm>
  24. using namespace decaf;
  25. static __inline__ void __attribute__((unused)) ignore_result ( int result ) { (void)result; }
  26. static double now(void) {
  27. struct timeval tv;
  28. gettimeofday(&tv, NULL);
  29. return tv.tv_sec + tv.tv_usec/1000000.0;
  30. }
  31. // RDTSC from the chacha code
  32. #ifndef __has_builtin
  33. #define __has_builtin(X) 0
  34. #endif
  35. #if defined(__clang__) && __has_builtin(__builtin_readcyclecounter)
  36. #define rdtsc __builtin_readcyclecounter
  37. #else
  38. static inline uint64_t rdtsc(void) {
  39. u_int64_t out = 0;
  40. # if (defined(__i386__) || defined(__x86_64__))
  41. __asm__ __volatile__ ("rdtsc" : "=A"(out));
  42. # endif
  43. return out;
  44. }
  45. #endif
  46. static void printSI(double x, const char *unit, const char *spacer = " ") {
  47. const char *small[] = {" ","m","µ","n","p"};
  48. const char *big[] = {" ","k","M","G","T"};
  49. if (x < 1) {
  50. unsigned di=0;
  51. for (di=0; di<sizeof(small)/sizeof(*small)-1 && x && x < 1; di++) {
  52. x *= 1000.0;
  53. }
  54. printf("%6.2f%s%s%s", x, spacer, small[di], unit);
  55. } else {
  56. unsigned di=0;
  57. for (di=0; di<sizeof(big)/sizeof(*big)-1 && x && x >= 1000; di++) {
  58. x /= 1000.0;
  59. }
  60. printf("%6.2f%s%s%s", x, spacer, big[di], unit);
  61. }
  62. }
  63. class Benchmark {
  64. static const int NTESTS = 20, NSAMPLES=50, DISCARD=2;
  65. static double totalCy, totalS;
  66. public:
  67. int i, j, ntests, nsamples;
  68. double begin;
  69. uint64_t tsc_begin;
  70. std::vector<double> times;
  71. std::vector<uint64_t> cycles;
  72. Benchmark(const char *s, double factor = 1) {
  73. printf("%s:", s);
  74. if (strlen(s) < 25) printf("%*s",int(25-strlen(s)),"");
  75. fflush(stdout);
  76. i = j = 0;
  77. ntests = NTESTS * factor;
  78. nsamples = NSAMPLES;
  79. begin = now();
  80. tsc_begin = rdtsc();
  81. times = std::vector<double>(NSAMPLES);
  82. cycles = std::vector<uint64_t>(NSAMPLES);
  83. }
  84. ~Benchmark() {
  85. double tsc = 0;
  86. double t = 0;
  87. std::sort(times.begin(), times.end());
  88. std::sort(cycles.begin(), cycles.end());
  89. for (int k=DISCARD; k<nsamples-DISCARD; k++) {
  90. tsc += cycles[k];
  91. t += times[k];
  92. }
  93. totalCy += tsc;
  94. totalS += t;
  95. t /= ntests*(nsamples-2*DISCARD);
  96. tsc /= ntests*(nsamples-2*DISCARD);
  97. printSI(t,"s");
  98. printf(" ");
  99. printSI(1/t,"/s");
  100. if (tsc) { printf(" "); printSI(tsc, "cy"); }
  101. printf("\n");
  102. }
  103. inline bool iter() {
  104. i++;
  105. if (i >= ntests) {
  106. uint64_t tsc = rdtsc() - tsc_begin;
  107. double t = now() - begin;
  108. begin += t;
  109. tsc_begin += tsc;
  110. assert(j >= 0 && j < nsamples);
  111. cycles[j] = tsc;
  112. times[j] = t;
  113. j++;
  114. i = 0;
  115. }
  116. return j < nsamples;
  117. }
  118. static void calib() {
  119. if (totalS && totalCy) {
  120. const char *s = "Cycle calibration";
  121. printf("%s:", s);
  122. if (strlen(s) < 25) printf("%*s",int(25-strlen(s)),"");
  123. printSI(totalCy / totalS, "Hz");
  124. printf("\n");
  125. }
  126. }
  127. };
  128. double Benchmark::totalCy = 0, Benchmark::totalS = 0;
  129. template<typename Group> struct Benches {
  130. typedef typename Group::Scalar Scalar;
  131. typedef typename Group::Point Point;
  132. typedef typename Group::Precomputed Precomputed;
  133. static void tdh (
  134. SpongeRng &clientRng,
  135. SpongeRng &serverRng,
  136. Scalar x, const Block &gx,
  137. Scalar y, const Block &gy
  138. ) {
  139. /* "TripleDH". A bit of a hack, really: the real TripleDH
  140. * sends gx and gy and certs over the channel, but its goal
  141. * is actually the opposite of STROBE in this case: it doesn't
  142. * hash gx and gy into the session secret (only into the MAC
  143. * and AD) because of IPR concerns.
  144. */
  145. Strobe client("example::tripleDH",Strobe::CLIENT), server("example::tripleDH",Strobe::SERVER);
  146. Scalar xe(clientRng);
  147. SecureBuffer gxe((Precomputed::base() * xe).serialize());
  148. client.send_plaintext(gxe);
  149. server.recv_plaintext(gxe);
  150. Scalar ye(serverRng);
  151. SecureBuffer gye((Precomputed::base() * ye).serialize());
  152. server.send_plaintext(gye);
  153. client.recv_plaintext(gye);
  154. Point pgxe(gxe);
  155. server.dh_key(pgxe*ye);
  156. SecureBuffer tag1 = server.produce_auth();
  157. //SecureBuffer ct = server.encrypt(gy);
  158. server.dh_key(pgxe*y);
  159. SecureBuffer tag2 = server.produce_auth();
  160. Point pgye(gye);
  161. client.dh_key(pgye*xe);
  162. client.verify_auth(tag1);
  163. client.dh_key(Point(gy) * xe);
  164. client.verify_auth(tag2);
  165. // ct = client.encrypt(gx);
  166. client.dh_key(pgye * x);
  167. tag1 = client.produce_auth();
  168. client.respec(STROBE_KEYED_128);
  169. server.dh_key(Point(gx) * ye);
  170. server.verify_auth(tag1);
  171. server.respec(STROBE_KEYED_128);
  172. }
  173. static void fhmqv (
  174. SpongeRng &clientRng,
  175. SpongeRng &serverRng,
  176. Scalar x, const Block &gx,
  177. Scalar y, const Block &gy
  178. ) {
  179. /* Don't use this, it's probably patented */
  180. Strobe client("example::fhmqv",Strobe::CLIENT), server("example::fhmqv",Strobe::SERVER);
  181. Scalar xe(clientRng);
  182. client.send_plaintext(gx);
  183. server.recv_plaintext(gx);
  184. SecureBuffer gxe((Precomputed::base() * xe).serialize());
  185. server.send_plaintext(gxe);
  186. client.recv_plaintext(gxe);
  187. Scalar ye(serverRng);
  188. server.send_plaintext(gy);
  189. client.recv_plaintext(gy);
  190. SecureBuffer gye((Precomputed::base() * ye).serialize());
  191. server.send_plaintext(gye);
  192. Scalar schx(server.prng(Scalar::SER_BYTES));
  193. Scalar schy(server.prng(Scalar::SER_BYTES));
  194. Scalar yec = y + ye*schy;
  195. server.dh_key(Point::double_scalarmul(Point(gx),yec,Point(gxe),yec*schx));
  196. SecureBuffer as = server.produce_auth();
  197. client.recv_plaintext(gye);
  198. Scalar cchx(client.prng(Scalar::SER_BYTES));
  199. Scalar cchy(client.prng(Scalar::SER_BYTES));
  200. Scalar xec = x + xe*schx;
  201. client.dh_key(Point::double_scalarmul(Point(gy),xec,Point(gye),xec*schy));
  202. client.verify_auth(as);
  203. SecureBuffer ac = client.produce_auth();
  204. client.respec(STROBE_KEYED_128);
  205. server.verify_auth(ac);
  206. server.respec(STROBE_KEYED_128);
  207. }
  208. static void spake2ee(
  209. SpongeRng &clientRng,
  210. SpongeRng &serverRng,
  211. const Block &hashed_password,
  212. bool aug
  213. ) {
  214. Strobe client("example::spake2ee",Strobe::CLIENT), server("example::spake2ee",Strobe::SERVER);
  215. Scalar x(clientRng);
  216. SHAKE<256> shake;
  217. shake.update(hashed_password);
  218. SecureBuffer h0 = shake.output(Point::HASH_BYTES);
  219. SecureBuffer h1 = shake.output(Point::HASH_BYTES);
  220. SecureBuffer h2 = shake.output(Scalar::SER_BYTES);
  221. Scalar gs(h2);
  222. Point hc = Point::from_hash(h0);
  223. hc = Point::from_hash(h0); // double-count
  224. Point hs = Point::from_hash(h1);
  225. hs = Point::from_hash(h1); // double-count
  226. SecureBuffer gx((Precomputed::base() * x + hc).serialize());
  227. client.send_plaintext(gx);
  228. server.recv_plaintext(gx);
  229. Scalar y(serverRng);
  230. SecureBuffer gy((Precomputed::base() * y + hs).serialize());
  231. server.send_plaintext(gy);
  232. client.recv_plaintext(gy);
  233. server.dh_key(h1);
  234. server.dh_key((Point(gx) - hc)*y);
  235. if(aug) {
  236. /* This step isn't actually online but whatever, it's fastish */
  237. SecureBuffer serverAug((Precomputed::base() * gs).serialize());
  238. server.dh_key(Point(serverAug)*y);
  239. }
  240. SecureBuffer tag = server.produce_auth();
  241. client.dh_key(h1);
  242. Point pgy(gy); pgy -= hs;
  243. client.dh_key(pgy*x);
  244. if (aug) client.dh_key(pgy * gs);
  245. client.verify_auth(tag);
  246. tag = client.produce_auth();
  247. client.respec(STROBE_KEYED_128);
  248. /* A real protocol would continue with fork etc here... */
  249. server.verify_auth(tag);
  250. server.respec(STROBE_KEYED_128);
  251. }
  252. static void cfrg() {
  253. SpongeRng rng(Block("bench_cfrg_crypto"),SpongeRng::DETERMINISTIC);
  254. FixedArrayBuffer<Group::DhLadder::PUBLIC_BYTES> base(rng);
  255. FixedArrayBuffer<Group::DhLadder::PRIVATE_BYTES> s1(rng);
  256. for (Benchmark b("RFC 7748 keygen"); b.iter(); ) { Group::DhLadder::generate_key(s1); }
  257. for (Benchmark b("RFC 7748 shared secret"); b.iter(); ) { Group::DhLadder::shared_secret(base,s1); }
  258. }
  259. static void macro() {
  260. printf("\nMacro-benchmarks for %s:\n", Group::name());
  261. printf("CFRG crypto benchmarks:\n");
  262. cfrg();
  263. printf("\nSample crypto benchmarks:\n");
  264. SpongeRng rng(Block("macro rng seed"),SpongeRng::DETERMINISTIC);
  265. PrivateKey<Group> s1((NOINIT())), s2(rng);
  266. PublicKey<Group> p1((NOINIT())), p2(s2);
  267. SecureBuffer message = rng.read(5), sig, ss;
  268. for (Benchmark b("Create private key",1); b.iter(); ) {
  269. s1 = PrivateKey<Group>(rng);
  270. SecureBuffer bb = s1.serialize();
  271. }
  272. for (Benchmark b("Sign",1); b.iter(); ) {
  273. sig = s1.sign(message);
  274. }
  275. p1 = s1.pub();
  276. for (Benchmark b("Verify",1); b.iter(); ) {
  277. rng.read(Buffer(message));
  278. try { p1.verify(message, sig); } catch (CryptoException) {}
  279. }
  280. for (Benchmark b("SharedSecret",1); b.iter(); ) {
  281. ss = s1.sharedSecret(p2,32,true);
  282. }
  283. printf("\nProtocol benchmarks:\n");
  284. SpongeRng clientRng(Block("client rng seed"),SpongeRng::DETERMINISTIC);
  285. SpongeRng serverRng(Block("server rng seed"),SpongeRng::DETERMINISTIC);
  286. SecureBuffer hashedPassword(Block("hello world"));
  287. for (Benchmark b("Spake2ee c+s",0.1); b.iter(); ) {
  288. spake2ee(clientRng, serverRng, hashedPassword,false);
  289. }
  290. for (Benchmark b("Spake2ee c+s aug",0.1); b.iter(); ) {
  291. spake2ee(clientRng, serverRng, hashedPassword,true);
  292. }
  293. Scalar x(clientRng);
  294. SecureBuffer gx((Precomputed::base() * x).serialize());
  295. Scalar y(serverRng);
  296. SecureBuffer gy((Precomputed::base() * y).serialize());
  297. for (Benchmark b("FHMQV c+s",0.1); b.iter(); ) {
  298. fhmqv(clientRng, serverRng,x,gx,y,gy);
  299. }
  300. for (Benchmark b("TripleDH anon c+s",0.1); b.iter(); ) {
  301. tdh(clientRng, serverRng, x,gx,y,gy);
  302. }
  303. }
  304. static void micro() {
  305. SpongeRng rng(Block("per-curve-benchmarks"),SpongeRng::DETERMINISTIC);
  306. Precomputed pBase;
  307. Point p,q;
  308. Scalar s(1),t(2);
  309. SecureBuffer ep, ep2(Point::SER_BYTES*2);
  310. printf("\nMicro-benchmarks for %s:\n", Group::name());
  311. for (Benchmark b("Scalar add", 1000); b.iter(); ) { s+=t; }
  312. for (Benchmark b("Scalar times", 100); b.iter(); ) { s*=t; }
  313. for (Benchmark b("Scalar inv", 1); b.iter(); ) { s.inverse(); }
  314. for (Benchmark b("Point add", 100); b.iter(); ) { p += q; }
  315. for (Benchmark b("Point double", 100); b.iter(); ) { p.double_in_place(); }
  316. for (Benchmark b("Point scalarmul"); b.iter(); ) { p * s; }
  317. for (Benchmark b("Point encode"); b.iter(); ) { ep = p.serialize(); }
  318. for (Benchmark b("Point decode"); b.iter(); ) { p = Point(ep); }
  319. for (Benchmark b("Point create/destroy"); b.iter(); ) { Point r; }
  320. for (Benchmark b("Point hash nonuniform"); b.iter(); ) { Point::from_hash(ep); }
  321. for (Benchmark b("Point hash uniform"); b.iter(); ) { Point::from_hash(ep2); }
  322. for (Benchmark b("Point unhash nonuniform"); b.iter(); ) { ignore_result(p.invert_elligator(ep,0)); }
  323. for (Benchmark b("Point unhash uniform"); b.iter(); ) { ignore_result(p.invert_elligator(ep2,0)); }
  324. for (Benchmark b("Point steg"); b.iter(); ) { p.steg_encode(rng); }
  325. for (Benchmark b("Point double scalarmul"); b.iter(); ) { Point::double_scalarmul(p,s,q,t); }
  326. for (Benchmark b("Point dual scalarmul"); b.iter(); ) { p.dual_scalarmul(p,q,s,t); }
  327. for (Benchmark b("Point precmp scalarmul"); b.iter(); ) { pBase * s; }
  328. for (Benchmark b("Point double scalarmul_v"); b.iter(); ) {
  329. s = Scalar(rng);
  330. t = Scalar(rng);
  331. p.non_secret_combo_with_base(s,t);
  332. }
  333. }
  334. }; /* template <typename group> struct Benches */
  335. int main(int argc, char **argv) {
  336. bool micro = false;
  337. if (argc >= 2 && !strcmp(argv[1], "--micro"))
  338. micro = true;
  339. SpongeRng rng(Block("micro-benchmarks"),SpongeRng::DETERMINISTIC);
  340. if (micro) {
  341. printf("\nMicro-benchmarks:\n");
  342. SHAKE<128> shake1;
  343. SHAKE<256> shake2;
  344. SHA3<512> sha5;
  345. Strobe strobe("example::bench",Strobe::CLIENT);
  346. unsigned char b1024[1024] = {1};
  347. for (Benchmark b("SHAKE128 1kiB", 30); b.iter(); ) { shake1 += Buffer(b1024,1024); }
  348. for (Benchmark b("SHAKE256 1kiB", 30); b.iter(); ) { shake2 += Buffer(b1024,1024); }
  349. for (Benchmark b("SHA3-512 1kiB", 30); b.iter(); ) { sha5 += Buffer(b1024,1024); }
  350. strobe.dh_key(Buffer(b1024,1024));
  351. strobe.respec(STROBE_128);
  352. for (Benchmark b("STROBE128 1kiB", 10); b.iter(); ) {
  353. strobe.encrypt_no_auth(Buffer(b1024,1024),Buffer(b1024,1024));
  354. }
  355. strobe.respec(STROBE_256);
  356. for (Benchmark b("STROBE256 1kiB", 10); b.iter(); ) {
  357. strobe.encrypt_no_auth(Buffer(b1024,1024),Buffer(b1024,1024));
  358. }
  359. strobe.respec(STROBE_KEYED_128);
  360. for (Benchmark b("STROBEk128 1kiB", 10); b.iter(); ) {
  361. strobe.encrypt_no_auth(Buffer(b1024,1024),Buffer(b1024,1024));
  362. }
  363. strobe.respec(STROBE_KEYED_256);
  364. for (Benchmark b("STROBEk256 1kiB", 10); b.iter(); ) {
  365. strobe.encrypt_no_auth(Buffer(b1024,1024),Buffer(b1024,1024));
  366. }
  367. Benches<IsoEd25519>::micro();
  368. Benches<Ed448Goldilocks>::micro();
  369. }
  370. Benches<IsoEd25519>::macro();
  371. Benches<Ed448Goldilocks>::macro();
  372. printf("\n");
  373. Benchmark::calib();
  374. printf("\n");
  375. return 0;
  376. }