Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.security.keystore.recovery; |
| 18 | |
| 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
Dmitry Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
| 24 | |
| 25 | import com.android.internal.util.Preconditions; |
| 26 | |
| 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.RetentionPolicy; |
| 29 | |
| 30 | /** |
| 31 | * Collection of parameters which define a key derivation function. |
Aseem Kumar | c1742e5 | 2018-03-12 14:34:58 -0700 | [diff] [blame] | 32 | * Currently only supports salted SHA-256. |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 33 | * |
| 34 | * @hide |
| 35 | */ |
Dmitry Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame] | 36 | @SystemApi |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 37 | public final class KeyDerivationParams implements Parcelable { |
Robert Berry | 52c15f1 | 2018-03-29 10:21:50 +0100 | [diff] [blame] | 38 | |
| 39 | // IMPORTANT! PLEASE READ! |
| 40 | // ----------------------- |
| 41 | // If you edit this file (e.g., to add new fields), please MAKE SURE to also do the following: |
| 42 | // - Update the #writeToParcel(Parcel) method below |
| 43 | // - Update the #(Parcel) constructor below |
| 44 | // - Update android.security.keystore.recovery.KeyChainSnapshotTest to make sure nobody |
| 45 | // accidentally breaks your fields in the Parcel in the future. |
| 46 | // - Update com.android.server.locksettings.recoverablekeystore.serialization |
| 47 | // .KeyChainSnapshotSerializer to correctly serialize your new field |
| 48 | // - Update com.android.server.locksettings.recoverablekeystore.serialization |
| 49 | // .KeyChainSnapshotSerializer to correctly deserialize your new field |
| 50 | // - Update com.android.server.locksettings.recoverablekeystore.serialization |
| 51 | // .KeychainSnapshotSerializerTest to make sure nobody breaks serialization of your field |
| 52 | // in the future. |
| 53 | |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 54 | private final int mAlgorithm; |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 55 | private final byte[] mSalt; |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 56 | private final int mMemoryDifficulty; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 57 | |
| 58 | /** @hide */ |
| 59 | @Retention(RetentionPolicy.SOURCE) |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 60 | @IntDef(prefix = {"ALGORITHM_"}, value = {ALGORITHM_SHA256, ALGORITHM_SCRYPT}) |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 61 | public @interface KeyDerivationAlgorithm { |
| 62 | } |
| 63 | |
| 64 | /** |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 65 | * Salted SHA256. |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 66 | */ |
| 67 | public static final int ALGORITHM_SHA256 = 1; |
| 68 | |
| 69 | /** |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 70 | * SCRYPT. |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 71 | */ |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 72 | public static final int ALGORITHM_SCRYPT = 2; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 75 | * Creates instance of the class to to derive keys using salted SHA256 hash. |
| 76 | * |
| 77 | * <p>The salted SHA256 hash is computed over the concatenation of four byte strings, salt_len + |
Bo Zhu | ba94b9a | 2018-03-29 16:42:29 -0700 | [diff] [blame] | 78 | * salt + key_material_len + key_material, where salt_len and key_material_len are 4-byte, and |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 79 | * denote the number of bytes for salt and key_material, respectively. |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 80 | */ |
Dmitry Dementyev | fd4ae0b | 2018-03-23 11:06:24 -0700 | [diff] [blame] | 81 | public static @NonNull KeyDerivationParams createSha256Params(@NonNull byte[] salt) { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 82 | return new KeyDerivationParams(ALGORITHM_SHA256, salt); |
| 83 | } |
| 84 | |
Dmitry Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame] | 85 | /** |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 86 | * Creates instance of the class to to derive keys using the password hashing algorithm SCRYPT. |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 87 | * |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 88 | * <p>We expose only one tuning parameter of SCRYPT, which is the memory cost parameter (i.e. N |
| 89 | * in <a href="https://www.tarsnap.com/scrypt/scrypt.pdf">the SCRYPT paper</a>). Regular/default |
| 90 | * values are used for the other parameters, to keep the overall running time low. Specifically, |
| 91 | * the parallelization parameter p is 1, the block size parameter r is 8, and the hashing output |
| 92 | * length is 32-byte. |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 93 | */ |
Dmitry Dementyev | fd4ae0b | 2018-03-23 11:06:24 -0700 | [diff] [blame] | 94 | public static @NonNull KeyDerivationParams createScryptParams( |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 95 | @NonNull byte[] salt, int memoryDifficulty) { |
| 96 | return new KeyDerivationParams(ALGORITHM_SCRYPT, salt, memoryDifficulty); |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Dmitry Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame] | 100 | * @hide |
| 101 | */ |
Dmitry Dementyev | fd4ae0b | 2018-03-23 11:06:24 -0700 | [diff] [blame] | 102 | private KeyDerivationParams(@KeyDerivationAlgorithm int algorithm, @NonNull byte[] salt) { |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 103 | this(algorithm, salt, /*memoryDifficulty=*/ -1); |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @hide |
| 108 | */ |
Dmitry Dementyev | 86f5bb1 | 2018-03-27 16:58:50 -0700 | [diff] [blame] | 109 | private KeyDerivationParams(@KeyDerivationAlgorithm int algorithm, @NonNull byte[] salt, |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 110 | int memoryDifficulty) { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 111 | mAlgorithm = algorithm; |
| 112 | mSalt = Preconditions.checkNotNull(salt); |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 113 | mMemoryDifficulty = memoryDifficulty; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Gets algorithm. |
| 118 | */ |
| 119 | public @KeyDerivationAlgorithm int getAlgorithm() { |
| 120 | return mAlgorithm; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Gets salt. |
| 125 | */ |
| 126 | public @NonNull byte[] getSalt() { |
| 127 | return mSalt; |
| 128 | } |
| 129 | |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 130 | /** |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 131 | * Gets the memory difficulty parameter for the hashing algorithm. |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 132 | * |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 133 | * <p>The effect of this parameter depends on the algorithm in use. For example, please see |
| 134 | * {@link #createScryptParams(byte[], int)} for choosing the parameter for SCRYPT. |
| 135 | * |
| 136 | * <p>If the specific algorithm does not support such a memory difficulty parameter, its value |
| 137 | * should be -1. |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 138 | */ |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 139 | public int getMemoryDifficulty() { |
| 140 | return mMemoryDifficulty; |
Bo Zhu | e066a59 | 2018-03-19 22:32:27 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 143 | public static final Parcelable.Creator<KeyDerivationParams> CREATOR = |
| 144 | new Parcelable.Creator<KeyDerivationParams>() { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 145 | public KeyDerivationParams createFromParcel(Parcel in) { |
| 146 | return new KeyDerivationParams(in); |
| 147 | } |
| 148 | |
| 149 | public KeyDerivationParams[] newArray(int length) { |
| 150 | return new KeyDerivationParams[length]; |
| 151 | } |
| 152 | }; |
| 153 | |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 154 | @Override |
| 155 | public void writeToParcel(Parcel out, int flags) { |
| 156 | out.writeInt(mAlgorithm); |
| 157 | out.writeByteArray(mSalt); |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 158 | out.writeInt(mMemoryDifficulty); |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @hide |
| 163 | */ |
| 164 | protected KeyDerivationParams(Parcel in) { |
| 165 | mAlgorithm = in.readInt(); |
| 166 | mSalt = in.createByteArray(); |
Bo Zhu | 40d8a45b | 2018-03-21 20:07:43 -0700 | [diff] [blame] | 167 | mMemoryDifficulty = in.readInt(); |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public int describeContents() { |
| 172 | return 0; |
| 173 | } |
| 174 | } |