Search in sources :

Example 6 with DigestException

use of java.security.DigestException in project robovm by robovm.

the class DigestExceptionTest method testDigestException06.

/**
     * Test for <code>DigestException(String, Throwable)</code> constructor
     * Assertion: constructs DigestException when <code>cause</code> is null
     * <code>msg</code> is null
     */
public void testDigestException06() {
    DigestException tE = new DigestException(null, null);
    assertNull("getMessage() must return null", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : DigestException(java.security.DigestException)

Example 7 with DigestException

use of java.security.DigestException in project robovm by robovm.

the class DigestExceptionTest method testDigestException02.

/**
     * Test for <code>DigestException(String)</code> constructor Assertion:
     * constructs DigestException with detail message msg. Parameter
     * <code>msg</code> is not null.
     */
public void testDigestException02() {
    DigestException tE;
    for (int i = 0; i < msgs.length; i++) {
        tE = new DigestException(msgs[i]);
        assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
        assertNull("getCause() must return null", tE.getCause());
    }
}
Also used : DigestException(java.security.DigestException)

Example 8 with DigestException

use of java.security.DigestException in project robovm by robovm.

the class DigestExceptionTest method testDigestException04.

/**
     * Test for <code>DigestException(Throwable)</code> constructor Assertion:
     * constructs DigestException when <code>cause</code> is null
     */
public void testDigestException04() {
    Throwable cause = null;
    DigestException tE = new DigestException(cause);
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : DigestException(java.security.DigestException)

Example 9 with DigestException

use of java.security.DigestException in project robovm by robovm.

the class MessageDigest1Test method test_digestLB$LILI.

/**
     * java.security.MessageDigest#digest(byte[], int, int)
     */
public void test_digestLB$LILI() throws Exception {
    MyMessageDigest1 md = new MyMessageDigest1("ABC");
    byte[] b = { 1, 2, 3, 4, 5 };
    assertEquals("incorrect result", 0, md.digest(b, 2, 3));
    assertTrue("digest failed", md.runEngineDigest);
    // Regression for Harmony-1148
    md = new MyMessageDigest1();
    final byte[] bytes = new byte[] { 2, 4, 1 };
    try {
        // buf == null
        md.digest(null, 0, 1);
        fail("No expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    try {
        // offset + len > buf.length
        md.digest(bytes, 0, bytes.length + 1);
        fail("No expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    try {
        // offset + len > Integer.MAX_VALUE
        md.digest(bytes, Integer.MAX_VALUE, 1);
        fail("No expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    // offset<0 and len<0 are passed to provider
    final int offset = -1;
    final int len = -1;
    final int status = 33;
    md = new MyMessageDigest1("ABC") {

        @Override
        public int engineDigest(byte[] arg0, int arg1, int arg2) {
            assertSame("buf", bytes, arg0);
            assertEquals("offset", offset, arg1);
            assertEquals("len", len, arg2);
            return status;
        }
    };
    assertEquals("returned status", status, md.digest(bytes, offset, len));
    try {
        MessageDigest digest = MessageDigest.getInstance("TestDigest", new TestProvider());
        digest.digest(new byte[5], 0, 5);
        fail("expected DigestException");
    } catch (DigestException e) {
    // ok
    }
}
Also used : DigestException(java.security.DigestException) MyMessageDigest1(org.apache.harmony.security.tests.support.MyMessageDigest1) MessageDigest(java.security.MessageDigest)

Example 10 with DigestException

use of java.security.DigestException in project platform_frameworks_base by android.

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)30 MessageDigest (java.security.MessageDigest)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 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