1 module scrypt.scryptenc;
2 /*-
3  * Copyright 2009 Colin Percival
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * This file was originally written by Colin Percival as part of the Tarsnap
28  * online backup system.
29  * 
30  * D binding created by Isak Andersson 2013 (BitPuffin@lavabit.com)
31  */
32 
33 import std.c.stdio;
34 
35 alias ubyte uint8_t;
36 
37 extern (C):
38 
39 /**
40  * The parameters maxmem, maxmemfrac, and maxtime used by all of these
41  * functions are defined as follows:
42  * maxmem - maximum number of bytes of storage to use for V array (which is
43  *     by far the largest consumer of memory).  If this value is set to 0, no
44  *     maximum will be enforced; any other value less than 1 MiB will be
45  *     treated as 1 MiB.
46  * maxmemfrac - maximum fraction of available storage to use for the V array,
47  *     where "available storage" is defined as the minimum out of the
48  *     RLIMIT_AS, RLIMIT_DATA. and RLIMIT_RSS resource limits (if any are
49  *     set).  If this value is set to 0 or more than 0.5 it will be treated
50  *     as 0.5; and 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 /**
62  * Return codes from scrypt(enc|dec)_(buf|file):
63  * 0	success
64  * 1	getrlimit or sysctl(hw.usermem) failed
65  * 2	clock_getres or clock_gettime failed
66  * 3	error computing derived key
67  * 4	could not read salt from /dev/urandom
68  * 5	error in OpenSSL
69  * 6	malloc failed
70  * 7	data is not a valid scrypt-encrypted block
71  * 8	unrecognized scrypt format
72  * 9	decrypting file would take too much memory
73  * 10	decrypting file would take too long
74  * 11	password is incorrect
75  * 12	error writing output file
76  * 13	error reading input file
77  */
78 
79 /**
80  * scryptenc_buf(inbuf, inbuflen, outbuf, passwd, passwdlen,
81  *     maxmem, maxmemfrac, maxtime):
82  * Encrypt inbuflen bytes from inbuf, writing the resulting inbuflen + 128
83  * bytes to outbuf.
84  */
85 int scryptenc_buf(const uint8_t *, size_t, uint8_t *,
86     const uint8_t *, size_t, size_t, double, double);
87 
88 /**
89  * scryptdec_buf(inbuf, inbuflen, outbuf, outlen, passwd, passwdlen,
90  *     maxmem, maxmemfrac, maxtime):
91  * Decrypt inbuflen bytes from inbuf, writing the result into outbuf and the
92  * decrypted data length to outlen.  The allocated length of outbuf must
93  * be at least inbuflen.
94  */
95 int scryptdec_buf(const uint8_t *, size_t, uint8_t *, size_t *,
96     const uint8_t *, size_t, size_t, double, double);
97 
98 /**
99  * scryptenc_file(infile, outfile, passwd, passwdlen,
100  *     maxmem, maxmemfrac, maxtime):
101  * Read a stream from infile and encrypt it, writing the resulting stream to
102  * outfile.
103  */
104 int scryptenc_file(FILE *, FILE *, const uint8_t *, size_t,
105     size_t, double, double);
106 
107 /**
108  * scryptdec_file(infile, outfile, passwd, passwdlen,
109  *     maxmem, maxmemfrac, maxtime):
110  * Read a stream from infile and decrypt it, writing the resulting stream to
111  * outfile.
112  */
113 int scryptdec_file(FILE *, FILE *, const uint8_t *, size_t,
114     size_t, double, double);
115