diff options
author | Kenny Root <kroot@google.com> | 2014-03-07 01:33:31 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-03-07 01:33:31 +0000 |
commit | c5742c8f039370e9fd270cfa79d566668e238926 (patch) | |
tree | a936d623e81d783f14ccd7fc9c064981be2816ee | |
parent | b2730f54c7f8bf4b1c7a9359e3b07ef75397fa05 (diff) | |
parent | f515af7d5874974a6b64e88bf0277411ce6c455a (diff) | |
download | libcore-c5742c8f039370e9fd270cfa79d566668e238926.zip libcore-c5742c8f039370e9fd270cfa79d566668e238926.tar.gz libcore-c5742c8f039370e9fd270cfa79d566668e238926.tar.bz2 |
am f515af7d: Merge "CryptoProvider: remove duplicate algorithms"
* commit 'f515af7d5874974a6b64e88bf0277411ce6c455a':
CryptoProvider: remove duplicate algorithms
7 files changed, 5 insertions, 1393 deletions
diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/CryptoProvider.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/CryptoProvider.java index 7c2785a..ad5ac7d 100644 --- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/CryptoProvider.java +++ b/luni/src/main/java/org/apache/harmony/security/provider/crypto/CryptoProvider.java @@ -20,12 +20,9 @@ package org.apache.harmony.security.provider.crypto; import java.security.Provider; /** - * Implementation of Provider for SecureRandom, MessageDigest and Signature - * using a Secure Hash Algorithm, SHA-1; - * see SECURE HASH STANDARD, FIPS PUB 180-1 (http://www.itl.nist.gov/fipspubs/fip180-1.htm) <BR> - * <BR> - * The implementation supports "SHA1PRNG", "SHA-1" and "SHA1withDSA" algorithms described in - * JavaTM Cryptography Architecture, API Specification & Reference + * Implementation of Provider for SecureRandom. The implementation supports the + * "SHA1PRNG" algorithm described in JavaTM Cryptography Architecture, API + * Specification & Reference */ public final class CryptoProvider extends Provider { @@ -36,46 +33,10 @@ public final class CryptoProvider extends Provider { * Creates a Provider and puts parameters */ public CryptoProvider() { - super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)"); - // names of classes implementing services - final String MD_NAME = "org.apache.harmony.security.provider.crypto.SHA1_MessageDigestImpl"; - final String SR_NAME = "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl"; - - final String SIGN_NAME = "org.apache.harmony.security.provider.crypto.SHA1withDSA_SignatureImpl"; - - final String SIGN_ALIAS = "SHA1withDSA"; - - - final String KEYF_NAME = "org.apache.harmony.security.provider.crypto.DSAKeyFactoryImpl"; - - put("MessageDigest.SHA-1", MD_NAME); - put("MessageDigest.SHA-1 ImplementedIn", "Software"); - put("Alg.Alias.MessageDigest.SHA1", "SHA-1"); - put("Alg.Alias.MessageDigest.SHA", "SHA-1"); - - put("SecureRandom.SHA1PRNG", SR_NAME); + put("SecureRandom.SHA1PRNG", + "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl"); put("SecureRandom.SHA1PRNG ImplementedIn", "Software"); - - put("Signature.SHA1withDSA", SIGN_NAME); - put("Signature.SHA1withDSA ImplementedIn", "Software"); - put("Alg.Alias.Signature.SHAwithDSA", SIGN_ALIAS); - put("Alg.Alias.Signature.DSAwithSHA1", SIGN_ALIAS); - put("Alg.Alias.Signature.SHA1/DSA", SIGN_ALIAS); - put("Alg.Alias.Signature.SHA/DSA", SIGN_ALIAS); - put("Alg.Alias.Signature.SHA-1/DSA", SIGN_ALIAS); - put("Alg.Alias.Signature.DSA", SIGN_ALIAS); - put("Alg.Alias.Signature.DSS", SIGN_ALIAS); - - put("Alg.Alias.Signature.OID.1.2.840.10040.4.3", SIGN_ALIAS); - put("Alg.Alias.Signature.1.2.840.10040.4.3", SIGN_ALIAS); - put("Alg.Alias.Signature.1.3.14.3.2.13", SIGN_ALIAS); - put("Alg.Alias.Signature.1.3.14.3.2.27", SIGN_ALIAS); - - put("KeyFactory.DSA", KEYF_NAME); - put("KeyFactory.DSA ImplementedIn", "Software"); - put("Alg.Alias.KeyFactory.1.3.14.3.2.12", "DSA"); - put("Alg.Alias.KeyFactory.1.2.840.10040.4.1", "DSA"); } } diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAKeyFactoryImpl.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAKeyFactoryImpl.java deleted file mode 100644 index 690d16e..0000000 --- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAKeyFactoryImpl.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.harmony.security.provider.crypto; - -import java.math.BigInteger; -import java.security.InvalidKeyException; -import java.security.Key; -import java.security.KeyFactorySpi; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.interfaces.DSAParams; -import java.security.interfaces.DSAPrivateKey; -import java.security.interfaces.DSAPublicKey; -import java.security.spec.DSAPrivateKeySpec; -import java.security.spec.DSAPublicKeySpec; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.KeySpec; -import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.X509EncodedKeySpec; - -public class DSAKeyFactoryImpl extends KeyFactorySpi { - - /** - * This method generates a DSAPrivateKey object from the provided key specification. - * - * @param - * keySpec - the specification (key material) for the DSAPrivateKey. - * - * @return - * a DSAPrivateKey object - * - * @throws InvalidKeySpecException - * if "keySpec" is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec - */ - protected PrivateKey engineGeneratePrivate(KeySpec keySpec) - throws InvalidKeySpecException { - - if (keySpec != null) { - if (keySpec instanceof DSAPrivateKeySpec) { - - return new DSAPrivateKeyImpl((DSAPrivateKeySpec) keySpec); - } - if (keySpec instanceof PKCS8EncodedKeySpec) { - - return new DSAPrivateKeyImpl((PKCS8EncodedKeySpec) keySpec); - } - } - throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec"); - } - - /** - * This method generates a DSAPublicKey object from the provided key specification. - * - * @param - * keySpec - the specification (key material) for the DSAPublicKey. - * - * @return - * a DSAPublicKey object - * - * @throws InvalidKeySpecException - * if "keySpec" is neither DSAPublicKeySpec nor X509EncodedKeySpec - */ - protected PublicKey engineGeneratePublic(KeySpec keySpec) - throws InvalidKeySpecException { - - if (keySpec != null) { - if (keySpec instanceof DSAPublicKeySpec) { - - return new DSAPublicKeyImpl((DSAPublicKeySpec) keySpec); - } - if (keySpec instanceof X509EncodedKeySpec) { - - return new DSAPublicKeyImpl((X509EncodedKeySpec) keySpec); - } - } - throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec"); - } - - /** - * This method returns a specification for the supplied key. - * - * The specification will be returned in the form of an object of the type - * specified by keySpec. - * - * @param key - - * either DSAPrivateKey or DSAPublicKey - * @param keySpec - - * either DSAPrivateKeySpec.class or DSAPublicKeySpec.class - * - * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec - * - * @throws InvalidKeySpecException - * if "keySpec" is not a specification for DSAPublicKey or - * DSAPrivateKey - */ - protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) - throws InvalidKeySpecException { - - BigInteger p, q, g, x, y; - - if (key != null) { - if (keySpec == null) { - throw new NullPointerException("keySpec == null"); - } - if (key instanceof DSAPrivateKey) { - DSAPrivateKey privateKey = (DSAPrivateKey) key; - - if (keySpec.equals(DSAPrivateKeySpec.class)) { - - x = privateKey.getX(); - - DSAParams params = privateKey.getParams(); - - p = params.getP(); - q = params.getQ(); - g = params.getG(); - - return (T) (new DSAPrivateKeySpec(x, p, q, g)); - } - - if (keySpec.equals(PKCS8EncodedKeySpec.class)) { - return (T) (new PKCS8EncodedKeySpec(key.getEncoded())); - } - - throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec"); - } - - if (key instanceof DSAPublicKey) { - DSAPublicKey publicKey = (DSAPublicKey) key; - - if (keySpec.equals(DSAPublicKeySpec.class)) { - - y = publicKey.getY(); - - DSAParams params = publicKey.getParams(); - - p = params.getP(); - q = params.getQ(); - g = params.getG(); - - return (T) (new DSAPublicKeySpec(y, p, q, g)); - } - - if (keySpec.equals(X509EncodedKeySpec.class)) { - return (T) (new X509EncodedKeySpec(key.getEncoded())); - } - - throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec"); - } - } - throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey"); - } - - /** - * The method generates a DSAPublicKey object from the provided key. - * - * @param - * key - a DSAPublicKey object or DSAPrivateKey object. - * - * @return - * object of the same type as the "key" argument - * - * @throws InvalidKeyException - * if "key" is neither DSAPublicKey nor DSAPrivateKey - */ - protected Key engineTranslateKey(Key key) throws InvalidKeyException { - - if (key != null) { - if (key instanceof DSAPrivateKey) { - - DSAPrivateKey privateKey = (DSAPrivateKey) key; - DSAParams params = privateKey.getParams(); - - try { - return engineGeneratePrivate(new DSAPrivateKeySpec( - privateKey.getX(), params.getP(), params.getQ(), - params.getG())); - } catch (InvalidKeySpecException e) { - // Actually this exception shouldn't be thrown - throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e); - } - } - - if (key instanceof DSAPublicKey) { - - DSAPublicKey publicKey = (DSAPublicKey) key; - DSAParams params = publicKey.getParams(); - - try { - return engineGeneratePublic(new DSAPublicKeySpec(publicKey - .getY(), params.getP(), params.getQ(), params - .getG())); - } catch (InvalidKeySpecException e) { - // Actually this exception shouldn't be thrown - throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e); - } - } - } - throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey"); - } - -} diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAPrivateKeyImpl.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAPrivateKeyImpl.java deleted file mode 100644 index c0fc766..0000000 --- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAPrivateKeyImpl.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - /* - * TODO - * 1. The class extends the PrivateKeyImpl class in "org.apache.harmony.security" package. - * - * 2. See a compatibility with RI comments - * in the below "DSAPrivateKeyImpl(PKCS8EncodedKeySpec keySpec)" constructor. - */ - - -package org.apache.harmony.security.provider.crypto; - -import java.io.IOException; -import java.io.NotActiveException; -import java.math.BigInteger; -import java.security.interfaces.DSAParams; -import java.security.interfaces.DSAPrivateKey; -import java.security.spec.DSAParameterSpec; -import java.security.spec.DSAPrivateKeySpec; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import org.apache.harmony.security.PrivateKeyImpl; -import org.apache.harmony.security.asn1.ASN1Integer; -import org.apache.harmony.security.pkcs8.PrivateKeyInfo; -import org.apache.harmony.security.utils.AlgNameMapper; -import org.apache.harmony.security.x509.AlgorithmIdentifier; - -/** - * The class provides DSAPrivateKey functionality by extending a class implementing PrivateKey - * and implementing methods defined in both interfaces, DSAKey and DSAPrivateKey - */ -public class DSAPrivateKeyImpl extends PrivateKeyImpl implements DSAPrivateKey { - - /** - * @serial - */ - private static final long serialVersionUID = -4716227614104950081L; - - private BigInteger x, g, p, q; - - private transient DSAParams params; - - /** - * Creates object from DSAPrivateKeySpec. - * - * @param keySpec - a DSAPrivateKeySpec object - */ - public DSAPrivateKeyImpl(DSAPrivateKeySpec keySpec) { - - super("DSA"); - - PrivateKeyInfo pki; - - g = keySpec.getG(); - p = keySpec.getP(); - q = keySpec.getQ(); - - ThreeIntegerSequence threeInts = new ThreeIntegerSequence(p - .toByteArray(), q.toByteArray(), g.toByteArray()); - - AlgorithmIdentifier ai = new AlgorithmIdentifier(AlgNameMapper - .map2OID("DSA"), - threeInts.getEncoded()); - x = keySpec.getX(); - - pki = new PrivateKeyInfo(0, ai, ASN1Integer.getInstance().encode( - x.toByteArray()), null); - - setEncoding(pki.getEncoded()); - - params = new DSAParameterSpec(p, q, g); - } - - /** - * Creates object from PKCS8EncodedKeySpec. - * - * @param keySpec - a XPKCS8EncodedKeySpec object - * - * @throws InvalidKeySpecException - if key data cannot be obtain from encoded format - */ - public DSAPrivateKeyImpl(PKCS8EncodedKeySpec keySpec) - throws InvalidKeySpecException { - - super("DSA"); - - AlgorithmIdentifier ai; - ThreeIntegerSequence threeInts = null; - - String alg, algName; - - byte[] encoding = keySpec.getEncoded(); - - PrivateKeyInfo privateKeyInfo = null; - - try { - privateKeyInfo = (PrivateKeyInfo) PrivateKeyInfo.ASN1 - .decode(encoding); - } catch (IOException e) { - throw new InvalidKeySpecException("Failed to decode keySpec encoding: " + e); - } - - try { - x = new BigInteger((byte[]) ASN1Integer.getInstance().decode( - privateKeyInfo.getPrivateKey())); - } catch (IOException e) { - throw new InvalidKeySpecException("Failed to decode parameters: " + e); - } - - ai = privateKeyInfo.getAlgorithmIdentifier(); - try { - threeInts = (ThreeIntegerSequence) ThreeIntegerSequence.ASN1 - .decode(ai.getParameters()); - } catch (IOException e) { - throw new InvalidKeySpecException("Failed to decode parameters: " + e); - } - p = new BigInteger(threeInts.p); - q = new BigInteger(threeInts.q); - g = new BigInteger(threeInts.g); - params = new DSAParameterSpec(p, q, g); - setEncoding(encoding); - - /* - * the following code implements RI behavior - */ - alg = ai.getAlgorithm(); - algName = AlgNameMapper.map2AlgName(alg); - setAlgorithm(algName == null ? alg : algName); - } - - public BigInteger getX() { - return x; - } - - public DSAParams getParams() { - return params; - } - - private void readObject(java.io.ObjectInputStream in) throws NotActiveException, IOException, ClassNotFoundException { - in.defaultReadObject(); - params = new DSAParameterSpec(p, q, g); - } - -} diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAPublicKeyImpl.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAPublicKeyImpl.java deleted file mode 100644 index 6b35970..0000000 --- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/DSAPublicKeyImpl.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - /* - * TODO - * 1. The class extends the PublicKeyImpl class in "org.apache.harmony.security" package. - * - * 2. The class uses methods in the auxiliary non-public "ThreeIntegerSequence" class - * defined along with the "DSAPrivateKeyImpl" class. - * - * 3. See a compatibility with RI comments - * in the below "DSAPublicKeyImpl(X509EncodedKeySpec keySpec)" constructor. - */ - -package org.apache.harmony.security.provider.crypto; - -import java.io.IOException; -import java.io.NotActiveException; -import java.math.BigInteger; -import java.security.interfaces.DSAParams; -import java.security.interfaces.DSAPublicKey; -import java.security.spec.DSAParameterSpec; -import java.security.spec.DSAPublicKeySpec; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.X509EncodedKeySpec; -import org.apache.harmony.security.PublicKeyImpl; -import org.apache.harmony.security.asn1.ASN1Integer; -import org.apache.harmony.security.utils.AlgNameMapper; -import org.apache.harmony.security.x509.AlgorithmIdentifier; -import org.apache.harmony.security.x509.SubjectPublicKeyInfo; - -/** - * The class provides DSAPublicKey functionality by extending a class implementing PublicKey - * and implementing methods defined in both interfaces, DSAKey and DSAPublicKey - */ -public class DSAPublicKeyImpl extends PublicKeyImpl implements DSAPublicKey { - - /** - * @serial - */ - private static final long serialVersionUID = -2279672131310978336L; - - private BigInteger y, g, p, q; - - private transient DSAParams params; - - /** - * Creates object from DSAPublicKeySpec. - * - * @param keySpec - a DSAPublicKeySpec object - */ - public DSAPublicKeyImpl(DSAPublicKeySpec keySpec) { - - super("DSA"); - - SubjectPublicKeyInfo spki; - - p = keySpec.getP(); - q = keySpec.getQ(); - g = keySpec.getG(); - - ThreeIntegerSequence threeInts = new ThreeIntegerSequence(p - .toByteArray(), q.toByteArray(), g.toByteArray()); - - AlgorithmIdentifier ai = new AlgorithmIdentifier(AlgNameMapper - .map2OID("DSA"), - threeInts.getEncoded()); - - y = keySpec.getY(); - - spki = new SubjectPublicKeyInfo(ai, ASN1Integer.getInstance().encode( - y.toByteArray())); - setEncoding(spki.getEncoded()); - - params = (DSAParams) (new DSAParameterSpec(p, q, g)); - } - - /** - * Creates object from X509EncodedKeySpec. - * - * @param keySpec - a X509EncodedKeySpec object - * - * @throws InvalidKeySpecException - if key data cannot be obtain from encoded format - */ - public DSAPublicKeyImpl(X509EncodedKeySpec keySpec) - throws InvalidKeySpecException { - - super("DSA"); - - AlgorithmIdentifier ai; - ThreeIntegerSequence threeInts = null; - - SubjectPublicKeyInfo subjectPublicKeyInfo = null; - - byte[] encoding = keySpec.getEncoded(); - - String alg, algName; - - try { - subjectPublicKeyInfo = (SubjectPublicKeyInfo) SubjectPublicKeyInfo.ASN1 - .decode(encoding); - } catch (IOException e) { - throw new InvalidKeySpecException("Failed to decode keySpec encoding: " + e); - } - - try { - y = new BigInteger((byte[]) ASN1Integer.getInstance().decode( - subjectPublicKeyInfo.getSubjectPublicKey())); - } catch (IOException e) { - throw new InvalidKeySpecException("Failed to decode parameters: " + e); - } - - ai = subjectPublicKeyInfo.getAlgorithmIdentifier(); - - try { - threeInts = (ThreeIntegerSequence) ThreeIntegerSequence.ASN1 - .decode(ai.getParameters()); - } catch (IOException e) { - throw new InvalidKeySpecException("Failed to decode parameters: " + e); - } - p = new BigInteger(threeInts.p); - q = new BigInteger(threeInts.q); - g = new BigInteger(threeInts.g); - params = (DSAParams) (new DSAParameterSpec(p, q, g)); - - setEncoding(encoding); - - /* - * the following code implements RI behavior - */ - alg = ai.getAlgorithm(); - algName = AlgNameMapper.map2AlgName(alg); - setAlgorithm(algName == null ? alg : algName); - } - - /** - * @return - * a value of a public key (y). - */ - public BigInteger getY() { - return y; - } - - /** - * @return - * DSA key parameters (p, q, g). - */ - public DSAParams getParams() { - return params; - } - - private void readObject(java.io.ObjectInputStream in) throws NotActiveException, IOException, ClassNotFoundException { - in.defaultReadObject(); - params = new DSAParameterSpec(p, q, g); - } - -} diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java deleted file mode 100644 index 3f41f18..0000000 --- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package org.apache.harmony.security.provider.crypto; - -import java.security.DigestException; -import java.security.MessageDigestSpi; -import java.util.Arrays; - -import static org.apache.harmony.security.provider.crypto.SHA1Constants.*; - -/** - * This class extends the MessageDigestSpi class implementing all its abstract methods; - * it overrides the "Object clone()" and "int engineGetDigestLength()" methods. <BR> - * The class implements the Cloneable interface. - */ -public class SHA1_MessageDigestImpl extends MessageDigestSpi implements Cloneable { - private int[] buffer; // buffer has the following structure: - // - 0-16 - frame for accumulating a message - // - 17-79 - for SHA1Impl methods - // - 80 - unused - // - 81 - to store length of the message - // - 82-86 - frame for current message digest - - private byte[] oneByte; // one byte buffer needed to use in engineUpdate(byte) - // having buffer as private field is just optimization - - private long messageLength; // total length of bytes supplied by user - - - /** - * The constructor creates needed buffers and sets the engine at initial state - */ - public SHA1_MessageDigestImpl() { - - // BYTES_OFFSET +6 is minimal length required by methods in SHA1Impl - buffer = new int[BYTES_OFFSET +6]; - - oneByte = new byte[1]; - - engineReset(); - } - - - /** - * The method performs final actions and invokes the "computeHash(int[])" method. - * In case if there is no enough words in current frame - * after processing its data, extra frame is prepared and - * the "computeHash(int[])" method is invoked second time. <BR> - * - * After processing, the method resets engine's state - * - * @param - * digest - byte array - * @param - * offset - offset in digest - */ - private void processDigest(byte[] digest, int offset) { - - int i, j; // implementation variables - int lastWord; // - - long nBits = messageLength <<3 ; // length has to be calculated before padding - - engineUpdate( (byte) 0x80 ); // beginning byte in padding - - i = 0; // i contains number of beginning word for following loop - - lastWord = (buffer[BYTES_OFFSET] + 3)>>2 ; // computing of # of full words by shifting - // # of bytes - - // possible cases: - // - // - buffer[BYTES_OFFSET] == 0 - buffer frame is empty, - // padding byte was 64th in previous frame - // current frame should contain only message's length - // - // - lastWord < 14 - two last, these are 14 & 15, words in 16 word frame are free; - // no extra frame needed - // - lastWord = 14 - only one last, namely 15-th, word in frame doesn't contain bytes; - // extra frame is needed - // - lastWord > 14 - last word in frame is not full; - // extra frame is needed - - if ( buffer[BYTES_OFFSET] != 0 ) { - - if ( lastWord < 15 ) { - i = lastWord; - } else { - if ( lastWord == 15 ) { - buffer[15] = 0; // last word in frame is set to "0" - } - SHA1Impl.computeHash(buffer); - i = 0; - } - } - Arrays.fill(buffer, i, 14, 0); - - buffer[14] = (int)( nBits >>>32 ); - buffer[15] = (int)( nBits & 0xFFFFFFFF ); - SHA1Impl.computeHash(buffer); - - // converting 5-word frame into 20 bytes - j = offset; - for ( i = HASH_OFFSET; i < HASH_OFFSET +5; i++ ) { - int k = buffer[i]; - digest[j ] = (byte) ( k >>>24 ); // getting first byte from left - digest[j+1] = (byte) ( k >>>16 ); // getting second byte from left - digest[j+2] = (byte) ( k >>> 8 ); // getting third byte from left - digest[j+3] = (byte) ( k ); // getting fourth byte from left - j += 4; - } - - engineReset(); - } - - // methods specified in java.security.MessageDigestSpi - - /** - * Returns a "deep" copy of this SHA1MDImpl object. <BR> - * - * The method overrides "clone()" in class Object. <BR> - * - * @return - * a clone of this object - */ - public Object clone() throws CloneNotSupportedException { - SHA1_MessageDigestImpl cloneObj = (SHA1_MessageDigestImpl) super.clone(); - cloneObj.buffer = buffer.clone(); - cloneObj.oneByte = oneByte.clone(); - return cloneObj; - } - - - /** - * Computes a message digest value. <BR> - * - * The method resets the engine. <BR> - * - * The method overrides "engineDigest()" in class MessageDigestSpi. <BR> - * - * @return - * byte array containing message digest value - */ - protected byte[] engineDigest() { - byte[] hash = new byte[DIGEST_LENGTH]; - processDigest(hash, 0); - return hash; - } - - - /** - * Computes message digest value. - * Upon return, the value is stored in "buf" buffer beginning "offset" byte. <BR> - * - * The method resets the engine. <BR> - * - * The method overrides "engineDigest(byte[],int,int) in class MessageDigestSpi. - * - * @param - * buf byte array to store a message digest returned - * @param - * offset a position in the array for first byte of the message digest - * @param - * len number of bytes within buffer allotted for the message digest; - * as this implementation doesn't provide partial digests, - * len should be >= 20, DigestException is thrown otherwise - * @return - * the length of the message digest stored in the "buf" buffer; - * in this implementation the length=20 - * - * @throws IllegalArgumentException - * if null is passed to the "buf" argument <BR> - * if offset + len > buf.length <BR> - * if offset > buf.length or len > buf.length - * - * @throws DigestException - * if len < 20 - * - * @throws ArrayIndexOutOfBoundsException - * if offset < 0 - */ - protected int engineDigest(byte[] buf, int offset, int len) throws DigestException { - if (buf == null) { - throw new IllegalArgumentException("buf == null"); - } - if (offset > buf.length || len > buf.length || (len + offset) > buf.length) { - throw new IllegalArgumentException(); - } - if (len < DIGEST_LENGTH) { - throw new DigestException("len < DIGEST_LENGTH"); - } - if (offset < 0) { - throw new ArrayIndexOutOfBoundsException(offset); - } - - processDigest(buf, offset); - - return DIGEST_LENGTH; - } - - - /** - * Returns a message digest length. <BR> - * - * The method overrides "engineGetDigestLength()" in class MessageDigestSpi. <BR> - * - * @return - * total length of current message digest as an int value - */ - protected int engineGetDigestLength() { - return DIGEST_LENGTH; - } - - - /** - * Resets the engine. <BR> - * - * The method overrides "engineReset()" in class MessageDigestSpi. <BR> - */ - protected void engineReset() { - - messageLength = 0; - - buffer[BYTES_OFFSET] = 0; - buffer[HASH_OFFSET ] = H0; - buffer[HASH_OFFSET +1] = H1; - buffer[HASH_OFFSET +2] = H2; - buffer[HASH_OFFSET +3] = H3; - buffer[HASH_OFFSET +4] = H4; - } - - - /** - * Supplements a byte to current message. <BR> - * - * The method overrides "engineUpdate(byte)" in class MessageDigestSpi. <BR> - * - * @param - * input byte to add to current message - */ - protected void engineUpdate(byte input) { - - oneByte[0] = input; - SHA1Impl.updateHash( buffer, oneByte, 0, 0 ); - messageLength++; - } - - - /** - * Updates current message. <BR> - * - * The method overrides "engineUpdate(byte[],int,int)" in class MessageDigestSpi. <BR> - * - * The method silently returns if "len" <= 0. - * - * @param - * input a byte array - * @param - * offset a number of first byte in the "input" array to use for updating - * @param - * len a number of bytes to use - * - * @throws NullPointerException - * if null is passed to the "buf" argument - * - * @throws IllegalArgumentException - * if offset > buf.length or len > buf.length or - * (len + offset) > buf.length - * @throws ArrayIndexOutOfBoundsException - * offset < 0 - */ - protected void engineUpdate(byte[] input, int offset, int len) { - if (input == null) { - throw new IllegalArgumentException("input == null"); - } - if (len <= 0) { - return; - } - if (offset < 0) { - throw new ArrayIndexOutOfBoundsException(offset); - } - if (offset > input.length || len > input.length || (len + offset) > input.length) { - throw new IllegalArgumentException(); - } - - SHA1Impl.updateHash(buffer, input, offset, offset + len -1 ); - messageLength += len; - } - -} diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1withDSA_SignatureImpl.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1withDSA_SignatureImpl.java deleted file mode 100644 index 2958e00..0000000 --- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1withDSA_SignatureImpl.java +++ /dev/null @@ -1,423 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.harmony.security.provider.crypto; - -import java.math.BigInteger; -import java.security.InvalidKeyException; -import java.security.InvalidParameterException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.SecureRandom; -import java.security.Signature; -import java.security.SignatureException; -import java.security.interfaces.DSAKey; -import java.security.interfaces.DSAParams; -import java.security.interfaces.DSAPrivateKey; -import java.security.interfaces.DSAPublicKey; - -public class SHA1withDSA_SignatureImpl extends Signature { - - private MessageDigest msgDigest; - - private DSAKey dsaKey; - - /** - * The solo constructor. - */ - public SHA1withDSA_SignatureImpl() throws NoSuchAlgorithmException { - - super("SHA1withDSA"); - - msgDigest = MessageDigest.getInstance("SHA1"); - } - - /** - * Deprecated method. - * - * @return - * null - */ - protected Object engineGetParameter(String param) - throws InvalidParameterException { - if (param == null) { - throw new NullPointerException("param == null"); - } - return null; - } - - /** - * Initializes this signature object with PrivateKey object - * passed as argument to the method. - * - * @params - * privateKey DSAPrivateKey object - * @throws - * InvalidKeyException if privateKey is not DSAPrivateKey object - */ - protected void engineInitSign(PrivateKey privateKey) - throws InvalidKeyException { - - DSAParams params; - - // parameters and private key - BigInteger p, q, x; - - int n; - - if (privateKey == null || !(privateKey instanceof DSAPrivateKey)) { - throw new InvalidKeyException(); - } - - params = ((DSAPrivateKey) privateKey).getParams(); - p = params.getP(); - q = params.getQ(); - x = ((DSAPrivateKey) privateKey).getX(); - - // checks described in DSA standard - n = p.bitLength(); - if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) { - throw new InvalidKeyException("bad p"); - } - if (q.signum() != 1 && q.bitLength() != 160) { - throw new InvalidKeyException("bad q"); - } - if (x.signum() != 1 || x.compareTo(q) != -1) { - throw new InvalidKeyException("x <= 0 || x >= q"); - } - - dsaKey = (DSAKey) privateKey; - - msgDigest.reset(); - } - - /** - * Initializes this signature object with PublicKey object - * passed as argument to the method. - * - * @params - * publicKey DSAPublicKey object - * @throws - * InvalidKeyException if publicKey is not DSAPublicKey object - */ - protected void engineInitVerify(PublicKey publicKey) - throws InvalidKeyException { - - // parameters and public key - BigInteger p, q, y; - - int n1; - - if (publicKey == null || !(publicKey instanceof DSAPublicKey)) { - throw new InvalidKeyException("publicKey is not an instance of DSAPublicKey"); - } - - DSAParams params = ((DSAPublicKey) publicKey).getParams(); - p = params.getP(); - q = params.getQ(); - y = ((DSAPublicKey) publicKey).getY(); - - // checks described in DSA standard - n1 = p.bitLength(); - if (p.compareTo(BigInteger.valueOf(1)) != 1 || n1 < 512 || n1 > 1024 || (n1 & 077) != 0) { - throw new InvalidKeyException("bad p"); - } - if (q.signum() != 1 || q.bitLength() != 160) { - throw new InvalidKeyException("bad q"); - } - if (y.signum() != 1) { - throw new InvalidKeyException("y <= 0"); - } - - dsaKey = (DSAKey) publicKey; - - msgDigest.reset(); - } - - /* - * Deprecated method. - * - * @throws - * InvalidParameterException - */ - protected void engineSetParameter(String param, Object value) throws InvalidParameterException { - if (param == null) { - throw new NullPointerException("param == null"); - } - throw new InvalidParameterException("invalid parameter for this engine"); - } - - /** - * Returns signature bytes as byte array containing - * ASN1 representation for two BigInteger objects - * which is SEQUENCE of two INTEGERS. - * Length of sequence varies from less than 46 to 48. - * - * Resets object to the state it was in - * when previous call to either "initSign" method was called. - * - * @return - * byte array containing signature in ASN1 representation - * @throws - * SignatureException if object's state is not SIGN or - * signature algorithm cannot process data - */ - - protected byte[] engineSign() throws SignatureException { - - // names of below BigIntegers are the same as they are defined in DSA standard - BigInteger r = null; - BigInteger s = null; - BigInteger k = null; - - // parameters and private key - BigInteger p, q, g, x; - - // BigInteger for message digest - BigInteger digestBI; - - // various byte array being used in computing signature - byte[] randomBytes; - byte[] rBytes; - byte[] sBytes; - byte[] signature; - - int n, n1, n2; - - DSAParams params; - - if (appRandom == null) { - appRandom = new SecureRandom(); - } - - params = dsaKey.getParams(); - p = params.getP(); - q = params.getQ(); - g = params.getG(); - x = ((DSAPrivateKey) dsaKey).getX(); - - // forming signature according algorithm described in chapter 5 of DSA standard - - digestBI = new BigInteger(1, msgDigest.digest()); - - randomBytes = new byte[20]; - - for (;;) { - - appRandom.nextBytes(randomBytes); - - k = new BigInteger(1, randomBytes); - if (k.compareTo(q) != -1) { - continue; - } - r = g.modPow(k, p).mod(q); - if (r.signum() == 0) { - continue; - } - - s = k.modInverse(q).multiply(digestBI.add(x.multiply(r)).mod(q)) - .mod(q); - - if (s.signum() != 0) { - break; - } - } - - // forming signature's ASN1 representation which is SEQUENCE of two INTEGERs - // - rBytes = r.toByteArray(); - n1 = rBytes.length; - if ((rBytes[0] & 0x80) != 0) { - n1++; - } - sBytes = s.toByteArray(); - n2 = sBytes.length; - if ((sBytes[0] & 0x80) != 0) { - n2++; - } - - signature = new byte[6 + n1 + n2]; // 48 is max. possible length of signature - signature[0] = (byte) 0x30; // ASN1 SEQUENCE tag - signature[1] = (byte) (4 + n1 + n2); // total length of two INTEGERs - signature[2] = (byte) 0x02; // ASN1 INTEGER tag - signature[3] = (byte) n1; // length of r - signature[4 + n1] = (byte) 0x02; // ASN1 INTEGER tag - signature[5 + n1] = (byte) n2; // length of s - - if (n1 == rBytes.length) { - n = 4; - } else { - n = 5; - } - System.arraycopy(rBytes, 0, signature, n, rBytes.length); - - if (n2 == sBytes.length) { - n = 6 + n1; - } else { - n = 7 + n1; - } - System.arraycopy(sBytes, 0, signature, n, sBytes.length); - - return signature; - } - - /** - * Updates data to sign or to verify. - * - * @params - * b byte to update - * @throws - * SignatureException if object was not initialized for signing or verifying - */ - protected void engineUpdate(byte b) throws SignatureException { - - msgDigest.update(b); - } - - /** - * Updates data to sign or to verify. - * - * @params - * b byte array containing bytes to update - * @params - * off offset in byte array to start from - * @params - * len number of bytes to use for updating - * @throws - * SignatureException if object was not initialized for signing or verifying - */ - protected void engineUpdate(byte[] b, int off, int len) - throws SignatureException { - - msgDigest.update(b, off, len); - } - - private boolean checkSignature(byte[] sigBytes, int offset, int length) - throws SignatureException { - - // names of below BigIntegers are the same as they are defined in DSA standard - BigInteger r, s, w; - BigInteger u1, u2, v; - - // parameters and public key - BigInteger p, q, g, y; - - DSAParams params; - - int n1, n2; - - byte[] bytes; - byte[] digest; - - // checking up on signature's ASN1 - try { - byte dummy; - n1 = sigBytes[offset + 3]; - n2 = sigBytes[offset + n1 + 5]; - - if (sigBytes[offset + 0] != 0x30 || sigBytes[offset + 2] != 2 - || sigBytes[offset + n1 + 4] != 2 - || sigBytes[offset + 1] != (n1 + n2 + 4) || n1 > 21 - || n2 > 21 - || (length != 0 && (sigBytes[offset + 1] + 2) > length)) { - throw new SignatureException("signature bytes have invalid encoding"); - } - - dummy = sigBytes[5 + n1 + n2]; // to check length of sigBytes - } catch (ArrayIndexOutOfBoundsException e) { - throw new SignatureException("bad argument: byte[] is too small"); - } - - digest = msgDigest.digest(); - - bytes = new byte[n1]; - System.arraycopy(sigBytes, offset + 4, bytes, 0, n1); - r = new BigInteger(bytes); - - bytes = new byte[n2]; - System.arraycopy(sigBytes, offset + 6 + n1, bytes, 0, n2); - s = new BigInteger(bytes); - - params = dsaKey.getParams(); - p = params.getP(); - q = params.getQ(); - g = params.getG(); - y = ((DSAPublicKey) dsaKey).getY(); - - // forming signature according algorithm described in chapter 6 of DSA standard - - if (r.signum() != 1 || r.compareTo(q) != -1 || s.signum() != 1 - || s.compareTo(q) != -1) { - return false; - } - - w = s.modInverse(q); - - u1 = (new BigInteger(1, digest)).multiply(w).mod(q); - u2 = r.multiply(w).mod(q); - - v = g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q); - - if (v.compareTo(r) != 0) { - return false; - } - return true; - } - - /** - * Verifies the signature bytes. - * - * @params - * sigBytes byte array with signature bytes to verify. - * @return - * true if signature bytes were verified, false otherwise - * @throws - * SignatureException if object's state is not VERIFY or - * signature format is not ASN1 representation or - * signature algorithm cannot process data - */ - protected boolean engineVerify(byte[] sigBytes) throws SignatureException { - if (sigBytes == null) { - throw new NullPointerException("sigBytes == null"); - } - - return checkSignature(sigBytes, 0, 0); - } - - /** - * Verifies the signature bytes. - * - * @params - * sigBytes byte array with signature bytes to verify. - * @params - * offset index in sigBytes to start from - * @params - * length number of bytes allotted for signature - * @return - * true if signature bytes were verified, false otherwise - * @throws - * SignatureException if object's state is not VERIFY or - * signature format is not ASN1 representation or - * signature algorithm cannot process data - */ - protected boolean engineVerify(byte[] sigBytes, int offset, int length) - throws SignatureException { - return checkSignature(sigBytes, offset, length); - } -} diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/ThreeIntegerSequence.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/ThreeIntegerSequence.java deleted file mode 100644 index 4f4232a..0000000 --- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/ThreeIntegerSequence.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package org.apache.harmony.security.provider.crypto; - -import org.apache.harmony.security.asn1.ASN1Integer; -import org.apache.harmony.security.asn1.ASN1Sequence; -import org.apache.harmony.security.asn1.ASN1Type; -import org.apache.harmony.security.asn1.BerInputStream; - - -/** - * The auxiliary class providing means to process ASN1Sequence of three Integers. - * Such sequences are parts of ASN1 encoded formats for DSA private and public keys. - */ -class ThreeIntegerSequence { - - byte[] p, q, g; - - private byte[] encoding; - - ThreeIntegerSequence(byte[] p, byte[] q, byte[] g) { - - this.p = p; - this.q = q; - this.g = g; - encoding = null; - } - - public byte[] getEncoded() { - if (encoding == null) { - encoding = ASN1.encode(this); - } - return encoding; - } - - public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] { - ASN1Integer.getInstance(), ASN1Integer.getInstance(), - ASN1Integer.getInstance() }) { - - protected Object getDecodedObject(BerInputStream in) { - - Object[] values = (Object[]) in.content; - - return new ThreeIntegerSequence((byte[]) values[0], - (byte[]) values[1], (byte[]) values[2]); - } - - protected void getValues(Object object, Object[] values) { - - ThreeIntegerSequence mySeq = (ThreeIntegerSequence) object; - - values[0] = mySeq.p; - values[1] = mySeq.q; - values[2] = mySeq.g; - } - }; -} |