CryptoAuth_benchmark.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "crypto/CryptoAuth.h"
  17. #include "io/FileWriter.h"
  18. #include "benc/Object.h"
  19. #include "memory/MallocAllocator.h"
  20. #include "util/Bits.h"
  21. #include "util/Hex.h"
  22. #include "util/Endian.h"
  23. #include "util/events/Time.h"
  24. #include "util/events/EventBase.h"
  25. #include "wire/Error.h"
  26. #include "util/Assert.h"
  27. #include <stdio.h>
  28. static const uint8_t* privateKey = (uint8_t*)
  29. "\x20\xca\x45\xd9\x5b\xbf\xca\xe7\x35\x3c\xd2\xdf\xfa\x12\x84\x4b"
  30. "\x4e\xff\xbe\x7d\x39\xd8\x4d\x8e\x14\x2b\x9d\x21\x89\x5b\x38\x09";
  31. static const uint8_t* publicKey = (uint8_t*)
  32. "\x51\xaf\x8d\xd9\x35\xe8\x61\x86\x3e\x94\x2b\x1b\x6d\x21\x22\xe0"
  33. "\x2f\xb2\xd0\x88\x20\xbb\xf3\xf0\x6f\xcd\xe5\x85\x30\xe0\x08\x34";
  34. struct Context
  35. {
  36. uint8_t padding[256];
  37. uint8_t buffer[2048];
  38. struct Message message;
  39. struct CryptoAuth* ca1;
  40. struct CryptoAuth* ca2;
  41. struct Interface if1;
  42. struct Interface* cif1;
  43. struct Message* if1Incoming;
  44. struct Interface if2;
  45. struct Interface* cif2;
  46. struct Message* if2Incoming;
  47. struct EventBase* base;
  48. };
  49. static inline uint8_t transferMessage(struct Message* message, struct Interface* iface)
  50. {
  51. struct Interface* otherIface = iface->senderContext;
  52. return otherIface->receiveMessage(message, otherIface);
  53. }
  54. static inline void setupMessage(struct Context* ctx, uint16_t length)
  55. {
  56. ctx->message.bytes = ctx->buffer;
  57. ctx->message.padding = 256;
  58. ctx->message.length = length;
  59. }
  60. static const char* KEY = "key";
  61. static const char* HELLO = "hello";
  62. static const char* TRAFFIC = "data";
  63. static inline void sendMessages(struct Context* ctx,
  64. int count,
  65. int size,
  66. const char* type)
  67. {
  68. printf("Test sending %d %d byte\t%s packets\n", count, size, type);
  69. ctx->cif2 = CryptoAuth_wrapInterface(&ctx->if2, NULL, NULL, false, "cif2", ctx->ca2);
  70. ctx->cif1 = CryptoAuth_wrapInterface(&ctx->if1, publicKey, NULL, false, "cif1", ctx->ca1);
  71. uint64_t startTime = Time_hrtime();
  72. if (type != HELLO) {
  73. setupMessage(ctx, size);
  74. ctx->cif1->sendMessage(&ctx->message, ctx->cif1);
  75. if (type == TRAFFIC) {
  76. setupMessage(ctx, size);
  77. ctx->cif2->sendMessage(&ctx->message, ctx->cif2);
  78. }
  79. }
  80. for (int i = 0; i < count; i++) {
  81. setupMessage(ctx, size);
  82. if (type == KEY) {
  83. ctx->cif2->sendMessage(&ctx->message, ctx->cif2);
  84. } else {
  85. ctx->cif1->sendMessage(&ctx->message, ctx->cif1);
  86. }
  87. }
  88. uint64_t endTimes = Time_hrtime();
  89. uint64_t time = (endTimes - startTime) / 1000000;
  90. uint64_t kbSent = (size * count * 8) / 1024;
  91. // same as kbSent / (time / 1024) (converting time to seconds)
  92. uint64_t kbps = (kbSent * 1024) / time;
  93. printf("\tFinished in %dms. %d Kb/s\n\n", (int)time, (int)kbps);
  94. }
  95. void CryptoAuth_benchmark(struct EventBase* base,
  96. struct Log* logger,
  97. struct Allocator* alloc)
  98. {
  99. struct Random* rand = Random_new(alloc, logger, NULL);
  100. struct Context ctx = {
  101. .ca1 = CryptoAuth_new(alloc, NULL, base, NULL, rand),
  102. .ca2 = CryptoAuth_new(alloc, privateKey, base, NULL, rand),
  103. .if1 = {
  104. .sendMessage = transferMessage,
  105. .senderContext = &ctx.if2,
  106. .allocator = alloc
  107. },
  108. .if2 = {
  109. .sendMessage = transferMessage,
  110. .senderContext = &ctx.if1,
  111. .allocator = alloc
  112. },
  113. .base = base
  114. };
  115. printf("These metrics are speed of encryption and decryption similar to the usage pattern\n"
  116. "when decrypting a packet, switching it, and re-encrypting it with another key.\n");
  117. sendMessages(&ctx, 1000, 64, HELLO);
  118. sendMessages(&ctx, 1000, 1500, HELLO);
  119. sendMessages(&ctx, 1000, 64, KEY);
  120. sendMessages(&ctx, 1000, 1500, KEY);
  121. sendMessages(&ctx, 100000, 64, TRAFFIC);
  122. printf("This is the switch configuration so this indicates expected switch throughput:\n");
  123. sendMessages(&ctx, 100000, 1500, TRAFFIC);
  124. }