Search in sources :

Example 31 with DigestException

use of java.security.DigestException in project android_frameworks_base by crdroidandroid.

the class ApkSignatureSchemeV2Verifier method computeContentDigests.

private static byte[][] computeContentDigests(int[] digestAlgorithms, DataSource[] contents) throws DigestException {
    // For each digest algorithm the result is computed as follows:
    // 1. Each segment of contents is split into consecutive chunks of 1 MB in size.
    //    The final chunk will be shorter iff the length of segment is not a multiple of 1 MB.
    //    No chunks are produced for empty (zero length) segments.
    // 2. The digest of each chunk is computed over the concatenation of byte 0xa5, the chunk's
    //    length in bytes (uint32 little-endian) and the chunk's contents.
    // 3. The output digest is computed over the concatenation of the byte 0x5a, the number of
    //    chunks (uint32 little-endian) and the concatenation of digests of chunks of all
    //    segments in-order.
    long totalChunkCountLong = 0;
    for (DataSource input : contents) {
        totalChunkCountLong += getChunkCount(input.size());
    }
    if (totalChunkCountLong >= Integer.MAX_VALUE / 1024) {
        throw new DigestException("Too many chunks: " + totalChunkCountLong);
    }
    int totalChunkCount = (int) totalChunkCountLong;
    byte[][] digestsOfChunks = new byte[digestAlgorithms.length][];
    for (int i = 0; i < digestAlgorithms.length; i++) {
        int digestAlgorithm = digestAlgorithms[i];
        int digestOutputSizeBytes = getContentDigestAlgorithmOutputSizeBytes(digestAlgorithm);
        byte[] concatenationOfChunkCountAndChunkDigests = new byte[5 + totalChunkCount * digestOutputSizeBytes];
        concatenationOfChunkCountAndChunkDigests[0] = 0x5a;
        setUnsignedInt32LittleEndian(totalChunkCount, concatenationOfChunkCountAndChunkDigests, 1);
        digestsOfChunks[i] = concatenationOfChunkCountAndChunkDigests;
    }
    byte[] chunkContentPrefix = new byte[5];
    chunkContentPrefix[0] = (byte) 0xa5;
    int chunkIndex = 0;
    MessageDigest[] mds = new MessageDigest[digestAlgorithms.length];
    for (int i = 0; i < digestAlgorithms.length; i++) {
        String jcaAlgorithmName = getContentDigestAlgorithmJcaDigestAlgorithm(digestAlgorithms[i]);
        try {
            mds[i] = MessageDigest.getInstance(jcaAlgorithmName);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(jcaAlgorithmName + " digest not supported", e);
        }
    }
    // TODO: Compute digests of chunks in parallel when beneficial. This requires some research
    // into how to parallelize (if at all) based on the capabilities of the hardware on which
    // this code is running and based on the size of input.
    int dataSourceIndex = 0;
    for (DataSource input : contents) {
        long inputOffset = 0;
        long inputRemaining = input.size();
        while (inputRemaining > 0) {
            int chunkSize = (int) Math.min(inputRemaining, CHUNK_SIZE_BYTES);
            setUnsignedInt32LittleEndian(chunkSize, chunkContentPrefix, 1);
            for (int i = 0; i < mds.length; i++) {
                mds[i].update(chunkContentPrefix);
            }
            try {
                input.feedIntoMessageDigests(mds, inputOffset, chunkSize);
            } catch (IOException e) {
                throw new DigestException("Failed to digest chunk #" + chunkIndex + " of section #" + dataSourceIndex, e);
            }
            for (int i = 0; i < digestAlgorithms.length; i++) {
                int digestAlgorithm = digestAlgorithms[i];
                byte[] concatenationOfChunkCountAndChunkDigests = digestsOfChunks[i];
                int expectedDigestSizeBytes = getContentDigestAlgorithmOutputSizeBytes(digestAlgorithm);
                MessageDigest md = mds[i];
                int actualDigestSizeBytes = md.digest(concatenationOfChunkCountAndChunkDigests, 5 + chunkIndex * expectedDigestSizeBytes, expectedDigestSizeBytes);
                if (actualDigestSizeBytes != expectedDigestSizeBytes) {
                    throw new RuntimeException("Unexpected output size of " + md.getAlgorithm() + " digest: " + actualDigestSizeBytes);
                }
            }
            inputOffset += chunkSize;
            inputRemaining -= chunkSize;
            chunkIndex++;
        }
        dataSourceIndex++;
    }
    byte[][] result = new byte[digestAlgorithms.length][];
    for (int i = 0; i < digestAlgorithms.length; i++) {
        int digestAlgorithm = digestAlgorithms[i];
        byte[] input = digestsOfChunks[i];
        String jcaAlgorithmName = getContentDigestAlgorithmJcaDigestAlgorithm(digestAlgorithm);
        MessageDigest md;
        try {
            md = MessageDigest.getInstance(jcaAlgorithmName);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(jcaAlgorithmName + " digest not supported", e);
        }
        byte[] output = md.digest(input);
        result[i] = output;
    }
    return result;
}
Also used : DigestException(java.security.DigestException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Aggregations

DigestException (java.security.DigestException)31 MessageDigest (java.security.MessageDigest)19 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 IOException (java.io.IOException)7 DataSource (com.android.apksigner.core.util.DataSource)3 ByteBuffer (java.nio.ByteBuffer)2 MyMessageDigest1 (org.apache.harmony.security.tests.support.MyMessageDigest1)2 LimitedLengthInputStream (android.content.pm.LimitedLengthInputStream)1 MacAuthenticatedInputStream (android.content.pm.MacAuthenticatedInputStream)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 ByteBufferDataSource (com.android.apksigner.core.internal.util.ByteBufferDataSource)1 MessageDigestSink (com.android.apksigner.core.internal.util.MessageDigestSink)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 SignatureException (java.security.SignatureException)1 HashMap (java.util.HashMap)1