changeset 15362:5c93d90ca062

8157579: com/sun/crypto/provider/Mac/MacClone.java failed on solaris12(sparcv9 and x86) Summary: Changed the MAC impl to fall back to SUN provider (or through provider list) if the most preferred impl does not support cloning Reviewed-by: vinnie, mullan
author valeriep
date Fri, 12 Aug 2016 00:39:34 +0000
parents 8559f6e0a25d
children 85217e8e1070
files src/java.base/share/classes/com/sun/crypto/provider/HmacCore.java
diffstat 1 files changed, 31 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/com/sun/crypto/provider/HmacCore.java	Fri Aug 12 00:10:07 2016 +0000
+++ b/src/java.base/share/classes/com/sun/crypto/provider/HmacCore.java	Fri Aug 12 00:39:34 2016 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -53,10 +53,37 @@
     private final int blockLen;
 
     /**
-     * Standard constructor, creates a new HmacCore instance using the
-     * specified MessageDigest object.
+     * Standard constructor, creates a new HmacCore instance instantiating
+     * a MessageDigest of the specified name.
      */
-    HmacCore(MessageDigest md, int bl) {
+    HmacCore(String digestAlgo, int bl) throws NoSuchAlgorithmException {
+        MessageDigest md = MessageDigest.getInstance(digestAlgo);
+        if (!(md instanceof Cloneable)) {
+            // use SUN provider if the most preferred one does not support
+            // cloning
+            Provider sun = Security.getProvider("SUN");
+            if (sun != null) {
+                md = MessageDigest.getInstance(digestAlgo, sun);
+            } else {
+                String noCloneProv = md.getProvider().getName();
+                // if no Sun provider, use provider list
+                Provider[] provs = Security.getProviders();
+                for (Provider p : provs) {
+                    try {
+                        if (!p.getName().equals(noCloneProv)) {
+                            MessageDigest md2 =
+                                MessageDigest.getInstance(digestAlgo, p);
+                            if (md2 instanceof Cloneable) {
+                                md = md2;
+                                break;
+                            }
+                        }
+                    } catch (NoSuchAlgorithmException nsae) {
+                        continue;
+                    }
+                }
+            }
+        }
         this.md = md;
         this.blockLen = bl;
         this.k_ipad = new byte[blockLen];
@@ -65,14 +92,6 @@
     }
 
     /**
-     * Standard constructor, creates a new HmacCore instance instantiating
-     * a MessageDigest of the specified name.
-     */
-    HmacCore(String digestAlgorithm, int bl) throws NoSuchAlgorithmException {
-        this(MessageDigest.getInstance(digestAlgorithm), bl);
-    }
-
-    /**
      * Returns the length of the HMAC in bytes.
      *
      * @return the HMAC length in bytes.