/* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.server.backup.encryption.tasks; import static com.android.internal.util.Preconditions.checkState; import android.annotation.Nullable; import android.app.backup.BackupDataInput; import com.android.server.backup.encryption.chunk.ChunkHash; import com.android.server.backup.encryption.chunking.ChunkEncryptor; import com.android.server.backup.encryption.chunking.ChunkHasher; import com.android.server.backup.encryption.chunking.EncryptedChunk; import com.android.server.backup.encryption.kv.KeyValueListingBuilder; import com.android.server.backup.encryption.protos.nano.KeyValueListingProto; import com.android.server.backup.encryption.protos.nano.KeyValuePairProto; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import javax.crypto.IllegalBlockSizeException; import javax.crypto.SecretKey; /** * Reads key value backup data from an input, converts each pair into a chunk and encrypts the * chunks. * *
The caller should pass in the key value listing from the previous backup, if there is one. * This class emits chunks for both existing and new pairs, using the provided listing to * determine the hashes of pairs that already exist. During the backup it computes the new listing, * which the caller should store on disk and pass in at the start of the next backup. * *
Also computes the message digest, which is {@code SHA-256(chunk hashes sorted * lexicographically)}. */ public class KvBackupEncrypter implements BackupEncrypter { private final BackupDataInput mBackupDataInput; private KeyValueListingProto.KeyValueListing mOldKeyValueListing; @Nullable private KeyValueListingBuilder mNewKeyValueListing; /** * Constructs a new instance which reads data from the given input. * *
By default this performs non-incremental backup, call {@link #setOldKeyValueListing} to
* perform incremental backup.
*/
public KvBackupEncrypter(BackupDataInput backupDataInput) {
mBackupDataInput = backupDataInput;
mOldKeyValueListing = KeyValueListingBuilder.emptyListing();
}
/** Sets the old listing to perform incremental backup against. */
public void setOldKeyValueListing(KeyValueListingProto.KeyValueListing oldKeyValueListing) {
mOldKeyValueListing = oldKeyValueListing;
}
@Override
public Result backup(
SecretKey secretKey,
@Nullable byte[] unusedFingerprintMixerSalt,
Set You must call {@link #backup} first.
*/
public KeyValueListingProto.KeyValueListing getNewKeyValueListing() {
checkState(mNewKeyValueListing != null, "Must call backup() first");
return mNewKeyValueListing.build();
}
private static Map