1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6414899
  27  * @summary Ensure the cloning functionality works.
  28  * @author Valerie Peng
  29  * @library ..
  30  * @key randomness
  31  * @modules jdk.crypto.cryptoki
  32  * @run main/othervm TestCloning
  33  * @run main/othervm TestCloning sm
  34  */
  35 
  36 import java.security.MessageDigest;
  37 import java.security.Provider;
  38 import java.util.Arrays;
  39 import java.util.Random;
  40 
  41 public class TestCloning extends PKCS11Test {
  42 
  43     private static final String[] ALGOS = {
  44         "MD2", "MD5", "SHA1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"
  45     };
  46 
  47     public static void main(String[] args) throws Exception {
  48         main(new TestCloning(), args);
  49     }
  50 
  51     private static final byte[] data1 = new byte[10];
  52     private static final byte[] data2 = new byte[10*1024];
  53 
  54 
  55     @Override
  56     public void main(Provider p) throws Exception {
  57         Random r = new Random();
  58         byte[] data1 = new byte[10];
  59         byte[] data2 = new byte[2*1024];
  60         r.nextBytes(data1);
  61         r.nextBytes(data2);
  62         System.out.println("Testing against provider " + p.getName());
  63         for (int i = 0; i < ALGOS.length; i++) {
  64             if (p.getService("MessageDigest", ALGOS[i]) == null) {
  65                 System.out.println(ALGOS[i] + " is not supported, skipping");
  66                 continue;
  67             } else {
  68                 System.out.println("Testing " + ALGOS[i] + " of " + p.getName());
  69                 MessageDigest md = MessageDigest.getInstance(ALGOS[i], p);
  70                 try {
  71                     md = testCloning(md, p);
  72                     // repeat the test again after generating digest once
  73                     for (int j = 0; j < 10; j++) {
  74                         md = testCloning(md, p);
  75                     }
  76                 } catch (Exception ex) {
  77                     if (ALGOS[i] == "MD2" &&
  78                         p.getName().equalsIgnoreCase("SunPKCS11-NSS")) {
  79                         // known bug in NSS; ignore for now
  80                         System.out.println("Ignore Known bug in MD2 of NSS");
  81                         continue;
  82                     }
  83                     throw ex;
  84                 }
  85             }
  86         }
  87     }
  88 
  89     private static MessageDigest testCloning(MessageDigest mdObj, Provider p)
  90         throws Exception {
  91 
  92         // copy#0: clone at state BLANK w/o any data
  93         MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();
  94 
  95         // copy#1: clone again at state BUFFERED w/ very short data
  96         mdObj.update(data1);
  97         mdCopy0.update(data1);
  98         MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();
  99 
 100         // copy#2: clone again after updating it w/ long data to trigger
 101         // the state into INIT
 102         mdObj.update(data2);
 103         mdCopy0.update(data2);
 104         mdCopy1.update(data2);
 105         MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();
 106 
 107         // copy#3: clone again after updating it w/ very short data
 108         mdObj.update(data1);
 109         mdCopy0.update(data1);
 110         mdCopy1.update(data1);
 111         mdCopy2.update(data1);
 112         MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();
 113 
 114         // copy#4: clone again after updating it w/ long data
 115         mdObj.update(data2);
 116         mdCopy0.update(data2);
 117         mdCopy1.update(data2);
 118         mdCopy2.update(data2);
 119         mdCopy3.update(data2);
 120         MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();
 121 
 122         // check digest equalities
 123         byte[] answer = mdObj.digest();
 124         byte[] result0 = mdCopy0.digest();
 125         byte[] result1 = mdCopy1.digest();
 126         byte[] result2 = mdCopy2.digest();
 127         byte[] result3 = mdCopy3.digest();
 128         byte[] result4 = mdCopy4.digest();
 129 
 130 
 131         check(answer, result0, "copy0");
 132         check(answer, result1, "copy1");
 133         check(answer, result2, "copy2");
 134         check(answer, result3, "copy3");
 135         check(answer, result4, "copy4");
 136 
 137         return mdCopy3;
 138     }
 139 
 140     private static void check(byte[] d1, byte[] d2, String copyName)
 141             throws Exception {
 142         if (Arrays.equals(d1, d2) == false) {
 143             throw new RuntimeException(copyName + " digest mismatch!");
 144         }
 145     }
 146 }
 147 
--- EOF ---