scryptenc_cpuperf.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*-
  2. * Copyright 2009 Colin Percival
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * This file was originally written by Colin Percival as part of the Tarsnap
  27. * online backup system.
  28. */
  29. #include <sys/time.h>
  30. #include <stdint.h>
  31. #include <stdio.h>
  32. #include "crypto_scrypt.h"
  33. #include "monoclock.h"
  34. #include "scryptenc.h"
  35. #include "scryptenc_cpuperf.h"
  36. static int
  37. getclockdiff(struct timeval * st, double * diffd)
  38. {
  39. struct timeval en;
  40. if (monoclock_get(&en))
  41. return (1);
  42. *diffd = timeval_diff((*st), en);
  43. return (0);
  44. }
  45. /**
  46. * scryptenc_cpuperf(opps):
  47. * Estimate the number of salsa20/8 cores which can be executed per second,
  48. * and store the value in ${opps}. Return a SCRYPT_* code.
  49. */
  50. int
  51. scryptenc_cpuperf(double * opps)
  52. {
  53. struct timeval st;
  54. double resd, diffd;
  55. uint64_t i = 0;
  56. /* Get the clock resolution. */
  57. if (monoclock_getres(&resd))
  58. return (SCRYPT_ECLOCK);
  59. #ifdef DEBUG
  60. fprintf(stderr, "Clock resolution is %g\n", resd);
  61. #endif
  62. /* Loop until the clock ticks. */
  63. if (monoclock_get(&st))
  64. return (SCRYPT_ECLOCK);
  65. do {
  66. /* Do an scrypt. */
  67. if (crypto_scrypt(NULL, 0, NULL, 0, 16, 1, 1, NULL, 0))
  68. return (SCRYPT_EKEY);
  69. /* Has the clock ticked? */
  70. if (getclockdiff(&st, &diffd))
  71. return (SCRYPT_ECLOCK);
  72. if (diffd > 0)
  73. break;
  74. } while (1);
  75. /* Count how many scrypts we can do before the next tick. */
  76. if (monoclock_get(&st))
  77. return (SCRYPT_ECLOCK);
  78. do {
  79. /* Do an scrypt. */
  80. if (crypto_scrypt(NULL, 0, NULL, 0, 128, 1, 1, NULL, 0))
  81. return (SCRYPT_EKEY);
  82. /* We invoked the salsa20/8 core 512 times. */
  83. i += 512;
  84. /* Check if we have looped for long enough. */
  85. if (getclockdiff(&st, &diffd))
  86. return (SCRYPT_ECLOCK);
  87. if (diffd > resd)
  88. break;
  89. } while (1);
  90. #ifdef DEBUG
  91. fprintf(stderr, "%ju salsa20/8 cores performed in %g seconds\n",
  92. (uintmax_t)i, diffd);
  93. #endif
  94. /* We can do approximately i salsa20/8 cores per diffd seconds. */
  95. *opps = (double)i / diffd;
  96. return (SCRYPT_OK);
  97. }