| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.security.keystore2; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.security.KeyStoreException; |
| 21 |
import android.security.KeyStoreOperation; |
| 22 |
import android.security.keymaster.KeymasterDefs; |
| 23 |
import android.security.keystore.ArrayUtils; |
| 24 |
|
| 25 |
import libcore.util.EmptyArray; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class KeyStoreCryptoOperationChunkedStreamer implements KeyStoreCryptoOperationStreamer { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
interface Stream { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
byte[] update(@NonNull byte[] input) throws KeyStoreException; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
byte[] finish(byte[] input, byte[] signature) throws KeyStoreException; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private static final int DEFAULT_CHUNK_SIZE_MAX = 32 * 1024; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private static final int DEFAULT_CHUNK_SIZE_THRESHOLD = 2 * 1024; |
| 89 |
|
| 90 |
private final Stream mKeyStoreStream; |
| 91 |
private final int mChunkSizeMax; |
| 92 |
private final int mChunkSizeThreshold; |
| 93 |
private final byte[] mChunk; |
| 94 |
private int mChunkLength = 0; |
| 95 |
private long mConsumedInputSizeBytes; |
| 96 |
private long mProducedOutputSizeBytes; |
| 97 |
|
| 98 |
KeyStoreCryptoOperationChunkedStreamer(Stream operation) { |
| 99 |
this(operation, DEFAULT_CHUNK_SIZE_THRESHOLD, DEFAULT_CHUNK_SIZE_MAX); |
| 100 |
} |
| 101 |
|
| 102 |
KeyStoreCryptoOperationChunkedStreamer(Stream operation, int chunkSizeThreshold) { |
| 103 |
this(operation, chunkSizeThreshold, DEFAULT_CHUNK_SIZE_MAX); |
| 104 |
} |
| 105 |
|
| 106 |
KeyStoreCryptoOperationChunkedStreamer(Stream operation, int chunkSizeThreshold, |
| 107 |
int chunkSizeMax) { |
| 108 |
mChunkLength = 0; |
| 109 |
mConsumedInputSizeBytes = 0; |
| 110 |
mProducedOutputSizeBytes = 0; |
| 111 |
mKeyStoreStream = operation; |
| 112 |
mChunkSizeMax = chunkSizeMax; |
| 113 |
if (chunkSizeThreshold <= 0) { |
| 114 |
mChunkSizeThreshold = 1; |
| 115 |
} else if (chunkSizeThreshold > chunkSizeMax) { |
| 116 |
mChunkSizeThreshold = chunkSizeMax; |
| 117 |
} else { |
| 118 |
mChunkSizeThreshold = chunkSizeThreshold; |
| 119 |
} |
| 120 |
mChunk = new byte[mChunkSizeMax]; |
| 121 |
} |
| 122 |
|
| 123 |
public byte[] update(byte[] input, int inputOffset, int inputLength) throws KeyStoreException { |
| 124 |
if (inputLength == 0 || input == null) { |
| 125 |
|
| 126 |
return EmptyArray.BYTE; |
| 127 |
} |
| 128 |
if (inputLength < 0 || inputOffset < 0 || (inputOffset + inputLength) > input.length) { |
| 129 |
throw new KeyStoreException(KeymasterDefs.KM_ERROR_UNKNOWN_ERROR, |
| 130 |
"Input offset and length out of bounds of input array"); |
| 131 |
} |
| 132 |
|
| 133 |
byte[] output = EmptyArray.BYTE; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if (mChunkLength > 0) { |
| 138 |
|
| 139 |
int inputConsumed = ArrayUtils.copy(input, inputOffset, mChunk, mChunkLength, |
| 140 |
inputLength); |
| 141 |
inputLength -= inputConsumed; |
| 142 |
inputOffset += inputConsumed; |
| 143 |
mChunkLength += inputConsumed; |
| 144 |
if (mChunkLength < mChunkSizeMax) return output; |
| 145 |
byte[] o = mKeyStoreStream.update(mChunk); |
| 146 |
if (o != null) { |
| 147 |
output = ArrayUtils.concat(output, o); |
| 148 |
} |
| 149 |
mConsumedInputSizeBytes += inputConsumed; |
| 150 |
mChunkLength = 0; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
while (inputLength >= mChunkSizeThreshold) { |
| 155 |
int nextChunkSize = inputLength < mChunkSizeMax ? inputLength : mChunkSizeMax; |
| 156 |
byte[] o = mKeyStoreStream.update(ArrayUtils.subarray(input, inputOffset, |
| 157 |
nextChunkSize)); |
| 158 |
inputLength -= nextChunkSize; |
| 159 |
inputOffset += nextChunkSize; |
| 160 |
mConsumedInputSizeBytes += nextChunkSize; |
| 161 |
if (o != null) { |
| 162 |
output = ArrayUtils.concat(output, o); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if (inputLength > 0) { |
| 169 |
mChunkLength = ArrayUtils.copy(input, inputOffset, mChunk, 0, inputLength); |
| 170 |
mConsumedInputSizeBytes += inputLength; |
| 171 |
} |
| 172 |
|
| 173 |
mProducedOutputSizeBytes += output.length; |
| 174 |
return output; |
| 175 |
} |
| 176 |
|
| 177 |
public byte[] doFinal(byte[] input, int inputOffset, int inputLength, |
| 178 |
byte[] signature) throws KeyStoreException { |
| 179 |
byte[] output = update(input, inputOffset, inputLength); |
| 180 |
byte[] finalChunk = ArrayUtils.subarray(mChunk, 0, mChunkLength); |
| 181 |
byte[] o = mKeyStoreStream.finish(finalChunk, signature); |
| 182 |
|
| 183 |
if (o != null) { |
| 184 |
|
| 185 |
|
| 186 |
mProducedOutputSizeBytes += o.length; |
| 187 |
if (output != null) { |
| 188 |
output = ArrayUtils.concat(output, o); |
| 189 |
} else { |
| 190 |
output = o; |
| 191 |
} |
| 192 |
} |
| 193 |
return output; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public long getConsumedInputSizeBytes() { |
| 198 |
return mConsumedInputSizeBytes; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public long getProducedOutputSizeBytes() { |
| 203 |
return mProducedOutputSizeBytes; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public static class MainDataStream implements Stream { |
| 213 |
|
| 214 |
private final KeyStoreOperation mOperation; |
| 215 |
|
| 216 |
MainDataStream(KeyStoreOperation operation) { |
| 217 |
mOperation = operation; |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public byte[] update(byte[] input) throws KeyStoreException { |
| 222 |
return mOperation.update(input); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public byte[] finish(byte[] input, byte[] signature) |
| 227 |
throws KeyStoreException { |
| 228 |
return mOperation.finish(input, signature); |
| 229 |
} |
| 230 |
} |
| 231 |
} |