001 /*
002 * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003 * Copyright (C) 2011 NightLabs Consulting GmbH
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
017 */
018 package org.cumulus4j.crypto.internal.mac;
019
020 import org.bouncycastle.crypto.CipherParameters;
021 import org.bouncycastle.crypto.DataLengthException;
022 import org.bouncycastle.crypto.Mac;
023 import org.cumulus4j.crypto.MACCalculator;
024
025 /**
026 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
027 */
028 public class MACCalculatorImpl
029 implements MACCalculator
030 {
031 private Mac macEngine;
032
033 private int keySize;
034 private int ivSize;
035
036 public MACCalculatorImpl(Mac macEngine, int keySize, int ivSize)
037 {
038 if (macEngine == null)
039 throw new IllegalArgumentException("macEngine == null");
040
041 this.macEngine = macEngine;
042 this.keySize = keySize;
043 this.ivSize = ivSize;
044 }
045
046 private CipherParameters parameters;
047
048 @Override
049 public void init(CipherParameters params) throws IllegalArgumentException {
050 macEngine.init(params);
051 this.parameters = params;
052 }
053
054 @Override
055 public CipherParameters getParameters() {
056 return parameters;
057 }
058
059 @Override
060 public int getKeySize() {
061 return keySize;
062 }
063
064 @Override
065 public int getIVSize() {
066 return ivSize;
067 }
068
069 private String algorithmName;
070
071 @Override
072 public void setAlgorithmName(String algorithmName) {
073 this.algorithmName = algorithmName;
074 }
075
076 @Override
077 public String getAlgorithmName() {
078 if (algorithmName != null)
079 return algorithmName;
080
081 return macEngine.getAlgorithmName();
082 }
083
084 @Override
085 public int getMacSize() {
086 return macEngine.getMacSize();
087 }
088
089 @Override
090 public void update(byte in)
091 throws IllegalStateException
092 {
093 macEngine.update(in);
094 }
095
096 @Override
097 public void update(byte[] in, int inOff, int len)
098 throws DataLengthException, IllegalStateException
099 {
100 macEngine.update(in, inOff, len);
101 }
102
103 @Override
104 public int doFinal(byte[] out, int outOff)
105 throws DataLengthException, IllegalStateException
106 {
107 return macEngine.doFinal(out, outOff);
108 }
109
110 @Override
111 public byte[] doFinal(byte[] in) throws IllegalStateException
112 {
113 byte[] mac = new byte[getMacSize()];
114 update(in, 0, in.length);
115 doFinal(mac, 0);
116 return mac;
117 }
118
119 @Override
120 public void reset() {
121 macEngine.reset();
122 }
123 }