bencryption_bench.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @file bencryption_bench.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdint.h>
  26. #include <limits.h>
  27. #include <misc/balloc.h>
  28. #include <security/BRandom.h>
  29. #include <security/BEncryption.h>
  30. #include <base/DebugObject.h>
  31. static void usage (char *name)
  32. {
  33. printf(
  34. "Usage: %s <enc/dec> <ciper> <num_blocks> <num_ops>\n"
  35. " <cipher> is one of (blowfish, aes).\n",
  36. name
  37. );
  38. exit(1);
  39. }
  40. int main (int argc, char **argv)
  41. {
  42. if (argc <= 0) {
  43. return 1;
  44. }
  45. if (argc != 5) {
  46. usage(argv[0]);
  47. }
  48. char *mode_str = argv[1];
  49. char *cipher_str = argv[2];
  50. int mode;
  51. int cipher;
  52. int num_blocks = atoi(argv[3]);
  53. int num_ops = atoi(argv[4]);
  54. if (!strcmp(mode_str, "enc")) {
  55. mode = BENCRYPTION_MODE_ENCRYPT;
  56. }
  57. else if (!strcmp(mode_str, "dec")) {
  58. mode = BENCRYPTION_MODE_DECRYPT;
  59. }
  60. else {
  61. usage(argv[0]);
  62. }
  63. if (!strcmp(cipher_str, "blowfish")) {
  64. cipher = BENCRYPTION_CIPHER_BLOWFISH;
  65. }
  66. else if (!strcmp(cipher_str, "aes")) {
  67. cipher = BENCRYPTION_CIPHER_AES;
  68. }
  69. else {
  70. usage(argv[0]);
  71. }
  72. if (num_blocks < 0 || num_ops < 0) {
  73. usage(argv[0]);
  74. }
  75. int key_size = BEncryption_cipher_key_size(cipher);
  76. int block_size = BEncryption_cipher_block_size(cipher);
  77. uint8_t key[key_size];
  78. BRandom_randomize(key, sizeof(key));
  79. uint8_t iv[block_size];
  80. BRandom_randomize(iv, sizeof(iv));
  81. if (num_blocks > INT_MAX / block_size) {
  82. printf("too much");
  83. goto fail0;
  84. }
  85. int unit_size = num_blocks * block_size;
  86. printf("unit size %d\n", unit_size);
  87. uint8_t *buf1 = BAlloc(unit_size);
  88. if (!buf1) {
  89. printf("BAlloc failed");
  90. goto fail0;
  91. }
  92. uint8_t *buf2 = BAlloc(unit_size);
  93. if (!buf2) {
  94. printf("BAlloc failed");
  95. goto fail1;
  96. }
  97. BEncryption enc;
  98. BEncryption_Init(&enc, mode, cipher, key);
  99. uint8_t *in = buf1;
  100. uint8_t *out = buf2;
  101. BRandom_randomize(in, unit_size);
  102. for (int i = 0; i < num_ops; i++) {
  103. BEncryption_Encrypt(&enc, in, out, unit_size, iv);
  104. uint8_t *t = in;
  105. in = out;
  106. out = t;
  107. }
  108. BEncryption_Free(&enc);
  109. BFree(buf2);
  110. fail1:
  111. BFree(buf1);
  112. fail0:
  113. DebugObjectGlobal_Finish();
  114. return 0;
  115. }