001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.nio.ByteBuffer;
024: import java.security.InvalidAlgorithmParameterException;
025: import java.security.InvalidKeyException;
026: import java.security.Key;
027: import java.security.NoSuchAlgorithmException;
028: import java.security.NoSuchProviderException;
029: import java.security.Provider;
030: import java.security.Security;
031: import java.security.spec.AlgorithmParameterSpec;
032:
033: import org.apache.harmony.crypto.internal.nls.Messages;
034: import org.apache.harmony.security.fortress.Engine;
035:
036: /**
037: * @com.intel.drl.spec_ref
038: *
039: */
040:
041: public class Mac implements Cloneable {
042:
043: //Used to access common engine functionality
044: private static final Engine engine = new Engine("Mac"); //$NON-NLS-1$
045:
046: // Store used provider
047: private final Provider provider;
048:
049: // Store used spi implementation
050: private final MacSpi spiImpl;
051:
052: // Store used algorithm name
053: private final String algorithm;
054:
055: // Store Mac state (initialized or not initialized)
056: private boolean isInitMac;
057:
058: /**
059: * @com.intel.drl.spec_ref
060: *
061: */
062: protected Mac(MacSpi macSpi, Provider provider, String algorithm) {
063: this .provider = provider;
064: this .algorithm = algorithm;
065: this .spiImpl = macSpi;
066: this .isInitMac = false;
067: }
068:
069: /**
070: * @com.intel.drl.spec_ref
071: *
072: */
073: public final String getAlgorithm() {
074: return algorithm;
075: }
076:
077: /**
078: * @com.intel.drl.spec_ref
079: *
080: */
081: public final Provider getProvider() {
082: return provider;
083: }
084:
085: /**
086: * @com.intel.drl.spec_ref
087: *
088: * throws NullPointerException if algorithm is null (instead of
089: * NoSuchAlgorithmException as in 1.4 release)
090: */
091: public static final Mac getInstance(String algorithm)
092: throws NoSuchAlgorithmException {
093: if (algorithm == null) {
094: throw new NullPointerException(Messages
095: .getString("crypto.02")); //$NON-NLS-1$
096: }
097: synchronized (engine) {
098: engine.getInstance(algorithm, null);
099: return new Mac((MacSpi) engine.spi, engine.provider,
100: algorithm);
101: }
102: }
103:
104: /**
105: * @com.intel.drl.spec_ref
106: *
107: * throws NullPointerException if algorithm is null (instead of
108: * NoSuchAlgorithmException as in 1.4 release)
109: */
110: public static final Mac getInstance(String algorithm,
111: String provider) throws NoSuchAlgorithmException,
112: NoSuchProviderException {
113: if ((provider == null) || (provider.length() == 0)) {
114: throw new IllegalArgumentException(Messages
115: .getString("crypto.03")); //$NON-NLS-1$
116: }
117: Provider impProvider = Security.getProvider(provider);
118: if (impProvider == null) {
119: throw new NoSuchProviderException(provider);
120: }
121: return getInstance(algorithm, impProvider);
122: }
123:
124: /**
125: * @com.intel.drl.spec_ref
126: *
127: * throws NullPointerException if algorithm is null (instead of
128: * NoSuchAlgorithmException as in 1.4 release)
129: */
130: public static final Mac getInstance(String algorithm,
131: Provider provider) throws NoSuchAlgorithmException {
132: if (provider == null) {
133: throw new IllegalArgumentException(Messages
134: .getString("crypto.04")); //$NON-NLS-1$
135: }
136: if (algorithm == null) {
137: throw new NullPointerException(Messages
138: .getString("crypto.02")); //$NON-NLS-1$
139: }
140: synchronized (engine) {
141: engine.getInstance(algorithm, provider, null);
142: return new Mac((MacSpi) engine.spi, provider, algorithm);
143: }
144: }
145:
146: /**
147: * @com.intel.drl.spec_ref
148: *
149: */
150: public final int getMacLength() {
151: return spiImpl.engineGetMacLength();
152: }
153:
154: /**
155: * @com.intel.drl.spec_ref
156: *
157: */
158: public final void init(Key key, AlgorithmParameterSpec params)
159: throws InvalidKeyException,
160: InvalidAlgorithmParameterException {
161: if (key == null) {
162: throw new InvalidKeyException(Messages
163: .getString("crypto.05")); //$NON-NLS-1$
164: }
165: spiImpl.engineInit(key, params);
166: isInitMac = true;
167: }
168:
169: /**
170: * @com.intel.drl.spec_ref
171: *
172: */
173: public final void init(Key key) throws InvalidKeyException {
174: if (key == null) {
175: throw new InvalidKeyException(Messages
176: .getString("crypto.05")); //$NON-NLS-1$
177: }
178: try {
179: spiImpl.engineInit(key, null);
180: isInitMac = true;
181: } catch (InvalidAlgorithmParameterException e) {
182: throw new RuntimeException(e);
183: }
184: }
185:
186: /**
187: * @com.intel.drl.spec_ref
188: *
189: */
190: public final void update(byte input) throws IllegalStateException {
191: if (!isInitMac) {
192: throw new IllegalStateException(Messages
193: .getString("crypto.01"));
194: }
195: spiImpl.engineUpdate(input);
196: }
197:
198: /**
199: * @com.intel.drl.spec_ref
200: *
201: */
202: public final void update(byte[] input, int offset, int len)
203: throws IllegalStateException {
204: if (!isInitMac) {
205: throw new IllegalStateException(Messages
206: .getString("crypto.01"));
207: }
208: if (input == null) {
209: return;
210: }
211: if ((offset < 0) || (len < 0)
212: || ((offset + len) > input.length)) {
213: throw new IllegalArgumentException(Messages
214: .getString("crypto.06")); //$NON-NLS-1$
215: }
216: spiImpl.engineUpdate(input, offset, len);
217: }
218:
219: /**
220: * @com.intel.drl.spec_ref
221: *
222: */
223: public final void update(byte[] input) throws IllegalStateException {
224: if (!isInitMac) {
225: throw new IllegalStateException(Messages
226: .getString("crypto.01"));
227: }
228: if (input != null) {
229: spiImpl.engineUpdate(input, 0, input.length);
230: }
231: }
232:
233: /**
234: * @com.intel.drl.spec_ref
235: *
236: */
237: public final void update(ByteBuffer input) {
238: if (!isInitMac) {
239: throw new IllegalStateException(Messages
240: .getString("crypto.01"));
241: }
242: if (input != null) {
243: spiImpl.engineUpdate(input);
244: } else {
245: throw new IllegalArgumentException(Messages
246: .getString("crypto.07")); //$NON-NLS-1$
247: }
248: }
249:
250: /**
251: * @com.intel.drl.spec_ref
252: *
253: */
254: public final byte[] doFinal() throws IllegalStateException {
255: if (!isInitMac) {
256: throw new IllegalStateException(Messages
257: .getString("crypto.01"));
258: }
259: return spiImpl.engineDoFinal();
260: }
261:
262: /**
263: * @com.intel.drl.spec_ref
264: *
265: */
266: public final void doFinal(byte[] output, int outOffset)
267: throws ShortBufferException, IllegalStateException {
268: if (!isInitMac) {
269: throw new IllegalStateException(Messages
270: .getString("crypto.01"));
271: }
272: if (output == null) {
273: throw new ShortBufferException(Messages
274: .getString("crypto.08")); //$NON-NLS-1$
275: }
276: if ((outOffset < 0) || (outOffset >= output.length)) {
277: throw new ShortBufferException(Messages.getString(
278: "crypto.09", //$NON-NLS-1$
279: Integer.toString(outOffset)));
280: }
281: int t = spiImpl.engineGetMacLength();
282: if (t > (output.length - outOffset)) {
283: throw new ShortBufferException(Messages.getString(
284: "crypto.0A", //$NON-NLS-1$
285: Integer.toString(t)));
286: }
287: byte[] result = spiImpl.engineDoFinal();
288: System.arraycopy(result, 0, output, outOffset, result.length);
289:
290: }
291:
292: /**
293: * @com.intel.drl.spec_ref
294: *
295: */
296: public final byte[] doFinal(byte[] input)
297: throws IllegalStateException {
298: if (!isInitMac) {
299: throw new IllegalStateException(Messages
300: .getString("crypto.0B")); //$NON-NLS-1$
301: }
302: if (input != null) {
303: spiImpl.engineUpdate(input, 0, input.length);
304: }
305: return spiImpl.engineDoFinal();
306: }
307:
308: /**
309: * @com.intel.drl.spec_ref
310: *
311: */
312: public final void reset() {
313: spiImpl.engineReset();
314: }
315:
316: /**
317: * @com.intel.drl.spec_ref
318: *
319: */
320: @Override
321: public final Object clone() throws CloneNotSupportedException {
322: MacSpi newSpiImpl = (MacSpi) spiImpl.clone();
323: Mac mac = new Mac(newSpiImpl, this.provider, this.algorithm);
324: mac.isInitMac = this.isInitMac;
325: return mac;
326: }
327: }
|