bencryption_bench.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <security/BRandom.h>
  28. #include <security/BEncryption.h>
  29. #include <system/DebugObject.h>
  30. static void usage (char *name)
  31. {
  32. printf(
  33. "Usage: %s <enc/dec> <ciper> <num_blocks> <num_ops>\n"
  34. " <cipher> is one of (blowfish, aes).\n",
  35. name
  36. );
  37. exit(1);
  38. }
  39. int main (int argc, char **argv)
  40. {
  41. if (argc <= 0) {
  42. return 1;
  43. }
  44. if (argc != 5) {
  45. usage(argv[0]);
  46. }
  47. char *mode_str = argv[1];
  48. char *cipher_str = argv[2];
  49. int mode;
  50. int cipher;
  51. int num_blocks = atoi(argv[3]);
  52. int num_ops = atoi(argv[4]);
  53. if (!strcmp(mode_str, "enc")) {
  54. mode = BENCRYPTION_MODE_ENCRYPT;
  55. }
  56. else if (!strcmp(mode_str, "dec")) {
  57. mode = BENCRYPTION_MODE_DECRYPT;
  58. }
  59. else {
  60. usage(argv[0]);
  61. }
  62. if (!strcmp(cipher_str, "blowfish")) {
  63. cipher = BENCRYPTION_CIPHER_BLOWFISH;
  64. }
  65. else if (!strcmp(cipher_str, "aes")) {
  66. cipher = BENCRYPTION_CIPHER_AES;
  67. }
  68. else {
  69. usage(argv[0]);
  70. }
  71. if (num_blocks < 0 || num_ops < 0) {
  72. usage(argv[0]);
  73. }
  74. int key_size = BEncryption_cipher_key_size(cipher);
  75. int block_size = BEncryption_cipher_block_size(cipher);
  76. uint8_t key[key_size];
  77. BRandom_randomize(key, sizeof(key));
  78. uint8_t iv[block_size];
  79. BRandom_randomize(iv, sizeof(iv));
  80. int unit_size = num_blocks * block_size;
  81. printf("unit size %d\n", unit_size);
  82. uint8_t *buf1 = malloc(unit_size);
  83. if (!buf1) {
  84. printf("malloc failed");
  85. goto fail0;
  86. }
  87. uint8_t *buf2 = malloc(unit_size);
  88. if (!buf2) {
  89. printf("malloc failed");
  90. goto fail1;
  91. }
  92. BEncryption enc;
  93. BEncryption_Init(&enc, mode, cipher, key);
  94. uint8_t *in = buf1;
  95. uint8_t *out = buf2;
  96. BRandom_randomize(in, unit_size);
  97. for (int i = 0; i < num_ops; i++) {
  98. BEncryption_Encrypt(&enc, in, out, unit_size, iv);
  99. uint8_t *t = in;
  100. in = out;
  101. out = t;
  102. }
  103. BEncryption_Free(&enc);
  104. free(buf2);
  105. fail1:
  106. free(buf1);
  107. fail0:
  108. DebugObjectGlobal_Finish();
  109. return 0;
  110. }