Encode nonce in backup payload

This commit is contained in:
ameba23
2021-04-19 11:37:46 +02:00
parent bdbc377c8f
commit ae35354e82
3 changed files with 17 additions and 6 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);
}