1 /*
   2  * Copyright (c) 1998, 2014, 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 import java.security.InvalidKeyException;
  25 import java.security.NoSuchAlgorithmException;
  26 import java.security.NoSuchProviderException;
  27 import java.security.Provider;
  28 import java.security.SecureRandom;
  29 import java.util.List;
  30 import javax.crypto.Mac;
  31 import javax.crypto.spec.SecretKeySpec;
  32 
  33 /**
  34  * @test
  35  * @bug 8048603
  36  * @summary Check if doFinal and update operation result in same Mac
  37  * @author Yu-Ching Valerie Peng, Bill Situ, Alexander Fomin
  38  * @library ..
  39  * @run main MacSameTest
  40  * @key randomness
  41  */
  42 public class MacSameTest extends PKCS11Test {
  43 
  44     private static final int MESSAGE_SIZE = 25;
  45     private static final int OFFSET = 5;
  46     private static final int KEY_SIZE = 70;
  47 
  48     /**
  49      * Initialize a message, instantiate a Mac object,
  50      * initialize the object with a SecretKey,
  51      * feed the message into the Mac object
  52      * all at once and get the output MAC as result1.
  53      * Reset the Mac object, chop the message into three pieces,
  54      * feed into the Mac object sequentially, and get the output MAC as result2.
  55      * Finally, compare result1 and result2 and see if they are the same.
  56      *
  57      * @param args the command line arguments
  58      */
  59     public static void main(String[] args) throws Exception {
  60         main(new MacSameTest());
  61     }
  62 
  63     @Override
  64     public void main(Provider p) {
  65         List<String> algorithms = getSupportedAlgorithms("Mac", "Hmac", p);
  66         boolean success = true;
  67         for (String alg : algorithms) {
  68             try {
  69                 doTest(alg, p);
  70             } catch (Exception e) {
  71                 System.out.println("Unexpected exception: " + e);
  72                 e.printStackTrace();
  73                 success = false;
  74             }
  75         }
  76 
  77         if (!success) {
  78             throw new RuntimeException("Test failed");
  79         }
  80     }
  81 
  82     private void doTest(String algo, Provider provider)
  83             throws NoSuchAlgorithmException, NoSuchProviderException,
  84             InvalidKeyException {
  85         System.out.println("Test " + algo);
  86         Mac mac;
  87         try {
  88             mac = Mac.getInstance(algo, provider);
  89         } catch (NoSuchAlgorithmException nsae) {
  90             if ("SunPKCS11-Solaris".equals(provider.getName())) {
  91                 // depending on Solaris configuration,
  92                 // it can support HMAC or not with Mac
  93                 System.out.println("Expected NoSuchAlgorithmException thrown: "
  94                         + nsae);
  95                 return;
  96             }
  97             throw nsae;
  98         }
  99 
 100         byte[] plain = new byte[MESSAGE_SIZE];
 101         for (int i = 0; i < MESSAGE_SIZE; i++) {
 102             plain[i] = (byte) (i % 256);
 103         }
 104 
 105         byte[] tail = new byte[plain.length - OFFSET];
 106         System.arraycopy(plain, OFFSET, tail, 0, tail.length);
 107 
 108         SecureRandom srdm = new SecureRandom();
 109         byte[] keyVal = new byte[KEY_SIZE];
 110         srdm.nextBytes(keyVal);
 111         SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");
 112 
 113         mac.init(keySpec);
 114         byte[] result1 = mac.doFinal(plain);
 115 
 116         mac.reset();
 117         mac.update(plain[0]);
 118         mac.update(plain, 1, OFFSET - 1);
 119         byte[] result2 = mac.doFinal(tail);
 120 
 121         if (!java.util.Arrays.equals(result1, result2)) {
 122             throw new RuntimeException("result1 and result2 are not the same");
 123         }
 124     }
 125 
 126 }