1 package org.bouncycastle.jce.provider; 2 3 import java.security.InvalidAlgorithmParameterException; 4 import java.security.InvalidParameterException; 5 import java.security.SecureRandom; 6 import java.security.spec.AlgorithmParameterSpec; 7 8 import javax.crypto.KeyGeneratorSpi; 9 import javax.crypto.SecretKey; 10 import javax.crypto.spec.SecretKeySpec; 11 12 import org.bouncycastle.crypto.CipherKeyGenerator; 13 import org.bouncycastle.crypto.KeyGenerationParameters; 14 import org.bouncycastle.crypto.generators.DESKeyGenerator; 15 16 public class JCEKeyGenerator 17 extends KeyGeneratorSpi 18 { 19 protected String algName; 20 protected int keySize; 21 protected int defaultKeySize; 22 protected CipherKeyGenerator engine; 23 24 protected boolean uninitialised = true; 25 26 protected JCEKeyGenerator( 27 String algName, 28 int defaultKeySize, 29 CipherKeyGenerator engine) 30 { 31 this.algName = algName; 32 this.keySize = this.defaultKeySize = defaultKeySize; 33 this.engine = engine; 34 } 35 36 protected void engineInit( 37 AlgorithmParameterSpec params, 38 SecureRandom random) 39 throws InvalidAlgorithmParameterException 40 { 41 throw new InvalidAlgorithmParameterException("Not Implemented"); 42 } 43 44 protected void engineInit( 45 SecureRandom random) 46 { 47 if (random != null) 48 { 49 engine.init(new KeyGenerationParameters(random, defaultKeySize)); 50 uninitialised = false; 51 } 52 } 53 54 protected void engineInit( 55 int keySize, 56 SecureRandom random) 57 { 58 try 59 { 60 // BEGIN android-added 61 if (random == null) { 62 random = new SecureRandom(); 63 } 64 // END android-added 65 engine.init(new KeyGenerationParameters(random, keySize)); 66 uninitialised = false; 67 } 68 catch (IllegalArgumentException e) 69 { 70 throw new InvalidParameterException(e.getMessage()); 71 } 72 } 73 74 protected SecretKey engineGenerateKey() 75 { 76 if (uninitialised) 77 { 78 engine.init(new KeyGenerationParameters(new SecureRandom(), defaultKeySize)); 79 uninitialised = false; 80 } 81 82 return new SecretKeySpec(engine.generateKey(), algName); 83 } 84 85 /** 86 * the generators that are defined directly off us. 87 */ 88 89 /** 90 * DES 91 */ 92 public static class DES 93 extends JCEKeyGenerator 94 { 95 public DES() 96 { 97 super("DES", 64, new DESKeyGenerator()); 98 } 99 } 100 101 // BEGIN android-removed 102 // /** 103 // * RC2 104 // */ 105 // public static class RC2 106 // extends JCEKeyGenerator 107 // { 108 // public RC2() 109 // { 110 // super("RC2", 128, new CipherKeyGenerator()); 111 // } 112 // } 113 // 114 // /** 115 // * GOST28147 116 // */ 117 // public static class GOST28147 118 // extends JCEKeyGenerator 119 // { 120 // public GOST28147() 121 // { 122 // super("GOST28147", 256, new CipherKeyGenerator()); 123 // } 124 // } 125 // END android-removed 126 127 // HMAC Related secret keys.. 128 129 // BEGIN android-removed 130 // /** 131 // * MD2HMAC 132 // */ 133 // public static class MD2HMAC 134 // extends JCEKeyGenerator 135 // { 136 // public MD2HMAC() 137 // { 138 // super("HMACMD2", 128, new CipherKeyGenerator()); 139 // } 140 // } 141 // 142 // 143 // /** 144 // * MD4HMAC 145 // */ 146 // public static class MD4HMAC 147 // extends JCEKeyGenerator 148 // { 149 // public MD4HMAC() 150 // { 151 // super("HMACMD4", 128, new CipherKeyGenerator()); 152 // } 153 // } 154 // END android-removed 155 156 /** 157 * MD5HMAC 158 */ 159 public static class MD5HMAC 160 extends JCEKeyGenerator 161 { 162 public MD5HMAC() 163 { 164 super("HMACMD5", 128, new CipherKeyGenerator()); 165 } 166 } 167 168 169 // /** 170 // * RIPE128HMAC 171 // */ 172 // public static class RIPEMD128HMAC 173 // extends JCEKeyGenerator 174 // { 175 // public RIPEMD128HMAC() 176 // { 177 // super("HMACRIPEMD128", 128, new CipherKeyGenerator()); 178 // } 179 // } 180 181 // /** 182 // * RIPE160HMAC 183 // */ 184 // public static class RIPEMD160HMAC 185 // extends JCEKeyGenerator 186 // { 187 // public RIPEMD160HMAC() 188 // { 189 // super("HMACRIPEMD160", 160, new CipherKeyGenerator()); 190 // } 191 // } 192 193 194 /** 195 * HMACSHA1 196 */ 197 public static class HMACSHA1 198 extends JCEKeyGenerator 199 { 200 public HMACSHA1() 201 { 202 super("HMACSHA1", 160, new CipherKeyGenerator()); 203 } 204 } 205 206 // BEGIN android-removed 207 // /** 208 // * HMACSHA224 209 // */ 210 // public static class HMACSHA224 211 // extends JCEKeyGenerator 212 // { 213 // public HMACSHA224() 214 // { 215 // super("HMACSHA224", 224, new CipherKeyGenerator()); 216 // } 217 // } 218 // END android-removed 219 220 /** 221 * HMACSHA256 222 */ 223 public static class HMACSHA256 224 extends JCEKeyGenerator 225 { 226 public HMACSHA256() 227 { 228 super("HMACSHA256", 256, new CipherKeyGenerator()); 229 } 230 } 231 232 /** 233 * HMACSHA384 234 */ 235 public static class HMACSHA384 236 extends JCEKeyGenerator 237 { 238 public HMACSHA384() 239 { 240 super("HMACSHA384", 384, new CipherKeyGenerator()); 241 } 242 } 243 244 /** 245 * HMACSHA512 246 */ 247 public static class HMACSHA512 248 extends JCEKeyGenerator 249 { 250 public HMACSHA512() 251 { 252 super("HMACSHA512", 512, new CipherKeyGenerator()); 253 } 254 } 255 256 // BEGIN android-removed 257 // /** 258 // * HMACTIGER 259 // */ 260 // public static class HMACTIGER 261 // extends JCEKeyGenerator 262 // { 263 // public HMACTIGER() 264 // { 265 // super("HMACTIGER", 192, new CipherKeyGenerator()); 266 // } 267 // } 268 // END android-removed 269 } 270