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.
 
 
 
 
 

404 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. public:
  66. int i, j, ntests, nsamples;
  67. double begin;
  68. uint64_t tsc_begin;
  69. std::vector<double> times;
  70. std::vector<uint64_t> cycles;
  71. Benchmark(const char *s, double factor = 1) {
  72. printf("%s:", s);
  73. if (strlen(s) < 25) printf("%*s",int(25-strlen(s)),"");
  74. fflush(stdout);
  75. i = j = 0;
  76. ntests = NTESTS * factor;
  77. nsamples = NSAMPLES;
  78. begin = now();
  79. tsc_begin = rdtsc();
  80. times = std::vector<double>(NSAMPLES);
  81. cycles = std::vector<uint64_t>(NSAMPLES);
  82. }
  83. ~Benchmark() {
  84. double tsc = 0;
  85. double t = 0;
  86. std::sort(times.begin(), times.end());
  87. std::sort(cycles.begin(), cycles.end());
  88. for (int k=DISCARD; k<nsamples-DISCARD; k++) {
  89. tsc += cycles[k];
  90. t += times[k];
  91. }
  92. totalCy += tsc;
  93. totalS += t;
  94. t /= ntests*(nsamples-2*DISCARD);
  95. tsc /= ntests*(nsamples-2*DISCARD);
  96. printSI(t,"s");
  97. printf(" ");
  98. printSI(1/t,"/s");
  99. if (tsc) { printf(" "); printSI(tsc, "cy"); }
  100. printf("\n");
  101. }
  102. inline bool iter() {
  103. i++;
  104. if (i >= ntests) {
  105. uint64_t tsc = rdtsc() - tsc_begin;
  106. double t = now() - begin;
  107. begin += t;
  108. tsc_begin += tsc;
  109. assert(j >= 0 && j < nsamples);
  110. cycles[j] = tsc;
  111. times[j] = t;
  112. j++;
  113. i = 0;
  114. }
  115. return j < nsamples;
  116. }
  117. static void calib() {
  118. if (totalS && totalCy) {
  119. const char *s = "Cycle calibration";
  120. printf("%s:", s);
  121. if (strlen(s) < 25) printf("%*s",int(25-strlen(s)),"");
  122. printSI(totalCy / totalS, "Hz");
  123. printf("\n");
  124. }
  125. }
  126. };
  127. double Benchmark::totalCy = 0, Benchmark::totalS = 0;
  128. static void tdh (
  129. SpongeRng &clientRng,
  130. SpongeRng &serverRng,
  131. Scalar x, const Block &gx,
  132. Scalar y, const Block &gy
  133. ) {
  134. Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
  135. Scalar xe(clientRng);
  136. SecureBuffer gxe = Precomputed::base() * xe;
  137. client.send_plaintext(gxe);
  138. server.recv_plaintext(gxe);
  139. Scalar ye(serverRng);
  140. SecureBuffer gye = Precomputed::base() * ye;
  141. server.send_plaintext(gye);
  142. client.recv_plaintext(gye);
  143. Point pgxe(gxe);
  144. server.key(pgxe*ye);
  145. SecureBuffer tag1 = server.produce_auth();
  146. SecureBuffer ct = server.encrypt(gy);
  147. server.key(pgxe*y);
  148. SecureBuffer tag2 = server.produce_auth();
  149. Point pgye(gye);
  150. client.key(pgye*xe);
  151. client.verify_auth(tag1);
  152. client.key(Point(client.decrypt(ct)) * xe);
  153. client.verify_auth(tag2);
  154. ct = client.encrypt(gx);
  155. client.key(pgye * x);
  156. tag1 = client.produce_auth();
  157. client.respec(STROBE_KEYED_128);
  158. server.key(Point(server.decrypt(ct)) * ye);
  159. server.verify_auth(tag1);
  160. server.respec(STROBE_KEYED_128);
  161. }
  162. static void fhmqv (
  163. SpongeRng &clientRng,
  164. SpongeRng &serverRng,
  165. Scalar x, const Block &gx,
  166. Scalar y, const Block &gy
  167. ) {
  168. /* Don't use this, it's probably patented */
  169. Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
  170. Scalar xe(clientRng);
  171. client.send_plaintext(gx);
  172. server.recv_plaintext(gx);
  173. SecureBuffer gxe = Precomputed::base() * xe;
  174. server.send_plaintext(gxe);
  175. client.recv_plaintext(gxe);
  176. Scalar ye(serverRng);
  177. server.send_plaintext(gy);
  178. client.recv_plaintext(gy);
  179. SecureBuffer gye = Precomputed::base() * ye;
  180. server.send_plaintext(gye);
  181. Scalar schx(server.prng(Scalar::SER_BYTES));
  182. Scalar schy(server.prng(Scalar::SER_BYTES));
  183. Scalar yec = y + ye*schy;
  184. server.key(Point::double_scalarmul(Point(gx),yec,Point(gxe),yec*schx));
  185. SecureBuffer as = server.produce_auth();
  186. client.recv_plaintext(gye);
  187. Scalar cchx(client.prng(Scalar::SER_BYTES));
  188. Scalar cchy(client.prng(Scalar::SER_BYTES));
  189. Scalar xec = x + xe*schx;
  190. client.key(Point::double_scalarmul(Point(gy),xec,Point(gye),xec*schy));
  191. client.verify_auth(as);
  192. SecureBuffer ac = client.produce_auth();
  193. client.respec(STROBE_KEYED_128);
  194. server.verify_auth(ac);
  195. server.respec(STROBE_KEYED_128);
  196. }
  197. static void spake2ee(
  198. SpongeRng &clientRng,
  199. SpongeRng &serverRng,
  200. const Block &hashed_password,
  201. bool aug
  202. ) {
  203. Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
  204. Scalar x(clientRng);
  205. SHAKE<256> shake;
  206. shake.update(hashed_password);
  207. SecureBuffer h0 = shake.output(Point::HASH_BYTES);
  208. SecureBuffer h1 = shake.output(Point::HASH_BYTES);
  209. SecureBuffer h2 = shake.output(Scalar::SER_BYTES);
  210. Scalar gs(h2);
  211. Point hc = Point::from_hash(h0);
  212. hc = Point::from_hash(h0); // double-count
  213. Point hs = Point::from_hash(h1);
  214. hs = Point::from_hash(h1); // double-count
  215. SecureBuffer gx(Precomputed::base() * x + hc);
  216. client.send_plaintext(gx);
  217. server.recv_plaintext(gx);
  218. Scalar y(serverRng);
  219. SecureBuffer gy(Precomputed::base() * y + hs);
  220. server.send_plaintext(gy);
  221. client.recv_plaintext(gy);
  222. server.key(h1);
  223. server.key((Point(gx) - hc)*y);
  224. if(aug) {
  225. /* This step isn't actually online but whatever, it's fastish */
  226. SecureBuffer serverAug(Precomputed::base() * gs);
  227. server.key(Point(serverAug)*y);
  228. }
  229. SecureBuffer tag = server.produce_auth();
  230. client.key(h1);
  231. Point pgy(gy); pgy -= hs;
  232. client.key(pgy*x);
  233. if (aug) client.key(pgy * gs);
  234. client.verify_auth(tag);
  235. tag = client.produce_auth();
  236. client.respec(STROBE_KEYED_128);
  237. /* TODO: fork... */
  238. server.verify_auth(tag);
  239. server.respec(STROBE_KEYED_128);
  240. }
  241. int main(int argc, char **argv) {
  242. bool micro = false;
  243. if (argc >= 2 && !strcmp(argv[1], "--micro"))
  244. micro = true;
  245. decaf_255_public_key_t p1,p2;
  246. decaf_255_private_key_t s1,s2;
  247. decaf_255_symmetric_key_t r1,r2;
  248. decaf_255_signature_t sig1;
  249. unsigned char ss[32];
  250. memset(r1,1,sizeof(r1));
  251. memset(r2,2,sizeof(r2));
  252. unsigned char umessage[] = {1,2,3,4,5};
  253. size_t lmessage = sizeof(umessage);
  254. if (micro) {
  255. Precomputed pBase;
  256. Point p,q;
  257. Scalar s,t;
  258. SecureBuffer ep, ep2(Point::SER_BYTES*2);
  259. SpongeRng rng(Block("micro-benchmarks"));
  260. printf("\nMicro-benchmarks:\n");
  261. SHAKE<128> shake1;
  262. SHAKE<256> shake2;
  263. SHA3<512> sha5;
  264. Strobe strobe(Strobe::CLIENT);
  265. unsigned char b1024[1024] = {1};
  266. for (Benchmark b("SHAKE128 1kiB", 30); b.iter(); ) { shake1 += TmpBuffer(b1024,1024); }
  267. for (Benchmark b("SHAKE256 1kiB", 30); b.iter(); ) { shake2 += TmpBuffer(b1024,1024); }
  268. for (Benchmark b("SHA3-512 1kiB", 30); b.iter(); ) { sha5 += TmpBuffer(b1024,1024); }
  269. strobe.key(TmpBuffer(b1024,1024));
  270. strobe.respec(STROBE_128);
  271. for (Benchmark b("STROBE128 1kiB", 10); b.iter(); ) {
  272. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  273. }
  274. strobe.respec(STROBE_256);
  275. for (Benchmark b("STROBE256 1kiB", 10); b.iter(); ) {
  276. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  277. }
  278. strobe.respec(STROBE_KEYED_128);
  279. for (Benchmark b("STROBEk128 1kiB", 10); b.iter(); ) {
  280. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  281. }
  282. strobe.respec(STROBE_KEYED_256);
  283. for (Benchmark b("STROBEk256 1kiB", 10); b.iter(); ) {
  284. strobe.encrypt_no_auth(TmpBuffer(b1024,1024),TmpBuffer(b1024,1024),b.i>1);
  285. }
  286. for (Benchmark b("Scalar add", 1000); b.iter(); ) { s+=t; }
  287. for (Benchmark b("Scalar times", 100); b.iter(); ) { s*=t; }
  288. for (Benchmark b("Scalar inv", 1); b.iter(); ) { s.inverse(); }
  289. for (Benchmark b("Point add", 100); b.iter(); ) { p += q; }
  290. for (Benchmark b("Point double", 100); b.iter(); ) { p.double_in_place(); }
  291. for (Benchmark b("Point scalarmul"); b.iter(); ) { p * s; }
  292. for (Benchmark b("Point encode"); b.iter(); ) { ep = SecureBuffer(p); }
  293. for (Benchmark b("Point decode"); b.iter(); ) { p = Point(ep); }
  294. for (Benchmark b("Point create/destroy"); b.iter(); ) { Point r; }
  295. for (Benchmark b("Point hash nonuniform"); b.iter(); ) { Point::from_hash(ep); }
  296. for (Benchmark b("Point hash uniform"); b.iter(); ) { Point::from_hash(ep2); }
  297. for (Benchmark b("Point unhash nonuniform"); b.iter(); ) { ignore_result(p.invert_elligator(ep,0)); }
  298. for (Benchmark b("Point unhash uniform"); b.iter(); ) { ignore_result(p.invert_elligator(ep2,0)); }
  299. for (Benchmark b("Point steg"); b.iter(); ) { p.steg_encode(rng); }
  300. for (Benchmark b("Point double scalarmul"); b.iter(); ) { Point::double_scalarmul(p,s,q,t); }
  301. for (Benchmark b("Point precmp scalarmul"); b.iter(); ) { pBase * s; }
  302. /* TODO: scalarmul for verif, etc */
  303. }
  304. printf("\nMacro-benchmarks:\n");
  305. for (Benchmark b("Keygen"); b.iter(); ) {
  306. decaf_255_derive_private_key(s1,r1);
  307. }
  308. decaf_255_private_to_public(p1,s1);
  309. decaf_255_derive_private_key(s2,r2);
  310. decaf_255_private_to_public(p2,s2);
  311. for (Benchmark b("Shared secret"); b.iter(); ) {
  312. decaf_bool_t ret = decaf_255_shared_secret(ss,sizeof(ss),s1,p2);
  313. ignore_result(ret);
  314. assert(ret);
  315. }
  316. for (Benchmark b("Sign"); b.iter(); ) {
  317. decaf_255_sign(sig1,s1,umessage,lmessage);
  318. }
  319. for (Benchmark b("Verify"); b.iter(); ) {
  320. decaf_bool_t ret = decaf_255_verify(sig1,p1,umessage,lmessage);
  321. umessage[0]++;
  322. umessage[1]^=umessage[0];
  323. ignore_result(ret);
  324. }
  325. printf("\nProtocol benchmarks:\n");
  326. SpongeRng clientRng(Block("client rng seed"));
  327. SpongeRng serverRng(Block("server rng seed"));
  328. SecureBuffer hashedPassword("hello world");
  329. for (Benchmark b("Spake2ee c+s",0.1); b.iter(); ) {
  330. spake2ee(clientRng, serverRng, hashedPassword,false);
  331. }
  332. for (Benchmark b("Spake2ee c+s aug",0.1); b.iter(); ) {
  333. spake2ee(clientRng, serverRng, hashedPassword,true);
  334. }
  335. Scalar x(clientRng);
  336. SecureBuffer gx(Precomputed::base() * x);
  337. Scalar y(serverRng);
  338. SecureBuffer gy(Precomputed::base() * y);
  339. for (Benchmark b("FHMQV c+s",0.1); b.iter(); ) {
  340. fhmqv(clientRng, serverRng,x,gx,y,gy);
  341. }
  342. for (Benchmark b("TripleDH anon c+s",0.1); b.iter(); ) {
  343. tdh(clientRng, serverRng, x,gx,y,gy);
  344. }
  345. printf("\n");
  346. Benchmark::calib();
  347. printf("\n");
  348. return 0;
  349. }