scryptenc_cpuperf.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*-
  2. * Copyright 2009-2025 Tarsnap Backup Inc.
  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. /* Success! */
  44. return (0);
  45. }
  46. /**
  47. * scryptenc_cpuperf(opps):
  48. * Estimate the number of salsa20/8 cores which can be executed per second,
  49. * and store the value in ${opps}. Return a SCRYPT_* code.
  50. */
  51. int
  52. scryptenc_cpuperf(double * opps)
  53. {
  54. struct timeval st;
  55. double resd, diffd;
  56. uint64_t i = 0;
  57. /* Get the clock resolution. */
  58. if (monoclock_getres(&resd))
  59. return (SCRYPT_ECLOCK);
  60. #ifdef DEBUG
  61. fprintf(stderr, "Clock resolution is %g\n", resd);
  62. #endif
  63. /* Loop until the clock ticks. */
  64. if (monoclock_get(&st))
  65. return (SCRYPT_ECLOCK);
  66. do {
  67. /* Do an scrypt. */
  68. if (crypto_scrypt(NULL, 0, NULL, 0, 16, 1, 1, NULL, 0))
  69. return (SCRYPT_EKEY);
  70. /* Has the clock ticked? */
  71. if (getclockdiff(&st, &diffd))
  72. return (SCRYPT_ECLOCK);
  73. if (diffd > 0)
  74. break;
  75. } while (1);
  76. /* Count how many scrypts we can do before the next tick. */
  77. if (monoclock_get(&st))
  78. return (SCRYPT_ECLOCK);
  79. do {
  80. /* Do an scrypt. */
  81. if (crypto_scrypt(NULL, 0, NULL, 0, 128, 1, 1, NULL, 0))
  82. return (SCRYPT_EKEY);
  83. /* We invoked the salsa20/8 core 512 times. */
  84. i += 512;
  85. /* Check if we have looped for long enough. */
  86. if (getclockdiff(&st, &diffd))
  87. return (SCRYPT_ECLOCK);
  88. if (diffd > resd)
  89. break;
  90. } while (1);
  91. #ifdef DEBUG
  92. fprintf(stderr, "%ju salsa20/8 cores performed in %g seconds\n",
  93. (uintmax_t)i, diffd);
  94. #endif
  95. /* We can do approximately i salsa20/8 cores per diffd seconds. */
  96. *opps = (double)i / diffd;
  97. /* Success! */
  98. return (SCRYPT_OK);
  99. }