11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.security.provider; 27 28 import java.security.MessageDigestSpi; 29 import java.security.DigestException; 30 import java.security.ProviderException; 31 32 /** 33 * Common base message digest implementation for the Sun provider. 34 * It implements all the JCA methods as suitable for a Java message digest 35 * implementation of an algorithm based on a compression function (as all 36 * commonly used algorithms are). The individual digest subclasses only need to 37 * implement the following methods: 38 * 39 * . abstract void implCompress(byte[] b, int ofs); 40 * . abstract void implDigest(byte[] out, int ofs); 41 * . abstract void implReset(); 42 * 43 * See the inline documentation for details. 44 * 45 * @since 1.5 46 * @author Andreas Sterbenz 47 */ 48 abstract class DigestBase extends MessageDigestSpi implements Cloneable { 49 50 // one element byte array, temporary storage for update(byte) 119 // compress completed block now 120 implCompress(buffer, 0); 121 bufOfs = 0; 122 } 123 } 124 // compress complete blocks 125 if (len >= blockSize) { 126 int limit = ofs + len; 127 ofs = implCompressMultiBlock(b, ofs, limit - blockSize); 128 len = limit - ofs; 129 } 130 // copy remainder to buffer 131 if (len > 0) { 132 System.arraycopy(b, ofs, buffer, 0, len); 133 bufOfs = len; 134 } 135 } 136 137 // compress complete blocks 138 private int implCompressMultiBlock(byte[] b, int ofs, int limit) { 139 for (; ofs <= limit; ofs += blockSize) { 140 implCompress(b, ofs); 141 } 142 return ofs; 143 } 144 145 // reset this object. See JCA doc. 146 protected final void engineReset() { 147 if (bytesProcessed == 0) { 148 // already reset, ignore 149 return; 150 } 151 implReset(); 152 bufOfs = 0; 153 bytesProcessed = 0; 154 } 155 156 // return the digest. See JCA doc. 157 protected final byte[] engineDigest() { 158 byte[] b = new byte[digestLength]; 159 try { 160 engineDigest(b, 0, b.length); 161 } catch (DigestException e) { 162 throw (ProviderException) | 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.security.provider; 27 28 import java.security.MessageDigestSpi; 29 import java.security.DigestException; 30 import java.security.ProviderException; 31 import java.util.Objects; 32 33 import jdk.internal.HotSpotIntrinsicCandidate; 34 35 /** 36 * Common base message digest implementation for the Sun provider. 37 * It implements all the JCA methods as suitable for a Java message digest 38 * implementation of an algorithm based on a compression function (as all 39 * commonly used algorithms are). The individual digest subclasses only need to 40 * implement the following methods: 41 * 42 * . abstract void implCompress(byte[] b, int ofs); 43 * . abstract void implDigest(byte[] out, int ofs); 44 * . abstract void implReset(); 45 * 46 * See the inline documentation for details. 47 * 48 * @since 1.5 49 * @author Andreas Sterbenz 50 */ 51 abstract class DigestBase extends MessageDigestSpi implements Cloneable { 52 53 // one element byte array, temporary storage for update(byte) 122 // compress completed block now 123 implCompress(buffer, 0); 124 bufOfs = 0; 125 } 126 } 127 // compress complete blocks 128 if (len >= blockSize) { 129 int limit = ofs + len; 130 ofs = implCompressMultiBlock(b, ofs, limit - blockSize); 131 len = limit - ofs; 132 } 133 // copy remainder to buffer 134 if (len > 0) { 135 System.arraycopy(b, ofs, buffer, 0, len); 136 bufOfs = len; 137 } 138 } 139 140 // compress complete blocks 141 private int implCompressMultiBlock(byte[] b, int ofs, int limit) { 142 implCompressMultiBlockCheck(b, ofs, limit); 143 return implCompressMultiBlock0(b, ofs, limit); 144 } 145 146 @HotSpotIntrinsicCandidate 147 private int implCompressMultiBlock0(byte[] b, int ofs, int limit) { 148 for (; ofs <= limit; ofs += blockSize) { 149 implCompress(b, ofs); 150 } 151 return ofs; 152 } 153 154 private void implCompressMultiBlockCheck(byte[] b, int ofs, int limit) { 155 if (limit < 0) { 156 return; // not an error because implCompressMultiBlockImpl won't execute if limit < 0 157 // and an exception is thrown if ofs < 0. 158 } 159 160 Objects.requireNonNull(b); 161 162 if (ofs < 0 || ofs >= b.length) { 163 throw new ArrayIndexOutOfBoundsException(ofs); 164 } 165 166 int endIndex = (limit / blockSize) * blockSize + blockSize - 1; 167 if (endIndex >= b.length) { 168 throw new ArrayIndexOutOfBoundsException(endIndex); 169 } 170 } 171 172 // reset this object. See JCA doc. 173 protected final void engineReset() { 174 if (bytesProcessed == 0) { 175 // already reset, ignore 176 return; 177 } 178 implReset(); 179 bufOfs = 0; 180 bytesProcessed = 0; 181 } 182 183 // return the digest. See JCA doc. 184 protected final byte[] engineDigest() { 185 byte[] b = new byte[digestLength]; 186 try { 187 engineDigest(b, 0, b.length); 188 } catch (DigestException e) { 189 throw (ProviderException) |