112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom/* 212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Copyright (C) 2008 The Android Open Source Project 312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * 412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License"); 512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * you may not use this file except in compliance with the License. 612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * You may obtain a copy of the License at 712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * 812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * 1012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Unless required by applicable law or agreed to in writing, software 1112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 1212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * See the License for the specific language governing permissions and 1412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * limitations under the License. 1512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 1612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 170ac85ead96f1ba7d35f3acadd154de4ef0a8fd87Brian Carlstrompackage com.android.org.bouncycastle.crypto.digests; 1812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 1912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstromimport junit.framework.TestCase; 200ac85ead96f1ba7d35f3acadd154de4ef0a8fd87Brian Carlstromimport com.android.org.bouncycastle.crypto.Digest; 210ac85ead96f1ba7d35f3acadd154de4ef0a8fd87Brian Carlstromimport com.android.org.bouncycastle.crypto.ExtendedDigest; 225fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Rootimport tests.util.SummaryStatistics; 2312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 2412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom/** 2512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Implements unit tests for our JNI wrapper around OpenSSL. We use the 2612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * existing Bouncy Castle implementation as our test oracle. 2712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 2812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrompublic class DigestTest extends TestCase { 2912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 3012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom /** 3112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Processes the two given message digests for the same data and checks 3212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * the results. Requirement is that the results must be equal, the digest 3312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * implementations must have the same properties, and the new implementation 3412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * must be faster than the old one. 3512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * 3612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * @param oldDigest The old digest implementation, provided by Bouncy Castle 3712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * @param newDigest The new digest implementation, provided by OpenSSL 3812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 3912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom public void doTestMessageDigest(Digest oldDigest, Digest newDigest) { 405fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root final int WARMUP = 10; 41a93687809123f32af429ab88bf29fe506212c75dKenny Root final int ITERATIONS = 100; 4212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 4312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom byte[] data = new byte[1024]; 4412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 4512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom byte[] oldHash = new byte[oldDigest.getDigestSize()]; 4612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom byte[] newHash = new byte[newDigest.getDigestSize()]; 4712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 4812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom assertEquals("Hash names must be equal", 4912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom oldDigest.getAlgorithmName(), newDigest.getAlgorithmName()); 5012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom assertEquals("Hash sizes must be equal", 5112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom oldHash.length, newHash.length); 5212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom assertEquals("Hash block sizes must be equal", 5312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom ((ExtendedDigest)oldDigest).getByteLength(), 5412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom ((ExtendedDigest)newDigest).getByteLength()); 5512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom for (int i = 0; i < data.length; i++) { 5612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom data[i] = (byte)i; 5712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 5812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 595fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root SummaryStatistics oldTime = new SummaryStatistics(); 605fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root SummaryStatistics newTime = new SummaryStatistics(); 6112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 625fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root for (int j = 0; j < ITERATIONS + WARMUP; j++) { 635fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root long t0 = System.nanoTime(); 6412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom for (int i = 0; i < 4; i++) { 6512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom oldDigest.update(data, 0, data.length); 6612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 6712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom int oldLength = oldDigest.doFinal(oldHash, 0); 685fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root long t1 = System.nanoTime(); 6912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 705fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root if (j >= WARMUP) { 715fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root oldTime.add(t1 - t0); 725fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root } 7312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 745fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root long t2 = System.nanoTime(); 7512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom for (int i = 0; i < 4; i++) { 7612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom newDigest.update(data, 0, data.length); 7712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 7812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom int newLength = newDigest.doFinal(newHash, 0); 795fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root long t3 = System.nanoTime(); 8012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 815fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root if (j >= WARMUP) { 825fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root newTime.add(t3 - t2); 835fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root } 8412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 8512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom assertEquals("Hash sizes must be equal", oldLength, newLength); 8612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 8712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom for (int i = 0; i < oldLength; i++) { 8812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom assertEquals("Hashes[" + i + "] must be equal", oldHash[i], newHash[i]); 8912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 9012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 9112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 925fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root System.out.println("Time for " + ITERATIONS + " x old hash processing: " 935fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root + oldTime.toString()); 945fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root System.out.println("Time for " + ITERATIONS + " x new hash processing: " 955fe1cd001f38fba460ac0ce5c15b85250e400f25Kenny Root + newTime.toString()); 9612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 9712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 9812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom /** 9912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Tests the MD5 implementation. 10012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 10112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom public void testMD5() { 10212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest oldDigest = new MD5Digest(); 10312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest newDigest = new OpenSSLDigest.MD5(); 10412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom doTestMessageDigest(oldDigest, newDigest); 10512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 10612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 10712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom /** 10812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Tests the SHA-1 implementation. 10912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 11012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom public void testSHA1() { 11112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest oldDigest = new SHA1Digest(); 11212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest newDigest = new OpenSSLDigest.SHA1(); 11312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom doTestMessageDigest(oldDigest, newDigest); 11412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 11512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 11612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom /** 11712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Tests the SHA-256 implementation. 11812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 11912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom public void testSHA256() { 12012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest oldDigest = new SHA256Digest(); 12112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest newDigest = new OpenSSLDigest.SHA256(); 12212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom doTestMessageDigest(oldDigest, newDigest); 12312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 12412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 12512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom /** 12612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Tests the SHA-384 implementation. 12712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 12812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom public void testSHA384() { 12912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest oldDigest = new SHA384Digest(); 13012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest newDigest = new OpenSSLDigest.SHA384(); 13112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom doTestMessageDigest(oldDigest, newDigest); 13212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 13312cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom 13412cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom /** 13512cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom * Tests the SHA-512 implementation. 13612cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom */ 13712cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom public void testSHA512() { 13812cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest oldDigest = new SHA512Digest(); 13912cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom Digest newDigest = new OpenSSLDigest.SHA512(); 14012cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom doTestMessageDigest(oldDigest, newDigest); 14112cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom } 14212cd1f00c2fa1a7f37bf644cecdf7588bdc0b0a9Brian Carlstrom} 143