Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SealedBox support #127

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/org/libsodium/jni/crypto/SealedBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.libsodium.jni.crypto;

import org.libsodium.jni.encoders.Encoder;

import static org.libsodium.jni.NaCl.sodium;
import static org.libsodium.jni.SodiumConstants.SEAL_BYTES;
import static org.libsodium.jni.crypto.Util.isValid;

public class SealedBox {

private byte[] publicKey;
private byte[] privateKey;

public SealedBox(byte[] publicKey) {
this.publicKey = publicKey;
this.privateKey = null;
}

public SealedBox(String publicKey, Encoder encoder) {
this(encoder.decode(publicKey));
}

public SealedBox(byte[] publicKey, byte[] privateKey) {
this.publicKey = publicKey;
this.privateKey = privateKey;
}

public SealedBox(String publicKey, String privateKey, Encoder encoder) {
this(encoder.decode(publicKey), encoder.decode(privateKey));
}

public byte[] encrypt(byte[] message) {
if (publicKey == null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to avoid waiting until encrypt time to determine that the public key passed at initialization is incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, moved that check into the constructor, now throwing IllegalArgumentException though that also inherits RuntimeException. Couldn't think of an appropriate checked exception here (if we want to go that way, we could do a custom exception though).

throw new RuntimeException("Encryption failed. Public key not available.");
byte[] ct = new byte[message.length + SEAL_BYTES];
isValid(sodium().crypto_box_seal(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Util class is marked deprecated and should not be used.

isValid is supposed to return a boolean, though throws a RuntimeException.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have removed the usage of isValid and have moved the check internally.

What's the planned approach to deal with all the deprecated classes ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Planning to remove the deprecated classes in a major release.
The deprecated classes haven't been removed yet as it's unclear impact to downstream developers.

ct, message, message.length, publicKey),
"Encryption failed");
return ct;
}

public byte[] decrypt(byte[] ciphertext) {
if (privateKey == null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to avoid waiting until decrypt time to determine that the public key passed at initialization is incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed that as well.

throw new RuntimeException("Decryption failed. Private key not available.");

byte[] message = new byte[ciphertext.length - SEAL_BYTES];
isValid(sodium().crypto_box_seal_open(
message, ciphertext, ciphertext.length, publicKey, privateKey),
"Decryption failed. Ciphertext failed verification");
return message;
}
}