measure.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdlib.h>
  2. #include "randombytes.h"
  3. #include "cpucycles.h"
  4. #include "crypto_secretbox.h"
  5. extern void printentry(long long,const char *,long long *,long long);
  6. extern unsigned char *alignedcalloc(unsigned long long);
  7. extern const char *primitiveimplementation;
  8. extern const char *implementationversion;
  9. extern const char *sizenames[];
  10. extern const long long sizes[];
  11. extern void allocate(void);
  12. extern void measure(void);
  13. const char *primitiveimplementation = crypto_secretbox_IMPLEMENTATION;
  14. const char *implementationversion = crypto_secretbox_VERSION;
  15. const char *sizenames[] = { "keybytes", "noncebytes", "zerobytes", "boxzerobytes", 0 };
  16. const long long sizes[] = { crypto_secretbox_KEYBYTES, crypto_secretbox_NONCEBYTES, crypto_secretbox_ZEROBYTES, crypto_secretbox_BOXZEROBYTES };
  17. #define MAXTEST_BYTES 4096
  18. static unsigned char *k;
  19. static unsigned char *n;
  20. static unsigned char *m;
  21. static unsigned char *c;
  22. void preallocate(void)
  23. {
  24. }
  25. void allocate(void)
  26. {
  27. k = alignedcalloc(crypto_secretbox_KEYBYTES);
  28. n = alignedcalloc(crypto_secretbox_NONCEBYTES);
  29. m = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
  30. c = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
  31. }
  32. #define TIMINGS 15
  33. static long long cycles[TIMINGS + 1];
  34. void measure(void)
  35. {
  36. int i;
  37. int loop;
  38. int mlen;
  39. for (loop = 0;loop < LOOPS;++loop) {
  40. for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / 8) {
  41. randombytes(k,crypto_secretbox_KEYBYTES);
  42. randombytes(n,crypto_secretbox_NONCEBYTES);
  43. randombytes(m + crypto_secretbox_ZEROBYTES,mlen);
  44. randombytes(c,mlen + crypto_secretbox_ZEROBYTES);
  45. for (i = 0;i <= TIMINGS;++i) {
  46. cycles[i] = cpucycles();
  47. crypto_secretbox(c,m,mlen + crypto_secretbox_ZEROBYTES,n,k);
  48. }
  49. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  50. printentry(mlen,"cycles",cycles,TIMINGS);
  51. for (i = 0;i <= TIMINGS;++i) {
  52. cycles[i] = cpucycles();
  53. crypto_secretbox_open(m,c,mlen + crypto_secretbox_ZEROBYTES,n,k);
  54. }
  55. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  56. printentry(mlen,"open_cycles",cycles,TIMINGS);
  57. ++c[crypto_secretbox_ZEROBYTES];
  58. for (i = 0;i <= TIMINGS;++i) {
  59. cycles[i] = cpucycles();
  60. crypto_secretbox_open(m,c,mlen + crypto_secretbox_ZEROBYTES,n,k);
  61. }
  62. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  63. printentry(mlen,"forgery_open_cycles",cycles,TIMINGS);
  64. }
  65. }
  66. }