scryptenc.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef SCRYPTENC_H_
  30. #define SCRYPTENC_H_
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. /**
  34. * NOTE: This file provides prototypes for routines which encrypt/decrypt data
  35. * using a key derived from a password by using the scrypt key derivation
  36. * function. If you are just trying to "hash" a password for user logins,
  37. * this is not the code you are looking for. You want to use the
  38. * crypto_scrypt() function directly.
  39. */
  40. /**
  41. * The parameters maxmem, maxmemfrac, and maxtime used by all of these
  42. * functions are defined as follows:
  43. * maxmem - maximum number of bytes of storage to use for V array (which is
  44. * by far the largest consumer of memory). If this value is set to 0, no
  45. * maximum will be enforced; any other value less than 1 MiB will be
  46. * treated as 1 MiB.
  47. * maxmemfrac - maximum fraction of available storage to use for the V array,
  48. * where "available storage" is defined as the minimum out of the
  49. * RLIMIT_AS, RLIMIT_DATA. and RLIMIT_RSS resource limits (if any are
  50. * set). This value will never cause a limit of less than 1 MiB to
  51. * be enforced.
  52. * maxtime - maximum amount of CPU time to spend computing the derived keys,
  53. * in seconds. This limit is only approximately enforced; the CPU
  54. * performance is estimated and parameter limits are chosen accordingly.
  55. * For the encryption functions, the parameters to the scrypt key derivation
  56. * function are chosen to make the key as strong as possible subject to the
  57. * specified limits; for the decryption functions, the parameters used are
  58. * compared to the computed limits and an error is returned if decrypting
  59. * the data would take too much memory or CPU time.
  60. */
  61. struct scryptenc_params {
  62. size_t maxmem;
  63. double maxmemfrac;
  64. double maxtime;
  65. /* Explicit parameters. */
  66. int logN;
  67. uint32_t r;
  68. uint32_t p;
  69. };
  70. /* Return codes from scrypt(enc|dec)_(buf|file|prep). */
  71. #define SCRYPT_OK 0 /* success */
  72. #define SCRYPT_ELIMIT 1 /* getrlimit or sysctrl(hw.usermem) failed */
  73. #define SCRYPT_ECLOCK 2 /* clock_getres or clock_gettime failed */
  74. #define SCRYPT_EKEY 3 /* error computing derived key */
  75. #define SCRYPT_ESALT 4 /* could not read salt */
  76. #define SCRYPT_EOPENSSL 5 /* error in OpenSSL */
  77. #define SCRYPT_ENOMEM 6 /* malloc failed */
  78. #define SCRYPT_EINVAL 7 /* data is not a valid scrypt-encrypted block */
  79. #define SCRYPT_EVERSION 8 /* unrecognized scrypt version number */
  80. #define SCRYPT_ETOOBIG 9 /* decrypting would take too much memory */
  81. #define SCRYPT_ETOOSLOW 10 /* decrypting would take too long */
  82. #define SCRYPT_EPASS 11 /* password is incorrect */
  83. #define SCRYPT_EWRFILE 12 /* error writing output file */
  84. #define SCRYPT_ERDFILE 13 /* error reading input file */
  85. #define SCRYPT_EPARAM 14 /* error in explicit parameters */
  86. #define SCRYPT_EBIGSLOW 15 /* both SCRYPT_ETOOBIG and SCRYPT_ETOOSLOW */
  87. /* Opaque structure. */
  88. struct scryptdec_file_cookie;
  89. /**
  90. * scryptenc_buf(inbuf, inbuflen, outbuf, passwd, passwdlen,
  91. * params, verbose, force):
  92. * Encrypt ${inbuflen} bytes from ${inbuf}, writing the resulting
  93. * ${inbuflen} + 128 bytes to ${outbuf}. If ${force} is 1, do not check
  94. * whether decryption will exceed the estimated available memory or time.
  95. * The explicit parameters within ${params} must be zero or must all be
  96. * non-zero. If explicit parameters are used and the computation is estimated
  97. * to exceed resource limits, print a warning instead of returning an error.
  98. * Return the explicit parameters used via ${params}.
  99. */
  100. int scryptenc_buf(const uint8_t *, size_t, uint8_t *,
  101. const uint8_t *, size_t, struct scryptenc_params *, int, int);
  102. /**
  103. * scryptdec_buf(inbuf, inbuflen, outbuf, outlen, passwd, passwdlen,
  104. * params, verbose, force):
  105. * Decrypt ${inbuflen} bytes from ${inbuf}, writing the result into ${outbuf}
  106. * and the decrypted data length to ${outlen}. The allocated length of
  107. * ${outbuf} must be at least ${inbuflen}. If ${force} is 1, do not check
  108. * whether decryption will exceed the estimated available memory or time.
  109. * The explicit parameters within ${params} must be zero. Return the explicit
  110. * parameters used via ${params}.
  111. */
  112. int scryptdec_buf(const uint8_t *, size_t, uint8_t *, size_t *,
  113. const uint8_t *, size_t, struct scryptenc_params *, int, int);
  114. /**
  115. * scryptenc_file(infile, outfile, passwd, passwdlen, params, verbose, force):
  116. * Read a stream from ${infile} and encrypt it, writing the resulting stream
  117. * to ${outfile}. If ${force} is 1, do not check whether decryption will
  118. * exceed the estimated available memory or time. The explicit parameters
  119. * within ${params} must be zero or must all be non-zero. If explicit
  120. * parameters are used and the computation is estimated to exceed resource
  121. * limits, print a warning instead of returning an error. Return the explicit
  122. * parameters used via ${params}.
  123. */
  124. int scryptenc_file(FILE *, FILE *, const uint8_t *, size_t,
  125. struct scryptenc_params *, int, int);
  126. /**
  127. * scryptdec_file_printparams(infile):
  128. * Print the encryption parameters (N, r, p) used for the encrypted ${infile}.
  129. */
  130. int scryptdec_file_printparams(FILE *);
  131. /**
  132. * scryptdec_file(infile, outfile, passwd, passwdlen, params, verbose, force):
  133. * Read a stream from ${infile} and decrypt it, writing the resulting stream
  134. * to ${outfile}. If ${force} is 1, do not check whether decryption
  135. * will exceed the estimated available memory or time. The explicit
  136. * parameters within ${params} must be zero. Return the explicit parameters
  137. * used via ${params}.
  138. */
  139. int scryptdec_file(FILE *, FILE *, const uint8_t *, size_t,
  140. struct scryptenc_params *, int, int);
  141. /**
  142. * scryptdec_file_prep(infile, passwd, passwdlen, params, verbose, force,
  143. * cookie):
  144. * Prepare to decrypt ${infile}, including checking the passphrase. Allocate
  145. * a cookie at ${cookie}. After calling this function, ${infile} should not
  146. * be modified until the decryption is completed by scryptdec_file_copy().
  147. * If ${force} is 1, do not check whether decryption will exceed the estimated
  148. * available memory or time. The explicit parameters within ${params} must be
  149. * zero. Return the explicit parameters to be used via ${params}.
  150. */
  151. int scryptdec_file_prep(FILE *, const uint8_t *, size_t,
  152. struct scryptenc_params *, int, int, struct scryptdec_file_cookie **);
  153. /**
  154. * scryptdec_file_copy(cookie, outfile):
  155. * Read a stream from the file that was passed into the ${cookie} by
  156. * scryptdec_file_prep(), decrypt it, and write the resulting stream to
  157. * ${outfile}. After this function completes, it is safe to modify/close
  158. * ${outfile} and the ${infile} which was given to scryptdec_file_prep().
  159. */
  160. int scryptdec_file_copy(struct scryptdec_file_cookie *, FILE *);
  161. /**
  162. * scryptdec_file_cookie_free(cookie):
  163. * Free the ${cookie}.
  164. */
  165. void scryptdec_file_cookie_free(struct scryptdec_file_cookie *);
  166. #endif /* !SCRYPTENC_H_ */