|
1 | 1 | package xbr.network.crypto; |
2 | 2 |
|
3 | | -import org.libsodium.jni.crypto.Random; |
4 | | -import org.libsodium.jni.crypto.Util; |
5 | | -import org.libsodium.jni.encoders.Encoder; |
6 | | - |
7 | 3 | import java.util.Arrays; |
8 | 4 |
|
9 | | -import static org.libsodium.jni.NaCl.sodium; |
10 | | -import static org.libsodium.jni.SodiumConstants.BOXZERO_BYTES; |
11 | | -import static org.libsodium.jni.SodiumConstants.XSALSA20_POLY1305_SECRETBOX_KEYBYTES; |
12 | | -import static org.libsodium.jni.SodiumConstants.XSALSA20_POLY1305_SECRETBOX_NONCEBYTES; |
13 | | -import static org.libsodium.jni.SodiumConstants.ZERO_BYTES; |
14 | | -import static org.libsodium.jni.crypto.Util.checkLength; |
15 | | -import static org.libsodium.jni.crypto.Util.isValid; |
16 | | -import static org.libsodium.jni.crypto.Util.removeZeros; |
| 5 | +import static io.xconn.cryptology.SecretBox.box; |
| 6 | +import static io.xconn.cryptology.SecretBox.boxOpen; |
| 7 | +import static io.xconn.cryptology.Util.checkLength; |
| 8 | +import static io.xconn.cryptology.Util.generateRandomBytesArray; |
17 | 9 |
|
| 10 | +import static xbr.network.Util.NONCE_SIZE; |
| 11 | +import static xbr.network.Util.SECRET_KEY_LEN; |
18 | 12 |
|
19 | 13 | public class SecretBox { |
20 | | - |
21 | | - private byte[] mKey; |
22 | | - private Encoder mEncoder; |
| 14 | + private final byte[] mKey; |
23 | 15 |
|
24 | 16 | public SecretBox(byte[] key) { |
25 | | - checkLength(key, XSALSA20_POLY1305_SECRETBOX_KEYBYTES); |
26 | | - mEncoder = Encoder.RAW; |
27 | | - mKey = key; |
| 17 | + checkLength(key, SECRET_KEY_LEN); |
| 18 | + mKey = Arrays.copyOf(key, key.length); |
28 | 19 | } |
29 | 20 |
|
30 | 21 | public byte[] encrypt(byte[] message) { |
31 | | - byte[] nonce = new Random().randomBytes(XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); |
| 22 | + byte[] nonce = generateRandomBytesArray(NONCE_SIZE); |
32 | 23 | return encrypt(nonce, message); |
33 | 24 | } |
34 | 25 |
|
35 | | - public byte[] encrypt(byte[] nonce, byte[] message) { |
36 | | - checkLength(nonce, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); |
37 | | - byte[] msg = org.libsodium.jni.crypto.Util.prependZeros(ZERO_BYTES, message); |
38 | | - byte[] ct = org.libsodium.jni.crypto.Util.zeros(msg.length); |
39 | | - isValid(sodium().crypto_secretbox_xsalsa20poly1305(ct, msg, msg.length, |
40 | | - nonce, mKey), "Encryption failed"); |
41 | | - byte[] cipherWithoutNonce = removeZeros(BOXZERO_BYTES, ct); |
42 | | - byte[] ciphertext = new byte[cipherWithoutNonce.length + |
43 | | - XSALSA20_POLY1305_SECRETBOX_NONCEBYTES]; |
| 26 | + public byte[] encrypt(byte[] nonce, byte[] plaintext) { |
| 27 | + byte[] cipherWithoutNonce = box(nonce, plaintext, mKey); |
| 28 | + byte[] ciphertext = new byte[cipherWithoutNonce.length + NONCE_SIZE]; |
44 | 29 | System.arraycopy(nonce, 0, ciphertext, 0, nonce.length); |
45 | | - System.arraycopy(cipherWithoutNonce, 0, ciphertext, nonce.length, |
46 | | - cipherWithoutNonce.length); |
| 30 | + System.arraycopy(cipherWithoutNonce, 0, ciphertext, nonce.length, cipherWithoutNonce.length); |
47 | 31 | return ciphertext; |
48 | 32 | } |
49 | 33 |
|
50 | 34 | public byte[] decrypt(byte[] ciphertext) { |
51 | | - byte[] nonce = Arrays.copyOfRange(ciphertext, 0, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); |
52 | | - byte[] message = Arrays.copyOfRange(ciphertext, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES, |
53 | | - ciphertext.length); |
54 | | - return decrypt(nonce, message); |
55 | | - } |
56 | | - |
57 | | - public byte[] decrypt(byte[] nonce, byte[] ciphertext) { |
58 | | - checkLength(nonce, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); |
59 | | - byte[] ct = org.libsodium.jni.crypto.Util.prependZeros(BOXZERO_BYTES, ciphertext); |
60 | | - byte[] message = Util.zeros(ct.length); |
61 | | - isValid(sodium().crypto_secretbox_xsalsa20poly1305_open(message, ct, |
62 | | - ct.length, nonce, mKey), "Decryption failed. Ciphertext failed verification"); |
63 | | - return removeZeros(ZERO_BYTES, message); |
| 35 | + byte[] nonce = Arrays.copyOfRange(ciphertext, 0, NONCE_SIZE); |
| 36 | + byte[] message = Arrays.copyOfRange(ciphertext, NONCE_SIZE, ciphertext.length); |
| 37 | + return boxOpen(nonce, message, mKey); |
64 | 38 | } |
65 | 39 | } |
0 commit comments