diff --git a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoder.java b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoder.java index e5bde9592..835869dac 100644 --- a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoder.java +++ b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoder.java @@ -11,6 +11,6 @@ import java.security.GeneralSecurityException; public interface BackupPayloadDecoder { SocialBackup decodeBackupPayload( SecretKey secret, - BackupPayload backupPayload, byte[] nonce) throws FormatException, + BackupPayload backupPayload) throws FormatException, GeneralSecurityException; } diff --git a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java index 59ce9cc03..145a0ef5a 100644 --- a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadDecoderImpl.java @@ -30,6 +30,7 @@ import javax.inject.Inject; import javax.inject.Provider; import static org.briarproject.briar.socialbackup.SocialBackupConstants.AUTH_TAG_BYTES; +import static org.briarproject.briar.socialbackup.SocialBackupConstants.NONCE_BYTES; public class BackupPayloadDecoderImpl { private final ClientHelper clientHelper; @@ -50,15 +51,22 @@ public class BackupPayloadDecoderImpl { public SocialBackup decodeBackupPayload( SecretKey secret, - BackupPayload backupPayload, byte[] nonce) + BackupPayload backupPayload) throws FormatException, GeneralSecurityException { + byte[] ciphertextWithNonce = backupPayload.getBytes(); + byte[] nonce = new byte[NONCE_BYTES]; + System.arraycopy(ciphertextWithNonce, 0, nonce, 0, NONCE_BYTES); + + byte[] ciphertext = new byte[ciphertextWithNonce.length - NONCE_BYTES]; + System.arraycopy(ciphertextWithNonce, nonce.length, ciphertext, 0, ciphertext.length); + AuthenticatedCipher cipher = cipherProvider.get(); cipher.init(false, secret, nonce); byte[] plaintext = - new byte[backupPayload.getBytes().length - AUTH_TAG_BYTES]; - int decrypted = cipher.process(backupPayload.getBytes(), 0, - backupPayload.getBytes().length, plaintext, 0); + new byte[ciphertext.length - AUTH_TAG_BYTES]; + int decrypted = cipher.process(ciphertext, 0, + ciphertext.length, plaintext, 0); if (decrypted != plaintext.length) throw new AssertionError(); BdfList backup = clientHelper.toList(plaintext); diff --git a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadEncoderImpl.java b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadEncoderImpl.java index db7f9000e..f813c4efd 100644 --- a/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadEncoderImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/socialbackup/BackupPayloadEncoderImpl.java @@ -84,7 +84,10 @@ class BackupPayloadEncoderImpl implements BackupPayloadEncoder { int encrypted = cipher.process(plaintext, 0, plaintext.length, ciphertext, 0); if (encrypted != ciphertext.length) throw new AssertionError(); - return new org.briarproject.briar.api.socialbackup.BackupPayload(ciphertext); + byte[] ciphertextWithNonce = new byte[ciphertext.length + nonce.length]; + System.arraycopy(nonce, 0, ciphertextWithNonce, 0, nonce.length); + System.arraycopy(ciphertext, 0, ciphertextWithNonce, nonce.length, ciphertext.length); + return new org.briarproject.briar.api.socialbackup.BackupPayload(ciphertextWithNonce); } catch (FormatException | GeneralSecurityException e) { throw new AssertionError(e); }