Home | History | Annotate | Download | only in digests
      1 /*
      2  * Copyright (C) 2008 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 com.android.org.bouncycastle.crypto.digests;
     18 
     19 import junit.framework.TestCase;
     20 import com.android.org.bouncycastle.crypto.Digest;
     21 import com.android.org.bouncycastle.crypto.ExtendedDigest;
     22 import tests.util.SummaryStatistics;
     23 
     24 /**
     25  * Implements unit tests for our JNI wrapper around OpenSSL. We use the
     26  * existing Bouncy Castle implementation as our test oracle.
     27  */
     28 public class DigestTest extends TestCase {
     29 
     30     /**
     31      * Processes the two given message digests for the same data and checks
     32      * the results. Requirement is that the results must be equal, the digest
     33      * implementations must have the same properties, and the new implementation
     34      * must be faster than the old one.
     35      *
     36      * @param oldDigest The old digest implementation, provided by Bouncy Castle
     37      * @param newDigest The new digest implementation, provided by OpenSSL
     38      */
     39     public void doTestMessageDigest(Digest oldDigest, Digest newDigest) {
     40         final int WARMUP = 10;
     41         final int ITERATIONS = 100;
     42 
     43         byte[] data = new byte[1024];
     44 
     45         byte[] oldHash = new byte[oldDigest.getDigestSize()];
     46         byte[] newHash = new byte[newDigest.getDigestSize()];
     47 
     48         assertEquals("Hash names must be equal",
     49                      oldDigest.getAlgorithmName(), newDigest.getAlgorithmName());
     50         assertEquals("Hash sizes must be equal",
     51                      oldHash.length, newHash.length);
     52         assertEquals("Hash block sizes must be equal",
     53                      ((ExtendedDigest)oldDigest).getByteLength(),
     54                      ((ExtendedDigest)newDigest).getByteLength());
     55         for (int i = 0; i < data.length; i++) {
     56             data[i] = (byte)i;
     57         }
     58 
     59         SummaryStatistics oldTime = new SummaryStatistics();
     60         SummaryStatistics newTime = new SummaryStatistics();
     61 
     62         for (int j = 0; j < ITERATIONS + WARMUP; j++) {
     63             long t0 = System.nanoTime();
     64             for (int i = 0; i < 4; i++) {
     65                 oldDigest.update(data, 0, data.length);
     66             }
     67             int oldLength = oldDigest.doFinal(oldHash, 0);
     68             long t1 = System.nanoTime();
     69 
     70             if (j >= WARMUP) {
     71                 oldTime.add(t1 - t0);
     72             }
     73 
     74             long t2 = System.nanoTime();
     75             for (int i = 0; i < 4; i++) {
     76                 newDigest.update(data, 0, data.length);
     77             }
     78             int newLength = newDigest.doFinal(newHash, 0);
     79             long t3 = System.nanoTime();
     80 
     81             if (j >= WARMUP) {
     82               newTime.add(t3 - t2);
     83             }
     84 
     85             assertEquals("Hash sizes must be equal", oldLength, newLength);
     86 
     87             for (int i = 0; i < oldLength; i++) {
     88                 assertEquals("Hashes[" + i + "] must be equal", oldHash[i], newHash[i]);
     89             }
     90         }
     91 
     92         System.out.println("Time for " + ITERATIONS + " x old hash processing: "
     93                 + oldTime.toString());
     94         System.out.println("Time for " + ITERATIONS + " x new hash processing: "
     95                 + newTime.toString());
     96     }
     97 
     98     /**
     99      * Tests the MD5 implementation.
    100      */
    101     public void testMD5() {
    102         Digest oldDigest = new MD5Digest();
    103         Digest newDigest = new OpenSSLDigest.MD5();
    104         doTestMessageDigest(oldDigest, newDigest);
    105     }
    106 
    107     /**
    108      * Tests the SHA-1 implementation.
    109      */
    110     public void testSHA1() {
    111         Digest oldDigest = new SHA1Digest();
    112         Digest newDigest = new OpenSSLDigest.SHA1();
    113         doTestMessageDigest(oldDigest, newDigest);
    114     }
    115 
    116     /**
    117      * Tests the SHA-256 implementation.
    118      */
    119     public void testSHA256() {
    120         Digest oldDigest = new SHA256Digest();
    121         Digest newDigest = new OpenSSLDigest.SHA256();
    122         doTestMessageDigest(oldDigest, newDigest);
    123     }
    124 
    125     /**
    126      * Tests the SHA-384 implementation.
    127      */
    128     public void testSHA384() {
    129         Digest oldDigest = new SHA384Digest();
    130         Digest newDigest = new OpenSSLDigest.SHA384();
    131         doTestMessageDigest(oldDigest, newDigest);
    132     }
    133 
    134     /**
    135      * Tests the SHA-512 implementation.
    136      */
    137     public void testSHA512() {
    138         Digest oldDigest = new SHA512Digest();
    139         Digest newDigest = new OpenSSLDigest.SHA512();
    140         doTestMessageDigest(oldDigest, newDigest);
    141     }
    142 }
    143