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.
 
 
 
 
 

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