/
LazysodiumCryptoInstance.java
73 lines (65 loc) · 2.72 KB
/
LazysodiumCryptoInstance.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
/*
* Copyright (c) 2018 Threema GmbH
*
* Licensed under the Apache License, Version 2.0, <see LICENSE-APACHE file>
* or the MIT license <see LICENSE-MIT file>, at your option. This file may not be
* copied, modified, or distributed except according to those terms.
*/
package org.saltyrtc.client.tests;
import com.goterl.lazysodium.LazySodiumJava;
import com.goterl.lazysodium.SodiumJava;
import com.goterl.lazysodium.interfaces.Box;
import org.saltyrtc.client.annotations.NonNull;
import org.saltyrtc.client.crypto.CryptoException;
import org.saltyrtc.client.crypto.CryptoInstance;
import org.saltyrtc.client.crypto.CryptoProvider;
import static com.goterl.lazysodium.interfaces.Box.BEFORENMBYTES;
import static com.goterl.lazysodium.interfaces.Box.MACBYTES;
/**
* An implementation of the CryptoInstance interface for lazysodium-java.
*/
public class LazysodiumCryptoInstance implements CryptoInstance {
@NonNull private final Box.Native sodium;
@NonNull private final byte[] sharedKey;
public LazysodiumCryptoInstance(
@NonNull SodiumJava sodium,
@NonNull byte[] ownPrivateKey,
@NonNull byte[] otherPublicKey
) throws CryptoException {
this.sodium = new LazySodiumJava(sodium);
// Verify key lengths
if (otherPublicKey.length != CryptoProvider.PUBLICKEYBYTES) {
throw new CryptoException("Invalid public key length");
}
if (ownPrivateKey.length != CryptoProvider.PRIVATEKEYBYTES) {
throw new CryptoException("Invalid private key length");
}
// Precalculate shared key
final byte[] k = new byte[BEFORENMBYTES];
final boolean success = this.sodium.cryptoBoxBeforeNm(k, otherPublicKey, ownPrivateKey);
if (!success) {
throw new CryptoException("Could not precalculate shared key");
}
this.sharedKey = k;
}
@NonNull
@Override
public byte[] encrypt(@NonNull byte[] data, @NonNull byte[] nonce) throws CryptoException {
final byte[] ciphertext = new byte[data.length + MACBYTES];
final boolean success = this.sodium.cryptoBoxEasyAfterNm(ciphertext, data, data.length, nonce, this.sharedKey);
if (!success) {
throw new CryptoException("Could not encrypt data");
}
return ciphertext;
}
@NonNull
@Override
public byte[] decrypt(@NonNull byte[] data, @NonNull byte[] nonce) throws CryptoException {
final byte[] plaintext = new byte[data.length - MACBYTES];
final boolean success = this.sodium.cryptoBoxOpenEasyAfterNm(plaintext, data, data.length, nonce, this.sharedKey);
if (!success) {
throw new CryptoException("Could not decrypt data");
}
return plaintext;
}
}