1 /*
2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.crypto;
27 
28 import java.security.*;
29 import java.security.spec.*;
30 
31 /**
32  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
33  * for the <code>KeyGenerator</code> class.
34  * All the abstract methods in this class must be implemented by each
35  * cryptographic service provider who wishes to supply the implementation
36  * of a key generator for a particular algorithm.
37  *
38  * <p>In case the client does not explicitly initialize the KeyGenerator
39  * (via a call to an {@code init} method), each provider must
40  * supply (and document) a default initialization.
41  * See the Keysize Restriction sections of the
42  * {@extLink security_guide_jdk_providers JDK Providers}
43  * document for information on the KeyGenerator defaults used by
44  * JDK providers.
45  * However, note that defaults may vary across different providers.
46  * Additionally, the default value for a provider may change in a future
47  * version. Therefore, it is recommended to explicitly initialize the
48  * KeyGenerator instead of relying on provider-specific defaults.
49  *
50  * @author Jan Luehe
51  *
52  * @see SecretKey
53  * @since 1.4
54  */
55 
56 public abstract class KeyGeneratorSpi {
57 
58     /**
59      * Initializes the key generator.
60      *
61      * @param random the source of randomness for this generator
62      */
engineInit(SecureRandom random)63     protected abstract void engineInit(SecureRandom random);
64 
65     /**
66      * Initializes the key generator with the specified parameter
67      * set and a user-provided source of randomness.
68      *
69      * @param params the key generation parameters
70      * @param random the source of randomness for this key generator
71      *
72      * @exception InvalidAlgorithmParameterException if <code>params</code> is
73      * inappropriate for this key generator
74      */
engineInit(AlgorithmParameterSpec params, SecureRandom random)75     protected abstract void engineInit(AlgorithmParameterSpec params,
76                                        SecureRandom random)
77         throws InvalidAlgorithmParameterException;
78 
79     /**
80      * Initializes this key generator for a certain keysize, using the given
81      * source of randomness.
82      *
83      * @param keysize the keysize. This is an algorithm-specific metric,
84      * specified in number of bits.
85      * @param random the source of randomness for this key generator
86      *
87      * @exception InvalidParameterException if the keysize is wrong or not
88      * supported.
89      */
engineInit(int keysize, SecureRandom random)90     protected abstract void engineInit(int keysize, SecureRandom random);
91 
92     /**
93      * Generates a secret key.
94      *
95      * @return the new key
96      */
engineGenerateKey()97     protected abstract SecretKey engineGenerateKey();
98 }
99