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.security.InvalidAlgorithmParameterException;
024: import java.security.InvalidKeyException;
025: import java.security.Key;
026: import java.security.NoSuchAlgorithmException;
027: import java.security.NoSuchProviderException;
028: import java.security.Provider;
029: import java.security.SecureRandom;
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 KeyAgreement {
042:
043: // Used to access common engine functionality
044: private static final Engine engine = new Engine("KeyAgreement"); //$NON-NLS-1$
045:
046: // Store SecureRandom
047: private static final SecureRandom rndm = new SecureRandom();
048:
049: // Store used provider
050: private final Provider provider;
051:
052: // Store used spi implementation
053: private final KeyAgreementSpi spiImpl;
054:
055: // Store used algorithm name
056: private final String algorithm;
057:
058: /**
059: * @com.intel.drl.spec_ref
060: *
061: */
062: protected KeyAgreement(KeyAgreementSpi keyAgreeSpi,
063: Provider provider, String algorithm) {
064: this .provider = provider;
065: this .algorithm = algorithm;
066: this .spiImpl = keyAgreeSpi;
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: */
089: public static final KeyAgreement getInstance(String algorithm)
090: throws NoSuchAlgorithmException {
091: if (algorithm == null) {
092: throw new NullPointerException(Messages
093: .getString("crypto.02")); //$NON-NLS-1$
094: }
095: synchronized (engine) {
096: engine.getInstance(algorithm, null);
097: return new KeyAgreement((KeyAgreementSpi) engine.spi,
098: engine.provider, algorithm);
099: }
100: }
101:
102: /**
103: * @com.intel.drl.spec_ref
104: *
105: */
106: public static final KeyAgreement getInstance(String algorithm,
107: String provider) throws NoSuchAlgorithmException,
108: NoSuchProviderException {
109: if ((provider == null) || (provider.length() == 0)) {
110: throw new IllegalArgumentException(Messages
111: .getString("crypto.03")); //$NON-NLS-1$
112: }
113: Provider impProvider = Security.getProvider(provider);
114: if (impProvider == null) {
115: throw new NoSuchProviderException(provider);
116: }
117: return getInstance(algorithm, impProvider);
118: }
119:
120: /**
121: * @com.intel.drl.spec_ref
122: *
123: */
124: public static final KeyAgreement getInstance(String algorithm,
125: Provider provider) throws NoSuchAlgorithmException {
126: if (provider == null) {
127: throw new IllegalArgumentException(Messages
128: .getString("crypto.04")); //$NON-NLS-1$
129: }
130: if (algorithm == null) {
131: throw new NullPointerException(Messages
132: .getString("crypto.02")); //$NON-NLS-1$
133: }
134: synchronized (engine) {
135: engine.getInstance(algorithm, provider, null);
136: return new KeyAgreement((KeyAgreementSpi) engine.spi,
137: provider, algorithm);
138: }
139: }
140:
141: /**
142: * @com.intel.drl.spec_ref
143: *
144: */
145: public final void init(Key key) throws InvalidKeyException {
146: spiImpl.engineInit(key, rndm);//new SecureRandom());
147: }
148:
149: /**
150: * @com.intel.drl.spec_ref
151: *
152: */
153: public final void init(Key key, SecureRandom random)
154: throws InvalidKeyException {
155: spiImpl.engineInit(key, random);
156: }
157:
158: /**
159: * @com.intel.drl.spec_ref
160: *
161: */
162: public final void init(Key key, AlgorithmParameterSpec params)
163: throws InvalidKeyException,
164: InvalidAlgorithmParameterException {
165: spiImpl.engineInit(key, params, rndm);//new SecureRandom());
166: }
167:
168: /**
169: * @com.intel.drl.spec_ref
170: *
171: */
172: public final void init(Key key, AlgorithmParameterSpec params,
173: SecureRandom random) throws InvalidKeyException,
174: InvalidAlgorithmParameterException {
175: spiImpl.engineInit(key, params, random);
176: }
177:
178: /**
179: * @com.intel.drl.spec_ref
180: *
181: */
182: public final Key doPhase(Key key, boolean lastPhase)
183: throws InvalidKeyException, IllegalStateException {
184: return spiImpl.engineDoPhase(key, lastPhase);
185: }
186:
187: /**
188: * @com.intel.drl.spec_ref
189: *
190: */
191: public final byte[] generateSecret() throws IllegalStateException {
192: return spiImpl.engineGenerateSecret();
193: }
194:
195: /**
196: * @com.intel.drl.spec_ref
197: *
198: */
199: public final int generateSecret(byte[] sharedSecret, int offset)
200: throws IllegalStateException, ShortBufferException {
201: return spiImpl.engineGenerateSecret(sharedSecret, offset);
202: }
203:
204: /**
205: * @com.intel.drl.spec_ref
206: *
207: */
208: public final SecretKey generateSecret(String algorithm)
209: throws IllegalStateException, NoSuchAlgorithmException,
210: InvalidKeyException {
211: return spiImpl.engineGenerateSecret(algorithm);
212: }
213:
214: }
|