001 /* MacSpi.java -- The MAC service provider interface. 002 Copyright (C) 2004 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package javax.crypto; 040 041 import java.nio.ByteBuffer; 042 import java.security.InvalidAlgorithmParameterException; 043 import java.security.InvalidKeyException; 044 import java.security.Key; 045 import java.security.spec.AlgorithmParameterSpec; 046 047 /** 048 * This is the <i>Service Provider Interface</i> (<b>SPI</b>) for the 049 * {@link Mac} class. 050 * 051 * <p>Providers wishing to implement a Mac must subclass this class and 052 * provide appropriate implementations of all its abstract methods, 053 * then provide an entry pointing to this implementation in the master 054 * {@link java.security.Provider} class. 055 * 056 * <p>Implementations may optionally implement the {@link 057 * java.lang.Cloneable} interface. 058 * 059 * @author Casey Marshall (csm@gnu.org) 060 * @since 1.4 061 */ 062 public abstract class MacSpi 063 { 064 065 // Constructor. 066 // ------------------------------------------------------------------------ 067 068 /** 069 * Create a new MacSpi instance. 070 */ 071 public MacSpi() 072 { 073 } 074 075 // Instance methods. 076 // ------------------------------------------------------------------------ 077 078 /** 079 * Returns a clone of this instance if cloning is supported. 080 * 081 * @return A clone of this instance. 082 * @throws java.lang.CloneNotSupportedException If this instance does 083 * not support cloneing. 084 */ 085 public Object clone() throws CloneNotSupportedException 086 { 087 return super.clone(); 088 } 089 090 // Abstract instance methods. 091 // ------------------------------------------------------------------------ 092 093 /** 094 * Finalize the computation of this MAC and return the result as a 095 * byte array. 096 * 097 * @return The MAC. 098 */ 099 protected abstract byte[] engineDoFinal(); 100 101 /** 102 * Return the total length, in bytes, of the computed MAC (the length 103 * of the byte array returned by {@link #doFinal()}. 104 * 105 * @return The MAC length. 106 */ 107 protected abstract int engineGetMacLength(); 108 109 /** 110 * Initialize (or re-initialize) this instance. 111 * 112 * @param key The key to use. 113 * @param params The parameters to use. 114 * @throws java.security.InvalidAlgorithmParameterException If this 115 * instance rejects the specified parameters. 116 * @throws java.security.InvalidKeyException If this instance rejects 117 * the specified key. 118 */ 119 protected abstract void engineInit(Key key, AlgorithmParameterSpec params) 120 throws InvalidAlgorithmParameterException, InvalidKeyException; 121 122 /** 123 * Reset this instance. After this method succeeds, the state of this 124 * instance should be the same as it was before any data was input 125 * (possibly after a call to {@link 126 * #init(java.security.Key,java.security.spec.AlgorithmParameterSpec)}, 127 * possibly not). 128 */ 129 protected abstract void engineReset(); 130 131 /** 132 * Update this MAC with a single byte. 133 * 134 * @param input The next byte. 135 */ 136 protected abstract void engineUpdate(byte input); 137 138 /** 139 * Update this MAC with a portion of a byte array. 140 * 141 * @param input The next bytes. 142 * @param offset The index in <code>input</code> at which to start. 143 * @param length The number of bytes to update. 144 */ 145 protected abstract void engineUpdate(byte[] input, int offset, int length); 146 147 /** 148 * Update this MAC with the remaining bytes of a buffer. 149 * 150 * @param buffer The input buffer. 151 * @since 1.5 152 */ 153 protected void engineUpdate (final ByteBuffer buffer) 154 { 155 byte[] buf = new byte[1024]; 156 while (buffer.hasRemaining ()) 157 { 158 int n = Math.min (buffer.remaining (), buf.length); 159 buffer.get (buf, 0, n); 160 engineUpdate (buf, 0, n); 161 } 162 } 163 }