SHA1.java revision a198e1ecc615e26a167d0f2dca9fa7e5fc62de10
1package org.bouncycastle.jcajce.provider.digest;
2
3import java.security.spec.InvalidKeySpecException;
4import java.security.spec.KeySpec;
5
6import javax.crypto.SecretKey;
7import javax.crypto.spec.PBEKeySpec;
8
9import org.bouncycastle.asn1.iana.IANAObjectIdentifiers;
10import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
11import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
12import org.bouncycastle.crypto.CipherKeyGenerator;
13import org.bouncycastle.crypto.CipherParameters;
14import org.bouncycastle.crypto.digests.SHA1Digest;
15import org.bouncycastle.crypto.macs.HMac;
16import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
17import org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey;
18import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
19import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac;
20import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
21import org.bouncycastle.jcajce.provider.symmetric.util.PBE;
22import org.bouncycastle.jcajce.provider.symmetric.util.PBESecretKeyFactory;
23
24public class SHA1
25{
26    private SHA1()
27    {
28
29    }
30
31    static public class Digest
32        extends BCMessageDigest
33        implements Cloneable
34    {
35        public Digest()
36        {
37            super(new SHA1Digest());
38        }
39
40        public Object clone()
41            throws CloneNotSupportedException
42        {
43            Digest d = (Digest)super.clone();
44            d.digest = new SHA1Digest((SHA1Digest)digest);
45
46            return d;
47        }
48    }
49
50    /**
51     * SHA1 HMac
52     */
53    public static class HashMac
54        extends BaseMac
55    {
56        public HashMac()
57        {
58            super(new HMac(new SHA1Digest()));
59        }
60    }
61
62    public static class KeyGenerator
63        extends BaseKeyGenerator
64    {
65        public KeyGenerator()
66        {
67            super("HMACSHA1", 160, new CipherKeyGenerator());
68        }
69    }
70
71    /**
72     * SHA1 HMac
73     */
74    public static class SHA1Mac
75        extends BaseMac
76    {
77        public SHA1Mac()
78        {
79            super(new HMac(new SHA1Digest()));
80        }
81    }
82
83    /**
84     * PBEWithHmacSHA
85     */
86    public static class PBEWithMacKeyFactory
87        extends PBESecretKeyFactory
88    {
89        public PBEWithMacKeyFactory()
90        {
91            super("PBEwithHmacSHA", null, false, PKCS12, SHA1, 160, 0);
92        }
93    }
94
95
96    public static class BasePBKDF2WithHmacSHA1
97        extends BaseSecretKeyFactory
98    {
99        private int scheme;
100
101        public BasePBKDF2WithHmacSHA1(String name, int scheme)
102        {
103            super(name, PKCSObjectIdentifiers.id_PBKDF2);
104
105            this.scheme = scheme;
106        }
107
108        protected SecretKey engineGenerateSecret(
109            KeySpec keySpec)
110            throws InvalidKeySpecException
111        {
112            if (keySpec instanceof PBEKeySpec)
113            {
114                PBEKeySpec pbeSpec = (PBEKeySpec)keySpec;
115
116                if (pbeSpec.getSalt() == null)
117                {
118                    throw new InvalidKeySpecException("missing required salt");
119                }
120
121                if (pbeSpec.getIterationCount() <= 0)
122                {
123                    throw new InvalidKeySpecException("positive iteration count required: "
124                        + pbeSpec.getIterationCount());
125                }
126
127                if (pbeSpec.getKeyLength() <= 0)
128                {
129                    throw new InvalidKeySpecException("positive key length required: "
130                        + pbeSpec.getKeyLength());
131                }
132
133                if (pbeSpec.getPassword().length == 0)
134                {
135                    throw new IllegalArgumentException("password empty");
136                }
137
138                int digest = SHA1;
139                int keySize = pbeSpec.getKeyLength();
140                int ivSize = -1;    // JDK 1,2 and earlier does not understand simplified version.
141                CipherParameters param = PBE.Util.makePBEMacParameters(pbeSpec, scheme, digest, keySize);
142
143                return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, param);
144            }
145
146            throw new InvalidKeySpecException("Invalid KeySpec");
147        }
148    }
149
150    public static class PBKDF2WithHmacSHA1UTF8
151        extends BasePBKDF2WithHmacSHA1
152    {
153        public PBKDF2WithHmacSHA1UTF8()
154        {
155            super("PBKDF2WithHmacSHA1", PKCS5S2_UTF8);
156        }
157    }
158
159    public static class PBKDF2WithHmacSHA18BIT
160        extends BasePBKDF2WithHmacSHA1
161    {
162        public PBKDF2WithHmacSHA18BIT()
163        {
164            super("PBKDF2WithHmacSHA1And8bit", PKCS5S2);
165        }
166    }
167
168    public static class Mappings
169        extends DigestAlgorithmProvider
170    {
171        private static final String PREFIX = SHA1.class.getName();
172
173        public Mappings()
174        {
175        }
176
177        public void configure(ConfigurableProvider provider)
178        {
179            provider.addAlgorithm("MessageDigest.SHA-1", PREFIX + "$Digest");
180            provider.addAlgorithm("Alg.Alias.MessageDigest.SHA1", "SHA-1");
181            provider.addAlgorithm("Alg.Alias.MessageDigest.SHA", "SHA-1");
182            provider.addAlgorithm("Alg.Alias.MessageDigest." + OIWObjectIdentifiers.idSHA1, "SHA-1");
183
184            addHMACAlgorithm(provider, "SHA1", PREFIX + "$HashMac", PREFIX + "$KeyGenerator");
185            addHMACAlias(provider, "SHA1", PKCSObjectIdentifiers.id_hmacWithSHA1);
186            addHMACAlias(provider, "SHA1", IANAObjectIdentifiers.hmacSHA1);
187
188            provider.addAlgorithm("Mac.PBEWITHHMACSHA", PREFIX + "$SHA1Mac");
189            provider.addAlgorithm("Mac.PBEWITHHMACSHA1", PREFIX + "$SHA1Mac");
190            provider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBEWITHHMACSHA", "PBEWITHHMACSHA1");
191            provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + OIWObjectIdentifiers.idSHA1, "PBEWITHHMACSHA1");
192            provider.addAlgorithm("Alg.Alias.Mac." + OIWObjectIdentifiers.idSHA1, "PBEWITHHMACSHA");
193
194            provider.addAlgorithm("SecretKeyFactory.PBEWITHHMACSHA1", PREFIX + "$PBEWithMacKeyFactory");
195            provider.addAlgorithm("SecretKeyFactory.PBKDF2WithHmacSHA1", PREFIX + "$PBKDF2WithHmacSHA1UTF8");
196            provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_PBKDF2, "PBKDF2WithHmacSHA1");
197            provider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBKDF2WithHmacSHA1AndUTF8", "PBKDF2WithHmacSHA1");
198            provider.addAlgorithm("SecretKeyFactory.PBKDF2WithHmacSHA1And8BIT", PREFIX + "$PBKDF2WithHmacSHA18BIT");
199        }
200    }
201}
202