-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncryptionHandlerV4.java
245 lines (213 loc) · 6.33 KB
/
EncryptionHandlerV4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright © 2020-2023 Andy Goryachev <andy@goryachev.com>
package goryachev.password.data.v4;
import goryachev.common.io.DReader;
import goryachev.common.io.DWriter;
import goryachev.common.util.CKit;
import goryachev.memsafecrypto.CByteArray;
import goryachev.memsafecrypto.Crypto;
import goryachev.memsafecrypto.OpaqueChars;
import goryachev.memsafecrypto.bc.Argon2BytesGenerator;
import goryachev.memsafecrypto.bc.Argon2Parameters;
import goryachev.memsafecrypto.bc.Blake2bDigest;
import goryachev.memsafecrypto.salsa.XSalsaTools;
import goryachev.password.data.IEncryptionHandler;
import java.io.ByteArrayOutputStream;
import java.security.SecureRandom;
/**
* Persists data with Argon2id + XSalsa20Poly1305.
*
* Format:
*
* long SIGNATURE;
* byte[] nonce; // nonce
* int payloadSize;
* byte[] payload; // payloadSize bytes of the payload
* // payload is encrypted with XSalsa20Poly1305 cipher
* // scrypt salt is a Blake2b hash of nonce
*
*/
public final class EncryptionHandlerV4
implements IEncryptionHandler
{
public static final long SIGNATURE = 0x1DEA_2022_0123_1530L;
public static final int KEY_SIZE_BYTES = 256/8;
public static final int NONCE_SIZE_BYTES = 256/8;
// hard-coded
public static final int ARGON_TYPE = Argon2Parameters.ARGON2_id;
public static final int ARGON_VERSION = Argon2Parameters.ARGON2_VERSION_13;
public static final int ARGON_MEM_KB = 256_000;
public static final int ARGON_LANES = 4;
public static final int ARGON_ITERATIONS = 4;
/** enforce encrypted block size in 4k increments to obfuscate the actual payload length */
public static final int BLOCK_LENGTH = 4096;
public static final int HEADER_SIZE = 8 + NONCE_SIZE_BYTES; // signature + nonce
public static final String ERROR_INVALID_FORMAT = "ERROR_INVALID_FORMAT";
public static final String ERROR_WRONG_SIGNATURE = "ERROR_WRONG_SIGNATURE";
/**
* Encrypts payload byte array with a key derived from the supplied password.
* Returns the byte array formatted according to the specification.
*/
public final byte[] encrypt(SecureRandom random, OpaqueChars pass, CByteArray payload) throws Exception
{
int containerSize = (((payload.length() + 4) / BLOCK_LENGTH) + 1) * BLOCK_LENGTH;
CByteArray container = new CByteArray(containerSize);
try
{
container.setInt(0, payload.length());
container.copyFrom(payload, 0, payload.length(), 4);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DWriter out = new DWriter(bout);
try
{
// header
out.writeLong(SIGNATURE);
// the same nonce is being used for both scrypt and EAX encryption
byte[] nonce = new byte[NONCE_SIZE_BYTES];
random.nextBytes(nonce);
out.write(nonce);
// salt = hashed nonce
CByteArray salt = hash(nonce);
CByteArray pw = null;
try
{
pw = (pass == null ? new CByteArray(0) : pass.getCByteArray());
Argon2Parameters mp = new Argon2Parameters.Builder(ARGON_TYPE).
withVersion(ARGON_VERSION).
withMemoryAsKB(ARGON_MEM_KB).
withParallelism(ARGON_LANES).
withIterations(ARGON_ITERATIONS).
withSalt(salt).
build();
CByteArray key = new CByteArray(KEY_SIZE_BYTES);
try
{
Argon2BytesGenerator ms = new Argon2BytesGenerator();
ms.init(mp);
ms.generateBytes(pw, key);
CByteArray enc = XSalsaTools.encryptXSalsa20Poly1305(key, CByteArray.readOnly(nonce), container);
byte[] b = enc.toByteArray();
out.write(b);
}
finally
{
Crypto.zero(key);
}
}
finally
{
Crypto.zero(pw);
}
}
finally
{
CKit.close(out);
}
return bout.toByteArray();
}
finally
{
container.zero();
}
}
/**
* Attempts to decrypt the supplied byte array using the specified passphrase.
* Returns the decrypted data or throws an exception.
*/
public final CByteArray decrypt(byte[] encrypted, OpaqueChars pass) throws Exception
{
if(pass == null)
{
return null;
}
else if(encrypted == null)
{
return null;
}
else if(encrypted.length == 0)
{
return null;
}
DReader rd = new DReader(encrypted);
try
{
// header
long ver = rd.readLong();
if(ver != SIGNATURE)
{
throw new Exception(ERROR_WRONG_SIGNATURE);
}
byte[] nonce = new byte[NONCE_SIZE_BYTES];
CKit.readFully(rd, nonce);
// salt = hashed nonce
CByteArray salt = hash(nonce);
CByteArray pw = null;
try
{
// generate key with scrypt
// P - passphrase
// S - salt
// N - cpu/memory cost
// r - block mix size parameter
// p - parallelization parameter
pw = pass.getCByteArray();
Argon2Parameters mp = new Argon2Parameters.Builder(ARGON_TYPE).
withVersion(ARGON_VERSION).
withMemoryAsKB(ARGON_MEM_KB).
withParallelism(ARGON_LANES).
withIterations(ARGON_ITERATIONS).
withSalt(salt).
build();
CByteArray key = new CByteArray(KEY_SIZE_BYTES);
try
{
Argon2BytesGenerator ms = new Argon2BytesGenerator();
ms.init(mp);
ms.generateBytes(pw, key);
CByteArray input = CByteArray.readOnly(encrypted, HEADER_SIZE, encrypted.length - HEADER_SIZE);
CByteArray decrypted = XSalsaTools.decryptXSalsa20Poly1305(key, CByteArray.readOnly(nonce), input);
try
{
int len = decrypted.getInt(0);
check(len, 0, Integer.MAX_VALUE, ERROR_INVALID_FORMAT);
return CByteArray.readOnly(decrypted, 4, len);
}
finally
{
decrypted.zero();
}
}
finally
{
Crypto.zero(key);
}
}
finally
{
Crypto.zero(pw);
}
}
finally
{
CKit.close(rd);
}
}
private static void check(int value, int min, int max, String err) throws Exception
{
if(value < min)
{
throw new Exception(err);
}
else if(value > max)
{
throw new Exception(err);
}
}
private static CByteArray hash(byte[] b)
{
Blake2bDigest d = new Blake2bDigest(NONCE_SIZE_BYTES * 8);
d.update(b, 0, b.length);
CByteArray out = new CByteArray(NONCE_SIZE_BYTES);
d.doFinal(out, 0);
return out;
}
}