1
0

CryptoAuth_benchmark.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "crypto/test/Exports.h"
  18. #include "io/FileWriter.h"
  19. #include "benc/Object.h"
  20. #include "memory/MallocAllocator.h"
  21. #include "util/Bits.h"
  22. #include "util/Hex.h"
  23. #include "util/Endian.h"
  24. #include "util/events/Time.h"
  25. #include "util/events/EventBase.h"
  26. #include "wire/Error.h"
  27. #include "util/Assert.h"
  28. #include <stdio.h>
  29. static const uint8_t* privateKey = (uint8_t*)
  30. "\x20\xca\x45\xd9\x5b\xbf\xca\xe7\x35\x3c\xd2\xdf\xfa\x12\x84\x4b"
  31. "\x4e\xff\xbe\x7d\x39\xd8\x4d\x8e\x14\x2b\x9d\x21\x89\x5b\x38\x09";
  32. static const uint8_t* publicKey = (uint8_t*)
  33. "\x51\xaf\x8d\xd9\x35\xe8\x61\x86\x3e\x94\x2b\x1b\x6d\x21\x22\xe0"
  34. "\x2f\xb2\xd0\x88\x20\xbb\xf3\xf0\x6f\xcd\xe5\x85\x30\xe0\x08\x34";
  35. struct Context
  36. {
  37. uint8_t padding[256];
  38. uint8_t buffer[2048];
  39. struct Message message;
  40. struct CryptoAuth* ca1;
  41. struct CryptoAuth* ca2;
  42. struct Interface if1;
  43. struct Interface* cif1;
  44. struct Message* if1Incoming;
  45. struct Interface if2;
  46. struct Interface* cif2;
  47. struct Message* if2Incoming;
  48. struct EventBase* base;
  49. };
  50. static inline uint8_t transferMessage(struct Message* message, struct Interface* iface)
  51. {
  52. struct Interface* otherIface = iface->senderContext;
  53. return otherIface->receiveMessage(message, otherIface);
  54. }
  55. static inline void setupMessage(struct Context* ctx, uint16_t length)
  56. {
  57. ctx->message.bytes = ctx->buffer;
  58. ctx->message.padding = 256;
  59. ctx->message.length = length;
  60. }
  61. static const char* KEY = "key";
  62. static const char* HELLO = "hello";
  63. static const char* TRAFFIC = "data";
  64. static inline void sendMessages(struct Context* ctx,
  65. int count,
  66. int size,
  67. const char* type,
  68. bool authPackets)
  69. {
  70. printf("Test sending %d %d byte\t%s packets.\tpacket auth %s",
  71. count, size, type, ((authPackets) ? "enabled." : "disabled."));
  72. ctx->cif2 = CryptoAuth_wrapInterface(&ctx->if2, NULL, false, authPackets, ctx->ca2);
  73. ctx->cif1 = CryptoAuth_wrapInterface(&ctx->if1, publicKey, false, authPackets, ctx->ca1);
  74. uint64_t startTime = Time_hrtime();
  75. if (type != HELLO) {
  76. setupMessage(ctx, size);
  77. ctx->cif1->sendMessage(&ctx->message, ctx->cif1);
  78. if (type == TRAFFIC) {
  79. setupMessage(ctx, size);
  80. ctx->cif2->sendMessage(&ctx->message, ctx->cif2);
  81. }
  82. }
  83. for (int i = 0; i < count; i++) {
  84. setupMessage(ctx, size);
  85. if (type == KEY) {
  86. ctx->cif2->sendMessage(&ctx->message, ctx->cif2);
  87. } else {
  88. ctx->cif1->sendMessage(&ctx->message, ctx->cif1);
  89. }
  90. }
  91. uint64_t endTimes = Time_hrtime();
  92. uint64_t time = (endTimes - startTime) / 1000000;
  93. uint64_t kbSent = (size * count * 8) / 1024;
  94. // same as kbSent / (time / 1024) (converting time to seconds)
  95. uint64_t kbps = (kbSent * 1024) / time;
  96. printf("\tFinished in %dms. %d Kb/s\n\n", (int)time, (int)kbps);
  97. }
  98. void CryptoAuth_benchmark(struct EventBase* base,
  99. struct Log* logger,
  100. struct Allocator* alloc)
  101. {
  102. struct Random* rand = Random_new(alloc, logger, NULL);
  103. struct Context ctx = {
  104. .ca1 = CryptoAuth_new(alloc, NULL, base, NULL, rand),
  105. .ca2 = CryptoAuth_new(alloc, privateKey, base, NULL, rand),
  106. .if1 = {
  107. .sendMessage = transferMessage,
  108. .senderContext = &ctx.if2,
  109. .allocator = alloc
  110. },
  111. .if2 = {
  112. .sendMessage = transferMessage,
  113. .senderContext = &ctx.if1,
  114. .allocator = alloc
  115. },
  116. .base = base
  117. };
  118. printf("These metrics are speed of encryption and decryption similar to the usage pattern\n"
  119. "when decrypting a packet, switching it, and re-encrypting it with another key.\n");
  120. sendMessages(&ctx, 1000, 64, HELLO, true);
  121. sendMessages(&ctx, 1000, 1500, HELLO, true);
  122. sendMessages(&ctx, 1000, 64, KEY, true);
  123. sendMessages(&ctx, 1000, 1500, KEY, true);
  124. sendMessages(&ctx, 100000, 64, TRAFFIC, false);
  125. sendMessages(&ctx, 100000, 1500, TRAFFIC, false);
  126. sendMessages(&ctx, 100000, 64, TRAFFIC, true);
  127. printf("This is the switch configuration so this indicates expected switch throughput:\n");
  128. sendMessages(&ctx, 100000, 1500, TRAFFIC, true);
  129. }