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