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