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