mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Encode nonce in backup payload
This commit is contained in:
@@ -11,6 +11,6 @@ import java.security.GeneralSecurityException;
|
|||||||
public interface BackupPayloadDecoder {
|
public interface BackupPayloadDecoder {
|
||||||
SocialBackup decodeBackupPayload(
|
SocialBackup decodeBackupPayload(
|
||||||
SecretKey secret,
|
SecretKey secret,
|
||||||
BackupPayload backupPayload, byte[] nonce) throws FormatException,
|
BackupPayload backupPayload) throws FormatException,
|
||||||
GeneralSecurityException;
|
GeneralSecurityException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import javax.inject.Inject;
|
|||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
|
||||||
import static org.briarproject.briar.socialbackup.SocialBackupConstants.AUTH_TAG_BYTES;
|
import static org.briarproject.briar.socialbackup.SocialBackupConstants.AUTH_TAG_BYTES;
|
||||||
|
import static org.briarproject.briar.socialbackup.SocialBackupConstants.NONCE_BYTES;
|
||||||
|
|
||||||
public class BackupPayloadDecoderImpl {
|
public class BackupPayloadDecoderImpl {
|
||||||
private final ClientHelper clientHelper;
|
private final ClientHelper clientHelper;
|
||||||
@@ -50,15 +51,22 @@ public class BackupPayloadDecoderImpl {
|
|||||||
|
|
||||||
public SocialBackup decodeBackupPayload(
|
public SocialBackup decodeBackupPayload(
|
||||||
SecretKey secret,
|
SecretKey secret,
|
||||||
BackupPayload backupPayload, byte[] nonce)
|
BackupPayload backupPayload)
|
||||||
throws FormatException, GeneralSecurityException {
|
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();
|
AuthenticatedCipher cipher = cipherProvider.get();
|
||||||
cipher.init(false, secret, nonce);
|
cipher.init(false, secret, nonce);
|
||||||
byte[] plaintext =
|
byte[] plaintext =
|
||||||
new byte[backupPayload.getBytes().length - AUTH_TAG_BYTES];
|
new byte[ciphertext.length - AUTH_TAG_BYTES];
|
||||||
int decrypted = cipher.process(backupPayload.getBytes(), 0,
|
int decrypted = cipher.process(ciphertext, 0,
|
||||||
backupPayload.getBytes().length, plaintext, 0);
|
ciphertext.length, plaintext, 0);
|
||||||
if (decrypted != plaintext.length) throw new AssertionError();
|
if (decrypted != plaintext.length) throw new AssertionError();
|
||||||
|
|
||||||
BdfList backup = clientHelper.toList(plaintext);
|
BdfList backup = clientHelper.toList(plaintext);
|
||||||
|
|||||||
@@ -84,7 +84,10 @@ class BackupPayloadEncoderImpl implements BackupPayloadEncoder {
|
|||||||
int encrypted = cipher.process(plaintext, 0, plaintext.length,
|
int encrypted = cipher.process(plaintext, 0, plaintext.length,
|
||||||
ciphertext, 0);
|
ciphertext, 0);
|
||||||
if (encrypted != ciphertext.length) throw new AssertionError();
|
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) {
|
} catch (FormatException | GeneralSecurityException e) {
|
||||||
throw new AssertionError(e);
|
throw new AssertionError(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user