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.
 
 
 
 
 

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