| 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 | #ifndef _SCRYPTENC_H_ |
| 30 | #define _SCRYPTENC_H_ |
| 31 | |
| 32 | #include <stdint.h> |
| 33 | #include <stdio.h> |
| 34 | |
| 35 | /** |
| 36 | * NOTE: This file provides prototypes for routines which encrypt/decrypt data |
| 37 | * using a key derived from a password by using the scrypt key derivation |
| 38 | * function. If you are just trying to "hash" a password for user logins, |
| 39 | * this is not the code you are looking for. You want to use the crypto_scrypt |
| 40 | * function directly. |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * The parameters maxmem, maxmemfrac, and maxtime used by all of these |
| 45 | * functions are defined as follows: |
| 46 | * maxmem - maximum number of bytes of storage to use for V array (which is |
| 47 | * by far the largest consumer of memory). If this value is set to 0, no |
| 48 | * maximum will be enforced; any other value less than 1 MiB will be |
| 49 | * treated as 1 MiB. |
| 50 | * maxmemfrac - maximum fraction of available storage to use for the V array, |
| 51 | * where "available storage" is defined as the minimum out of the |
| 52 | * RLIMIT_AS, RLIMIT_DATA. and RLIMIT_RSS resource limits (if any are |
| 53 | * set). If this value is set to 0 or more than 0.5 it will be treated |
| 54 | * as 0.5; and this value will never cause a limit of less than 1 MiB to |
| 55 | * be enforced. |
| 56 | * maxtime - maximum amount of CPU time to spend computing the derived keys, |
| 57 | * in seconds. This limit is only approximately enforced; the CPU |
| 58 | * performance is estimated and parameter limits are chosen accordingly. |
| 59 | * For the encryption functions, the parameters to the scrypt key derivation |
| 60 | * function are chosen to make the key as strong as possible subject to the |
| 61 | * specified limits; for the decryption functions, the parameters used are |
| 62 | * compared to the computed limits and an error is returned if decrypting |
| 63 | * the data would take too much memory or CPU time. |
| 64 | */ |
| 65 | /** |
| 66 | * Return codes from scrypt(enc|dec)_(buf|file): |
| 67 | * 0 success |
| 68 | * 1 getrlimit or sysctl(hw.usermem) failed |
| 69 | * 2 clock_getres or clock_gettime failed |
| 70 | * 3 error computing derived key |
| 71 | * 4 could not read salt from /dev/urandom |
| 72 | * 5 error in OpenSSL |
| 73 | * 6 malloc failed |
| 74 | * 7 data is not a valid scrypt-encrypted block |
| 75 | * 8 unrecognized scrypt format |
| 76 | * 9 decrypting file would take too much memory |
| 77 | * 10 decrypting file would take too long |
| 78 | * 11 password is incorrect |
| 79 | * 12 error writing output file |
| 80 | * 13 error reading input file |
| 81 | */ |
| 82 | |
| 83 | /** |
| 84 | * scryptenc_buf(inbuf, inbuflen, outbuf, passwd, passwdlen, |
| 85 | * maxmem, maxmemfrac, maxtime, verbose): |
| 86 | * Encrypt inbuflen bytes from inbuf, writing the resulting inbuflen + 128 |
| 87 | * bytes to outbuf. |
| 88 | */ |
| 89 | int scryptenc_buf(const uint8_t *, size_t, uint8_t *, |
| 90 | const uint8_t *, size_t, size_t, double, double, int); |
| 91 | |
| 92 | /** |
| 93 | * scryptdec_buf(inbuf, inbuflen, outbuf, outlen, passwd, passwdlen, |
| 94 | * maxmem, maxmemfrac, maxtime, verbose, force): |
| 95 | * Decrypt inbuflen bytes from inbuf, writing the result into outbuf and the |
| 96 | * decrypted data length to outlen. The allocated length of outbuf must |
| 97 | * be at least inbuflen. If ${force} is 1, do not check whether |
| 98 | * decryption will exceed the estimated available memory or time. |
| 99 | */ |
| 100 | int scryptdec_buf(const uint8_t *, size_t, uint8_t *, size_t *, |
| 101 | const uint8_t *, size_t, size_t, double, double, int, int); |
| 102 | |
| 103 | /** |
| 104 | * scryptenc_file(infile, outfile, passwd, passwdlen, |
| 105 | * maxmem, maxmemfrac, maxtime, verbose): |
| 106 | * Read a stream from infile and encrypt it, writing the resulting stream to |
| 107 | * outfile. |
| 108 | */ |
| 109 | int scryptenc_file(FILE *, FILE *, const uint8_t *, size_t, |
| 110 | size_t, double, double, int); |
| 111 | |
| 112 | /** |
| 113 | * scryptdec_file(infile, outfile, passwd, passwdlen, |
| 114 | * maxmem, maxmemfrac, maxtime, verbose, force): |
| 115 | * Read a stream from infile and decrypt it, writing the resulting stream to |
| 116 | * outfile. If ${force} is 1, do not check whether decryption |
| 117 | * will exceed the estimated available memory or time. |
| 118 | */ |
| 119 | int scryptdec_file(FILE *, FILE *, const uint8_t *, size_t, |
| 120 | size_t, double, double, int, int); |
| 121 | |
| 122 | #endif /* !_SCRYPTENC_H_ */ |