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 7087021 27 * @summary MacClone 28 * @author Jan Luehe 29 */ 30 import javax.crypto.*; 31 32 public class MacClone { 33 34 public static void main(String[] args) throws Exception { 35 36 // 37 // Clone uninitialized Mac object 38 // 39 Mac mac = Mac.getInstance("HmacSHA1", "SunJCE"); 40 Mac macClone = (Mac)mac.clone(); 41 System.out.println(macClone.getProvider().toString()); 42 System.out.println(macClone.getAlgorithm()); 43 boolean thrown = false; 44 try { 45 macClone.update((byte)0x12); 46 } catch (IllegalStateException ise) { 47 thrown = true; 48 } 49 if (!thrown) { 50 throw new Exception("Expected IllegalStateException not thrown"); 51 } 52 53 // 54 // Clone initialized Mac object 55 // 56 KeyGenerator kgen = KeyGenerator.getInstance("DES"); 57 SecretKey skey = kgen.generateKey(); 58 59 mac = Mac.getInstance("HmacSHA1", "SunJCE"); 60 mac.init(skey); 61 macClone = (Mac)mac.clone(); 62 System.out.println(macClone.getProvider().toString()); 63 System.out.println(macClone.getAlgorithm()); 64 mac.update((byte)0x12); 65 macClone.update((byte)0x12); 66 byte[] macFinal = mac.doFinal(); 67 byte[] macCloneFinal = macClone.doFinal(); 68 if (!java.util.Arrays.equals(macFinal, macCloneFinal)) { 69 throw new Exception("MAC results are different"); 70 } 71 } 72 } | 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 7087021 27 * @summary MacClone 28 * @author Jan Luehe 29 */ 30 import javax.crypto.*; 31 import javax.crypto.spec.SecretKeySpec; 32 33 public class MacClone { 34 35 public static void main(String[] args) throws Exception { 36 37 String[] algos = { "HmacMD5", "HmacSHA1", "HmacSHA224", "HmacSHA256", 38 "HmacSHA384", "HmacSHA512" }; 39 KeyGenerator kgen = KeyGenerator.getInstance("DES"); 40 SecretKey skey = kgen.generateKey(); 41 for (String algo : algos) { 42 doTest(algo, skey); 43 } 44 45 String[] algos2 = { "HmacPBESHA1" }; 46 skey = new SecretKeySpec("whatever".getBytes(), "PBE"); 47 for (String algo : algos2) { 48 doTest(algo, skey); 49 } 50 System.out.println("Test Passed"); 51 } 52 53 private static void doTest(String algo, SecretKey skey) throws Exception { 54 // 55 // Clone an uninitialized Mac object 56 // 57 Mac mac = Mac.getInstance(algo, "SunJCE"); 58 Mac macClone = (Mac)mac.clone(); 59 System.out.println(macClone.getProvider().toString()); 60 System.out.println(macClone.getAlgorithm()); 61 boolean thrown = false; 62 try { 63 macClone.update((byte)0x12); 64 } catch (IllegalStateException ise) { 65 thrown = true; 66 } 67 if (!thrown) { 68 throw new Exception("Expected IllegalStateException not thrown"); 69 } 70 71 // 72 // Clone an initialized Mac object 73 // 74 mac = Mac.getInstance(algo, "SunJCE"); 75 mac.init(skey); 76 macClone = (Mac)mac.clone(); 77 System.out.println(macClone.getProvider().toString()); 78 System.out.println(macClone.getAlgorithm()); 79 mac.update((byte)0x12); 80 macClone.update((byte)0x12); 81 byte[] macFinal = mac.doFinal(); 82 byte[] macCloneFinal = macClone.doFinal(); 83 if (!java.util.Arrays.equals(macFinal, macCloneFinal)) { 84 throw new Exception("ERROR: MAC result of init clone is different"); 85 } else System.out.println("MAC check#1 passed"); 86 87 // 88 // Clone an updated Mac object 89 // 90 mac.update((byte)0x12); 91 macClone = (Mac)mac.clone(); 92 mac.update((byte)0x34); 93 macClone.update((byte)0x34); 94 macFinal = mac.doFinal(); 95 macCloneFinal = macClone.doFinal(); 96 if (!java.util.Arrays.equals(macFinal, macCloneFinal)) { 97 throw new Exception("ERROR: MAC result of updated clone is different"); 98 } else System.out.println("MAC check#2 passed"); 99 } 100 } |