bencryption_bench.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file bencryption_bench.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdint.h>
  33. #include <limits.h>
  34. #include <misc/balloc.h>
  35. #include <security/BRandom.h>
  36. #include <security/BEncryption.h>
  37. #include <base/DebugObject.h>
  38. static void usage (char *name)
  39. {
  40. printf(
  41. "Usage: %s <enc/dec> <ciper> <num_blocks> <num_ops>\n"
  42. " <cipher> is one of (blowfish, aes).\n",
  43. name
  44. );
  45. exit(1);
  46. }
  47. int main (int argc, char **argv)
  48. {
  49. if (argc <= 0) {
  50. return 1;
  51. }
  52. if (argc != 5) {
  53. usage(argv[0]);
  54. }
  55. char *mode_str = argv[1];
  56. char *cipher_str = argv[2];
  57. int mode;
  58. int cipher = 0; // silence warning
  59. int num_blocks = atoi(argv[3]);
  60. int num_ops = atoi(argv[4]);
  61. if (!strcmp(mode_str, "enc")) {
  62. mode = BENCRYPTION_MODE_ENCRYPT;
  63. }
  64. else if (!strcmp(mode_str, "dec")) {
  65. mode = BENCRYPTION_MODE_DECRYPT;
  66. }
  67. else {
  68. usage(argv[0]);
  69. }
  70. if (!strcmp(cipher_str, "blowfish")) {
  71. cipher = BENCRYPTION_CIPHER_BLOWFISH;
  72. }
  73. else if (!strcmp(cipher_str, "aes")) {
  74. cipher = BENCRYPTION_CIPHER_AES;
  75. }
  76. else {
  77. usage(argv[0]);
  78. }
  79. if (num_blocks < 0 || num_ops < 0) {
  80. usage(argv[0]);
  81. }
  82. int key_size = BEncryption_cipher_key_size(cipher);
  83. int block_size = BEncryption_cipher_block_size(cipher);
  84. uint8_t key[BENCRYPTION_MAX_KEY_SIZE];
  85. BRandom_randomize(key, key_size);
  86. uint8_t iv[BENCRYPTION_MAX_BLOCK_SIZE];
  87. BRandom_randomize(iv, block_size);
  88. if (num_blocks > INT_MAX / block_size) {
  89. printf("too much");
  90. goto fail0;
  91. }
  92. int unit_size = num_blocks * block_size;
  93. printf("unit size %d\n", unit_size);
  94. uint8_t *buf1 = (uint8_t *)BAlloc(unit_size);
  95. if (!buf1) {
  96. printf("BAlloc failed");
  97. goto fail0;
  98. }
  99. uint8_t *buf2 = (uint8_t *)BAlloc(unit_size);
  100. if (!buf2) {
  101. printf("BAlloc failed");
  102. goto fail1;
  103. }
  104. BEncryption enc;
  105. BEncryption_Init(&enc, mode, cipher, key);
  106. uint8_t *in = buf1;
  107. uint8_t *out = buf2;
  108. BRandom_randomize(in, unit_size);
  109. for (int i = 0; i < num_ops; i++) {
  110. BEncryption_Encrypt(&enc, in, out, unit_size, iv);
  111. uint8_t *t = in;
  112. in = out;
  113. out = t;
  114. }
  115. BEncryption_Free(&enc);
  116. BFree(buf2);
  117. fail1:
  118. BFree(buf1);
  119. fail0:
  120. DebugObjectGlobal_Finish();
  121. return 0;
  122. }