Counter Strike : Global Offensive Source Code
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.

317 lines
12 KiB

  1. // bench2.cpp - written and placed in the public domain by Wei Dai
  2. #include "bench.h"
  3. #include "validate.h"
  4. #include "files.h"
  5. #include "hex.h"
  6. #include "rsa.h"
  7. #include "nr.h"
  8. #include "dsa.h"
  9. #include "luc.h"
  10. #include "rw.h"
  11. #include "eccrypto.h"
  12. #include "ecp.h"
  13. #include "ec2n.h"
  14. #include "asn.h"
  15. #include "dh.h"
  16. #include "mqv.h"
  17. #include "xtrcrypt.h"
  18. #include "esign.h"
  19. #include "pssr.h"
  20. #include "oids.h"
  21. #include "randpool.h"
  22. #include <time.h>
  23. #include <math.h>
  24. #include <iostream>
  25. #include <iomanip>
  26. USING_NAMESPACE(CryptoPP)
  27. USING_NAMESPACE(std)
  28. void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken);
  29. void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal, bool pc=false)
  30. {
  31. unsigned int len = 16;
  32. SecByteBlock plaintext(len), ciphertext(key.CiphertextLength(len));
  33. GlobalRNG().GenerateBlock(plaintext, len);
  34. clock_t start = clock();
  35. unsigned int i;
  36. double timeTaken;
  37. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
  38. key.Encrypt(GlobalRNG(), plaintext, len, ciphertext);
  39. OutputResultOperations(name, "Encryption", pc, i, timeTaken);
  40. if (!pc && key.GetMaterial().SupportsPrecomputation())
  41. {
  42. key.AccessMaterial().Precompute(16);
  43. BenchMarkEncryption(name, key, timeTotal, true);
  44. }
  45. }
  46. void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub, double timeTotal)
  47. {
  48. unsigned int len = 16;
  49. SecByteBlock ciphertext(pub.CiphertextLength(len));
  50. SecByteBlock plaintext(pub.MaxPlaintextLength(ciphertext.size()));
  51. GlobalRNG().GenerateBlock(plaintext, len);
  52. pub.Encrypt(GlobalRNG(), plaintext, len, ciphertext);
  53. clock_t start = clock();
  54. unsigned int i;
  55. double timeTaken;
  56. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
  57. priv.Decrypt(GlobalRNG(), ciphertext, ciphertext.size(), plaintext);
  58. OutputResultOperations(name, "Decryption", false, i, timeTaken);
  59. }
  60. void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool pc=false)
  61. {
  62. unsigned int len = 16;
  63. AlignedSecByteBlock message(len), signature(key.SignatureLength());
  64. GlobalRNG().GenerateBlock(message, len);
  65. clock_t start = clock();
  66. unsigned int i;
  67. double timeTaken;
  68. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
  69. key.SignMessage(GlobalRNG(), message, len, signature);
  70. OutputResultOperations(name, "Signature", pc, i, timeTaken);
  71. if (!pc && key.GetMaterial().SupportsPrecomputation())
  72. {
  73. key.AccessMaterial().Precompute(16);
  74. BenchMarkSigning(name, key, timeTotal, true);
  75. }
  76. }
  77. void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier &pub, double timeTotal, bool pc=false)
  78. {
  79. unsigned int len = 16;
  80. AlignedSecByteBlock message(len), signature(pub.SignatureLength());
  81. GlobalRNG().GenerateBlock(message, len);
  82. priv.SignMessage(GlobalRNG(), message, len, signature);
  83. clock_t start = clock();
  84. unsigned int i;
  85. double timeTaken;
  86. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
  87. pub.VerifyMessage(message, len, signature, signature.size());
  88. OutputResultOperations(name, "Verification", pc, i, timeTaken);
  89. if (!pc && pub.GetMaterial().SupportsPrecomputation())
  90. {
  91. pub.AccessMaterial().Precompute(16);
  92. BenchMarkVerification(name, priv, pub, timeTotal, true);
  93. }
  94. }
  95. void BenchMarkKeyGen(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
  96. {
  97. SecByteBlock priv(d.PrivateKeyLength()), pub(d.PublicKeyLength());
  98. clock_t start = clock();
  99. unsigned int i;
  100. double timeTaken;
  101. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
  102. d.GenerateKeyPair(GlobalRNG(), priv, pub);
  103. OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
  104. if (!pc && d.GetMaterial().SupportsPrecomputation())
  105. {
  106. d.AccessMaterial().Precompute(16);
  107. BenchMarkKeyGen(name, d, timeTotal, true);
  108. }
  109. }
  110. void BenchMarkKeyGen(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
  111. {
  112. SecByteBlock priv(d.EphemeralPrivateKeyLength()), pub(d.EphemeralPublicKeyLength());
  113. clock_t start = clock();
  114. unsigned int i;
  115. double timeTaken;
  116. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
  117. d.GenerateEphemeralKeyPair(GlobalRNG(), priv, pub);
  118. OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
  119. if (!pc && d.GetMaterial().SupportsPrecomputation())
  120. {
  121. d.AccessMaterial().Precompute(16);
  122. BenchMarkKeyGen(name, d, timeTotal, true);
  123. }
  124. }
  125. void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
  126. {
  127. SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
  128. SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
  129. d.GenerateKeyPair(GlobalRNG(), priv1, pub1);
  130. d.GenerateKeyPair(GlobalRNG(), priv2, pub2);
  131. SecByteBlock val(d.AgreedValueLength());
  132. clock_t start = clock();
  133. unsigned int i;
  134. double timeTaken;
  135. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
  136. {
  137. d.Agree(val, priv1, pub2);
  138. d.Agree(val, priv2, pub1);
  139. }
  140. OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
  141. }
  142. void BenchMarkAgreement(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
  143. {
  144. SecByteBlock spriv1(d.StaticPrivateKeyLength()), spriv2(d.StaticPrivateKeyLength());
  145. SecByteBlock epriv1(d.EphemeralPrivateKeyLength()), epriv2(d.EphemeralPrivateKeyLength());
  146. SecByteBlock spub1(d.StaticPublicKeyLength()), spub2(d.StaticPublicKeyLength());
  147. SecByteBlock epub1(d.EphemeralPublicKeyLength()), epub2(d.EphemeralPublicKeyLength());
  148. d.GenerateStaticKeyPair(GlobalRNG(), spriv1, spub1);
  149. d.GenerateStaticKeyPair(GlobalRNG(), spriv2, spub2);
  150. d.GenerateEphemeralKeyPair(GlobalRNG(), epriv1, epub1);
  151. d.GenerateEphemeralKeyPair(GlobalRNG(), epriv2, epub2);
  152. SecByteBlock val(d.AgreedValueLength());
  153. clock_t start = clock();
  154. unsigned int i;
  155. double timeTaken;
  156. for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
  157. {
  158. d.Agree(val, spriv1, epriv1, spub2, epub2);
  159. d.Agree(val, spriv2, epriv2, spub1, epub1);
  160. }
  161. OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
  162. }
  163. //VC60 workaround: compiler bug triggered without the extra dummy parameters
  164. template <class SCHEME>
  165. void BenchMarkCrypto(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
  166. {
  167. FileSource f(filename, true, new HexDecoder());
  168. typename SCHEME::Decryptor priv(f);
  169. typename SCHEME::Encryptor pub(priv);
  170. BenchMarkEncryption(name, pub, timeTotal);
  171. BenchMarkDecryption(name, priv, pub, timeTotal);
  172. }
  173. //VC60 workaround: compiler bug triggered without the extra dummy parameters
  174. template <class SCHEME>
  175. void BenchMarkSignature(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
  176. {
  177. FileSource f(filename, true, new HexDecoder());
  178. typename SCHEME::Signer priv(f);
  179. typename SCHEME::Verifier pub(priv);
  180. BenchMarkSigning(name, priv, timeTotal);
  181. BenchMarkVerification(name, priv, pub, timeTotal);
  182. }
  183. //VC60 workaround: compiler bug triggered without the extra dummy parameters
  184. template <class D>
  185. void BenchMarkKeyAgreement(const char *filename, const char *name, double timeTotal, D *x=NULL)
  186. {
  187. FileSource f(filename, true, new HexDecoder());
  188. D d(f);
  189. BenchMarkKeyGen(name, d, timeTotal);
  190. BenchMarkAgreement(name, d, timeTotal);
  191. }
  192. extern double g_hertz;
  193. void BenchmarkAll2(double t, double hertz)
  194. {
  195. g_hertz = hertz;
  196. #if 0
  197. cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right>" << endl;
  198. cout << "<THEAD><TR><TH>Operation<TH>Milliseconds/Operation" << (g_hertz ? "<TH>Megacycles/Operation" : "") << endl;
  199. cout << "\n<TBODY style=\"background: yellow\">";
  200. BenchMarkCrypto<RSAES<OAEP<SHA> > >("TestData/rsa1024.dat", "RSA 1024", t);
  201. BenchMarkCrypto<LUCES<OAEP<SHA> > >("TestData/luc1024.dat", "LUC 1024", t);
  202. BenchMarkCrypto<DLIES<> >("TestData/dlie1024.dat", "DLIES 1024", t);
  203. BenchMarkCrypto<LUC_IES<> >("TestData/lucc512.dat", "LUCELG 512", t);
  204. cout << "\n<TBODY style=\"background: white\">";
  205. BenchMarkCrypto<RSAES<OAEP<SHA> > >("TestData/rsa2048.dat", "RSA 2048", t);
  206. BenchMarkCrypto<LUCES<OAEP<SHA> > >("TestData/luc2048.dat", "LUC 2048", t);
  207. BenchMarkCrypto<DLIES<> >("TestData/dlie2048.dat", "DLIES 2048", t);
  208. BenchMarkCrypto<LUC_IES<> >("TestData/lucc1024.dat", "LUCELG 1024", t);
  209. cout << "\n<TBODY style=\"background: yellow\">";
  210. BenchMarkSignature<RSASS<PSSR, SHA> >("TestData/rsa1024.dat", "RSA 1024", t);
  211. BenchMarkSignature<RWSS<PSSR, SHA> >("TestData/rw1024.dat", "RW 1024", t);
  212. BenchMarkSignature<LUCSS<PSSR, SHA> >("TestData/luc1024.dat", "LUC 1024", t);
  213. BenchMarkSignature<NR<SHA> >("TestData/nr1024.dat", "NR 1024", t);
  214. BenchMarkSignature<DSA>("TestData/dsa1024.dat", "DSA 1024", t);
  215. BenchMarkSignature<LUC_HMP<SHA> >("TestData/lucs512.dat", "LUC-HMP 512", t);
  216. BenchMarkSignature<ESIGN<SHA> >("TestData/esig1023.dat", "ESIGN 1023", t);
  217. BenchMarkSignature<ESIGN<SHA> >("TestData/esig1536.dat", "ESIGN 1536", t);
  218. cout << "\n<TBODY style=\"background: white\">";
  219. BenchMarkSignature<RSASS<PSSR, SHA> >("TestData/rsa2048.dat", "RSA 2048", t);
  220. BenchMarkSignature<RWSS<PSSR, SHA> >("TestData/rw2048.dat", "RW 2048", t);
  221. BenchMarkSignature<LUCSS<PSSR, SHA> >("TestData/luc2048.dat", "LUC 2048", t);
  222. BenchMarkSignature<NR<SHA> >("TestData/nr2048.dat", "NR 2048", t);
  223. BenchMarkSignature<LUC_HMP<SHA> >("TestData/lucs1024.dat", "LUC-HMP 1024", t);
  224. BenchMarkSignature<ESIGN<SHA> >("TestData/esig2046.dat", "ESIGN 2046", t);
  225. cout << "\n<TBODY style=\"background: yellow\">";
  226. BenchMarkKeyAgreement<XTR_DH>("TestData/xtrdh171.dat", "XTR-DH 171", t);
  227. BenchMarkKeyAgreement<XTR_DH>("TestData/xtrdh342.dat", "XTR-DH 342", t);
  228. BenchMarkKeyAgreement<DH>("TestData/dh1024.dat", "DH 1024", t);
  229. BenchMarkKeyAgreement<DH>("TestData/dh2048.dat", "DH 2048", t);
  230. BenchMarkKeyAgreement<LUC_DH>("TestData/lucd512.dat", "LUCDIF 512", t);
  231. BenchMarkKeyAgreement<LUC_DH>("TestData/lucd1024.dat", "LUCDIF 1024", t);
  232. BenchMarkKeyAgreement<MQV>("TestData/mqv1024.dat", "MQV 1024", t);
  233. BenchMarkKeyAgreement<MQV>("TestData/mqv2048.dat", "MQV 2048", t);
  234. #endif
  235. cout << "\n<TBODY style=\"background: white\">";
  236. {
  237. ECIES<ECP>::Decryptor cpriv(GlobalRNG(), ASN1::secp256k1());
  238. ECIES<ECP>::Encryptor cpub(cpriv);
  239. ECDSA<ECP, SHA>::Signer spriv(cpriv);
  240. ECDSA<ECP, SHA>::Verifier spub(spriv);
  241. ECDH<ECP>::Domain ecdhc(ASN1::secp256k1());
  242. ECMQV<ECP>::Domain ecmqvc(ASN1::secp256k1());
  243. BenchMarkEncryption("ECIES over GF(p) 256", cpub, t);
  244. BenchMarkDecryption("ECIES over GF(p) 256", cpriv, cpub, t);
  245. BenchMarkSigning("ECDSA over GF(p) 256", spriv, t);
  246. BenchMarkVerification("ECDSA over GF(p) 256", spriv, spub, t);
  247. BenchMarkKeyGen("ECDHC over GF(p) 256", ecdhc, t);
  248. BenchMarkAgreement("ECDHC over GF(p) 256", ecdhc, t);
  249. BenchMarkKeyGen("ECMQVC over GF(p) 256", ecmqvc, t);
  250. BenchMarkAgreement("ECMQVC over GF(p) 256", ecmqvc, t);
  251. }
  252. cout << "<TBODY style=\"background: yellow\">" << endl;
  253. {
  254. ECIES<EC2N>::Decryptor cpriv(GlobalRNG(), ASN1::sect233r1());
  255. ECIES<EC2N>::Encryptor cpub(cpriv);
  256. ECDSA<EC2N, SHA>::Signer spriv(cpriv);
  257. ECDSA<EC2N, SHA>::Verifier spub(spriv);
  258. ECDH<EC2N>::Domain ecdhc(ASN1::sect233r1());
  259. ECMQV<EC2N>::Domain ecmqvc(ASN1::sect233r1());
  260. BenchMarkEncryption("ECIES over GF(2^n) 233", cpub, t);
  261. BenchMarkDecryption("ECIES over GF(2^n) 233", cpriv, cpub, t);
  262. BenchMarkSigning("ECDSA over GF(2^n) 233", spriv, t);
  263. BenchMarkVerification("ECDSA over GF(2^n) 233", spriv, spub, t);
  264. BenchMarkKeyGen("ECDHC over GF(2^n) 233", ecdhc, t);
  265. BenchMarkAgreement("ECDHC over GF(2^n) 233", ecdhc, t);
  266. BenchMarkKeyGen("ECMQVC over GF(2^n) 233", ecmqvc, t);
  267. BenchMarkAgreement("ECMQVC over GF(2^n) 233", ecmqvc, t);
  268. }
  269. cout << "</TABLE>" << endl;
  270. }