Path: blob/master/src/java.base/share/classes/javax/crypto/MacSpi.java
41152 views
/*1* Copyright (c) 1998, 2020, 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* Constructor for subclasses to call.50*/51public MacSpi() {}5253/**54* Returns the length of the MAC in bytes.55*56* @return the MAC length in bytes.57*/58protected abstract int engineGetMacLength();5960/**61* Initializes the MAC with the given (secret) key and algorithm62* parameters.63*64* @param key the (secret) key.65* @param params the algorithm parameters.66*67* @exception InvalidKeyException if the given key is inappropriate for68* initializing this MAC.69* @exception InvalidAlgorithmParameterException if the given algorithm70* parameters are inappropriate for this MAC.71*/72protected abstract void engineInit(Key key,73AlgorithmParameterSpec params)74throws InvalidKeyException, InvalidAlgorithmParameterException ;7576/**77* Processes the given byte.78*79* @param input the input byte to be processed.80*/81protected abstract void engineUpdate(byte input);8283/**84* Processes the first <code>len</code> bytes in <code>input</code>,85* starting at <code>offset</code> inclusive.86*87* @param input the input buffer.88* @param offset the offset in <code>input</code> where the input starts.89* @param len the number of bytes to process.90*/91protected abstract void engineUpdate(byte[] input, int offset, int len);9293/**94* Processes <code>input.remaining()</code> bytes in the ByteBuffer95* <code>input</code>, starting at <code>input.position()</code>.96* Upon return, the buffer's position will be equal to its limit;97* its limit will not have changed.98*99* <p>Subclasses should consider overriding this method if they can100* process ByteBuffers more efficiently than byte arrays.101*102* @param input the ByteBuffer103* @since 1.5104*/105protected void engineUpdate(ByteBuffer input) {106if (input.hasRemaining() == false) {107return;108}109if (input.hasArray()) {110byte[] b = input.array();111int ofs = input.arrayOffset();112int pos = input.position();113int lim = input.limit();114engineUpdate(b, ofs + pos, lim - pos);115input.position(lim);116} else {117int len = input.remaining();118byte[] b = new byte[CipherSpi.getTempArraySize(len)];119while (len > 0) {120int chunk = Math.min(len, b.length);121input.get(b, 0, chunk);122engineUpdate(b, 0, chunk);123len -= chunk;124}125}126}127128/**129* Completes the MAC computation and resets the MAC for further use,130* maintaining the secret key that the MAC was initialized with.131*132* @return the MAC result.133*/134protected abstract byte[] engineDoFinal();135136/**137* Resets the MAC for further use, maintaining the secret key that the138* MAC was initialized with.139*/140protected abstract void engineReset();141142/**143* Returns a clone if the implementation is cloneable.144*145* @return a clone if the implementation is cloneable.146*147* @exception CloneNotSupportedException if this is called148* on an implementation that does not support <code>Cloneable</code>.149*/150public Object clone() throws CloneNotSupportedException {151if (this instanceof Cloneable) {152return super.clone();153} else {154throw new CloneNotSupportedException();155}156}157}158159160