Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/crypto/MacSpi.java
38829 views
/*1* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto;2627import java.security.*;28import java.security.spec.*;2930import java.nio.ByteBuffer;3132/**33* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)34* for the <code>Mac</code> class.35* All the abstract methods in this class must be implemented by each36* cryptographic service provider who wishes to supply the implementation37* of a particular MAC algorithm.38*39* <p> Implementations are free to implement the Cloneable interface.40*41* @author Jan Luehe42*43* @since 1.444*/4546public abstract class MacSpi {4748/**49* Returns the length of the MAC in bytes.50*51* @return the MAC length in bytes.52*/53protected abstract int engineGetMacLength();5455/**56* Initializes the MAC with the given (secret) key and algorithm57* parameters.58*59* @param key the (secret) key.60* @param params the algorithm parameters.61*62* @exception InvalidKeyException if the given key is inappropriate for63* initializing this MAC.64* @exception InvalidAlgorithmParameterException if the given algorithm65* parameters are inappropriate for this MAC.66*/67protected abstract void engineInit(Key key,68AlgorithmParameterSpec params)69throws InvalidKeyException, InvalidAlgorithmParameterException ;7071/**72* Processes the given byte.73*74* @param input the input byte to be processed.75*/76protected abstract void engineUpdate(byte input);7778/**79* Processes the first <code>len</code> bytes in <code>input</code>,80* starting at <code>offset</code> inclusive.81*82* @param input the input buffer.83* @param offset the offset in <code>input</code> where the input starts.84* @param len the number of bytes to process.85*/86protected abstract void engineUpdate(byte[] input, int offset, int len);8788/**89* Processes <code>input.remaining()</code> bytes in the ByteBuffer90* <code>input</code>, starting at <code>input.position()</code>.91* Upon return, the buffer's position will be equal to its limit;92* its limit will not have changed.93*94* <p>Subclasses should consider overriding this method if they can95* process ByteBuffers more efficiently than byte arrays.96*97* @param input the ByteBuffer98* @since 1.599*/100protected void engineUpdate(ByteBuffer input) {101if (input.hasRemaining() == false) {102return;103}104if (input.hasArray()) {105byte[] b = input.array();106int ofs = input.arrayOffset();107int pos = input.position();108int lim = input.limit();109engineUpdate(b, ofs + pos, lim - pos);110input.position(lim);111} else {112int len = input.remaining();113byte[] b = new byte[CipherSpi.getTempArraySize(len)];114while (len > 0) {115int chunk = Math.min(len, b.length);116input.get(b, 0, chunk);117engineUpdate(b, 0, chunk);118len -= chunk;119}120}121}122123/**124* Completes the MAC computation and resets the MAC for further use,125* maintaining the secret key that the MAC was initialized with.126*127* @return the MAC result.128*/129protected abstract byte[] engineDoFinal();130131/**132* Resets the MAC for further use, maintaining the secret key that the133* MAC was initialized with.134*/135protected abstract void engineReset();136137/**138* Returns a clone if the implementation is cloneable.139*140* @return a clone if the implementation is cloneable.141*142* @exception CloneNotSupportedException if this is called143* on an implementation that does not support <code>Cloneable</code>.144*/145public Object clone() throws CloneNotSupportedException {146if (this instanceof Cloneable) {147return super.clone();148} else {149throw new CloneNotSupportedException();150}151}152}153154155