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.
 
 
 
 
 

405 lines
12 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 <stdio.h>
  15. #include <sys/time.h>
  16. #include <assert.h>
  17. #include <stdint.h>
  18. #include <vector>
  19. #include <algorithm>
  20. using namespace decaf;
  21. typedef Ed255::Scalar Scalar;
  22. typedef Ed255::Point Point;
  23. typedef Ed255::Precomputed Precomputed;
  24. static __inline__ void __attribute__((unused)) ignore_result ( int result ) { (void)result; }
  25. static double now(void) {
  26. struct timeval tv;
  27. gettimeofday(&tv, NULL);
  28. return tv.tv_sec + tv.tv_usec/1000000.0;
  29. }
  30. // RDTSC from the chacha code
  31. #ifndef __has_builtin
  32. #define __has_builtin(X) 0
  33. #endif
  34. #if defined(__clang__) && __has_builtin(__builtin_readcyclecounter)
  35. #define rdtsc __builtin_readcyclecounter
  36. #else
  37. static inline uint64_t rdtsc(void) {
  38. u_int64_t out = 0;
  39. # if (defined(__i386__) || defined(__x86_64__))
  40. __asm__ __volatile__ ("rdtsc" : "=A"(out));
  41. # endif
  42. return out;
  43. }
  44. #endif
  45. static void printSI(double x, const char *unit, const char *spacer = " ") {
  46. const char *small[] = {" ","m","ยต","n","p"};
  47. const char *big[] = {" ","k","M","G","T"};
  48. if (x < 1) {
  49. unsigned di=0;
  50. for (di=0; di<sizeof(small)/sizeof(*small)-1 && x && x < 1; di++) {
  51. x *= 1000.0;
  52. }
  53. printf("%6.2f%s%s%s", x, spacer, small[di], unit);
  54. } else {
  55. unsigned di=0;
  56. for (di=0; di<sizeof(big)/sizeof(*big)-1 && x && x >= 1000; di++) {
  57. x /= 1000.0;
  58. }
  59. printf("%6.2f%s%s%s", x, spacer, big[di], unit);
  60. }
  61. }
  62. class Benchmark {
  63. static const int NTESTS = 20, NSAMPLES=50, DISCARD=2;
  64. static double totalCy, totalS;
  65. /* FIXME Tcy if get descheduled */
  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. static void tdh (
  130. SpongeRng &clientRng,
  131. SpongeRng &serverRng,
  132. Scalar x, const Block &gx,
  133. Scalar y, const Block &gy
  134. ) {
  135. Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
  136. Scalar xe(clientRng);
  137. SecureBuffer gxe = Precomputed::base() * xe;
  138. client.send_plaintext(gxe);
  139. server.recv_plaintext(gxe);
  140. Scalar ye(serverRng);
  141. SecureBuffer gye = Precomputed::base() * ye;
  142. server.send_plaintext(gye);
  143. client.recv_plaintext(gye);
  144. Point pgxe(gxe);
  145. server.key(pgxe*ye);
  146. SecureBuffer tag1 = server.produce_auth();
  147. SecureBuffer ct = server.encrypt(gy);
  148. server.key(pgxe*y);
  149. SecureBuffer tag2 = server.produce_auth();
  150. Point pgye(gye);
  151. client.key(pgye*xe);
  152. client.verify_auth(tag1);
  153. client.key(Point(client.decrypt(ct)) * xe);
  154. client.verify_auth(tag2);
  155. ct = client.encrypt(gx);
  156. client.key(pgye * x);
  157. tag1 = client.produce_auth();
  158. client.respec(STROBE_KEYED_128);
  159. server.key(Point(server.decrypt(ct)) * ye);
  160. server.verify_auth(tag1);
  161. server.respec(STROBE_KEYED_128);
  162. }
  163. static void fhmqv (
  164. SpongeRng &clientRng,
  165. SpongeRng &serverRng,
  166. Scalar x, const Block &gx,
  167. Scalar y, const Block &gy
  168. ) {
  169. /* Don't use this, it's probably patented */
  170. Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
  171. Scalar xe(clientRng);
  172. client.send_plaintext(gx);
  173. server.recv_plaintext(gx);
  174. SecureBuffer gxe = Precomputed::base() * xe;
  175. server.send_plaintext(gxe);
  176. client.recv_plaintext(gxe);
  177. Scalar ye(serverRng);
  178. server.send_plaintext(gy);
  179. client.recv_plaintext(gy);
  180. SecureBuffer gye = Precomputed::base() * ye;
  181. server.send_plaintext(gye);
  182. Scalar schx(server.prng(Scalar::SER_BYTES));
  183. Scalar schy(server.prng(Scalar::SER_BYTES));
  184. Scalar yec = y + ye*schy;
  185. server.key(Point::double_scalarmul(Point(gx),yec,Point(gxe),yec*schx));
  186. SecureBuffer as = server.produce_auth();
  187. client.recv_plaintext(gye);
  188. Scalar cchx(client.prng(Scalar::SER_BYTES));
  189. Scalar cchy(client.prng(Scalar::SER_BYTES));
  190. Scalar xec = x + xe*schx;
  191. client.key(Point::double_scalarmul(Point(gy),xec,Point(gye),xec*schy));
  192. client.verify_auth(as);
  193. SecureBuffer ac = client.produce_auth();
  194. client.respec(STROBE_KEYED_128);
  195. server.verify_auth(ac);
  196. server.respec(STROBE_KEYED_128);
  197. }
  198. static void spake2ee(
  199. SpongeRng &clientRng,
  200. SpongeRng &serverRng,
  201. const Block &hashed_password,
  202. bool aug
  203. ) {
  204. Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
  205. Scalar x(clientRng);
  206. SHAKE<256> shake;
  207. shake.update(hashed_password);
  208. SecureBuffer h0 = shake.output(Point::HASH_BYTES);
  209. SecureBuffer h1 = shake.output(Point::HASH_BYTES);
  210. SecureBuffer h2 = shake.output(Scalar::SER_BYTES);
  211. Scalar gs(h2);
  212. Point hc = Point::from_hash(h0);
  213. hc = Point::from_hash(h0); // double-count
  214. Point hs = Point::from_hash(h1);
  215. hs = Point::from_hash(h1); // double-count
  216. SecureBuffer gx(Precomputed::base() * x + hc);
  217. client.send_plaintext(gx);
  218. server.recv_plaintext(gx);
  219. Scalar y(serverRng);
  220. SecureBuffer gy(Precomputed::base() * y + hs);
  221. server.send_plaintext(gy);
  222. client.recv_plaintext(gy);
  223. server.key(h1);
  224. server.key((Point(gx) - hc)*y);
  225. if(aug) {
  226. /* This step isn't actually online but whatever, it's fastish */
  227. SecureBuffer serverAug(Precomputed::base() * gs);
  228. server.key(Point(serverAug)*y);
  229. }
  230. SecureBuffer tag = server.produce_auth();
  231. client.key(h1);
  232. Point pgy(gy); pgy -= hs;
  233. client.key(pgy*x);
  234. if (aug) client.key(pgy * gs);
  235. client.verify_auth(tag);
  236. tag = client.produce_auth();
  237. client.respec(STROBE_KEYED_128);
  238. /* TODO: fork... */
  239. server.verify_auth(tag);
  240. server.respec(STROBE_KEYED_128);
  241. }
  242. int main(int argc, char **argv) {
  243. bool micro = false;
  244. if (argc >= 2 && !strcmp(argv[1], "--micro"))
  245. micro = true;
  246. decaf_255_public_key_t p1,p2;
  247. decaf_255_private_key_t s1,s2;
  248. decaf_255_symmetric_key_t r1,r2;
  249. decaf_255_signature_t sig1;
  250. unsigned char ss[32];
  251. memset(r1,1,sizeof(r1));
  252. memset(r2,2,sizeof(r2));
  253. unsigned char umessage[] = {1,2,3,4,5};
  254. size_t lmessage = sizeof(umessage);
  255. if (micro) {
  256. Precomputed pBase;
  257. Point p,q;
  258. Scalar s,t;
  259. SecureBuffer ep, ep2(Point::SER_BYTES*2);
  260. SpongeRng rng(Block("micro-benchmarks"));
  261. printf("\nMicro-benchmarks:\n");
  262. SHAKE<128> shake1;
  263. SHAKE<256> shake2;
  264. SHA3<512> sha5;
  265. Strobe strobe(Strobe::CLIENT);
  266. unsigned char b1024[1024] = {1};
  267. for (Benchmark b("SHAKE128 1kiB", 30); b.iter(); ) { shake1 += TmpBuffer(b1024,1024); }
  268. for (Benchmark b("SHAKE256 1kiB", 30); b.iter(); ) { shake2 += TmpBuffer(b1024,1024); }
  269. for (Benchmark b("SHA3-512 1kiB", 30); b.iter(); ) { sha5 += TmpBuffer(b1024,1024); }
  270. strobe.key(TmpBuffer(b1024,1024));
  271. strobe.respec(STROBE_128);
  272. for (Benchmark b("STROBE128 1kiB", 10); b.iter(); ) {
  273. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  274. }
  275. strobe.respec(STROBE_256);
  276. for (Benchmark b("STROBE256 1kiB", 10); b.iter(); ) {
  277. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  278. }
  279. strobe.respec(STROBE_KEYED_128);
  280. for (Benchmark b("STROBEk128 1kiB", 10); b.iter(); ) {
  281. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  282. }
  283. strobe.respec(STROBE_KEYED_256);
  284. for (Benchmark b("STROBEk256 1kiB", 10); b.iter(); ) {
  285. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  286. }
  287. for (Benchmark b("Scalar add", 1000); b.iter(); ) { s+=t; }
  288. for (Benchmark b("Scalar times", 100); b.iter(); ) { s*=t; }
  289. for (Benchmark b("Scalar inv", 1); b.iter(); ) { s.inverse(); }
  290. for (Benchmark b("Point add", 100); b.iter(); ) { p += q; }
  291. for (Benchmark b("Point double", 100); b.iter(); ) { p.double_in_place(); }
  292. for (Benchmark b("Point scalarmul"); b.iter(); ) { p * s; }
  293. for (Benchmark b("Point encode"); b.iter(); ) { ep = SecureBuffer(p); }
  294. for (Benchmark b("Point decode"); b.iter(); ) { p = Point(ep); }
  295. for (Benchmark b("Point create/destroy"); b.iter(); ) { Point r; }
  296. for (Benchmark b("Point hash nonuniform"); b.iter(); ) { Point::from_hash(ep); }
  297. for (Benchmark b("Point hash uniform"); b.iter(); ) { Point::from_hash(ep2); }
  298. for (Benchmark b("Point unhash nonuniform"); b.iter(); ) { ignore_result(p.invert_elligator(ep,0)); }
  299. for (Benchmark b("Point unhash uniform"); b.iter(); ) { ignore_result(p.invert_elligator(ep2,0)); }
  300. for (Benchmark b("Point steg"); b.iter(); ) { p.steg_encode(rng); }
  301. for (Benchmark b("Point double scalarmul"); b.iter(); ) { Point::double_scalarmul(p,s,q,t); }
  302. for (Benchmark b("Point precmp scalarmul"); b.iter(); ) { pBase * s; }
  303. /* TODO: scalarmul for verif, etc */
  304. }
  305. printf("\nMacro-benchmarks:\n");
  306. for (Benchmark b("Keygen"); b.iter(); ) {
  307. decaf_255_derive_private_key(s1,r1);
  308. }
  309. decaf_255_private_to_public(p1,s1);
  310. decaf_255_derive_private_key(s2,r2);
  311. decaf_255_private_to_public(p2,s2);
  312. for (Benchmark b("Shared secret"); b.iter(); ) {
  313. decaf_bool_t ret = decaf_255_shared_secret(ss,sizeof(ss),s1,p2);
  314. ignore_result(ret);
  315. assert(ret);
  316. }
  317. for (Benchmark b("Sign"); b.iter(); ) {
  318. decaf_255_sign(sig1,s1,umessage,lmessage);
  319. }
  320. for (Benchmark b("Verify"); b.iter(); ) {
  321. decaf_bool_t ret = decaf_255_verify(sig1,p1,umessage,lmessage);
  322. umessage[0]++;
  323. umessage[1]^=umessage[0];
  324. ignore_result(ret);
  325. }
  326. printf("\nProtocol benchmarks:\n");
  327. SpongeRng clientRng(Block("client rng seed"));
  328. SpongeRng serverRng(Block("server rng seed"));
  329. SecureBuffer hashedPassword("hello world");
  330. for (Benchmark b("Spake2ee c+s",0.1); b.iter(); ) {
  331. spake2ee(clientRng, serverRng, hashedPassword,false);
  332. }
  333. for (Benchmark b("Spake2ee c+s aug",0.1); b.iter(); ) {
  334. spake2ee(clientRng, serverRng, hashedPassword,true);
  335. }
  336. Scalar x(clientRng);
  337. SecureBuffer gx(Precomputed::base() * x);
  338. Scalar y(serverRng);
  339. SecureBuffer gy(Precomputed::base() * y);
  340. for (Benchmark b("FHMQV c+s",0.1); b.iter(); ) {
  341. fhmqv(clientRng, serverRng,x,gx,y,gy);
  342. }
  343. for (Benchmark b("TripleDH anon c+s",0.1); b.iter(); ) {
  344. tdh(clientRng, serverRng, x,gx,y,gy);
  345. }
  346. printf("\n");
  347. Benchmark::calib();
  348. printf("\n");
  349. return 0;
  350. }