1 /*
2  * Copyright (c) 1999, 2020, 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.Key;
29 import java.security.AlgorithmParameters;
30 import java.security.InvalidKeyException;
31 import java.security.InvalidAlgorithmParameterException;
32 import java.security.spec.AlgorithmParameterSpec;
33 
34 /**
35  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
36  * for the <code>ExemptionMechanism</code> class.
37  * All the abstract methods in this class must be implemented by each
38  * cryptographic service provider who wishes to supply the implementation
39  * of a particular exemption mechanism.
40  *
41  * @author Sharon Liu
42  *
43  * @since 1.4
44  */
45 
46 public abstract class ExemptionMechanismSpi {
47 
48     /**
49      * Constructor for subclasses to call.
50      */
ExemptionMechanismSpi()51     public ExemptionMechanismSpi() {}
52 
53     /**
54      * Returns the length in bytes that an output buffer would need to be in
55      * order to hold the result of the next
56      * {@link #engineGenExemptionBlob(byte[], int) engineGenExemptionBlob}
57      * operation, given the input length <code>inputLen</code> (in bytes).
58      *
59      * <p>The actual output length of the next
60      * {@link #engineGenExemptionBlob(byte[], int) engineGenExemptionBlob}
61      * call may be smaller than the length returned by this method.
62      *
63      * @param inputLen the input length (in bytes)
64      *
65      * @return the required output buffer size (in bytes)
66      */
engineGetOutputSize(int inputLen)67     protected abstract int engineGetOutputSize(int inputLen);
68 
69     /**
70      * Initializes this exemption mechanism with a key.
71      *
72      * <p>If this exemption mechanism requires any algorithm parameters
73      * that cannot be derived from the given <code>key</code>, the underlying
74      * exemption mechanism implementation is supposed to generate the required
75      * parameters itself (using provider-specific default values); in the case
76      * that algorithm parameters must be specified by the caller, an
77      * <code>InvalidKeyException</code> is raised.
78      *
79      * @param key the key for this exemption mechanism
80      *
81      * @exception InvalidKeyException if the given key is inappropriate for
82      * this exemption mechanism.
83      * @exception ExemptionMechanismException if problem(s) encountered in the
84      * process of initializing.
85      */
engineInit(Key key)86     protected abstract void engineInit(Key key)
87     throws InvalidKeyException, ExemptionMechanismException;
88 
89     /**
90      * Initializes this exemption mechanism with a key and a set of algorithm
91      * parameters.
92      *
93      * <p>If this exemption mechanism requires any algorithm parameters and
94      * <code>params</code> is null, the underlying exemption mechanism
95      * implementation is supposed to generate the required parameters
96      * itself (using provider-specific default values); in the case that
97      * algorithm parameters must be specified by the caller, an
98      * <code>InvalidAlgorithmParameterException</code> is raised.
99      *
100      * @param key the key for this exemption mechanism
101      * @param params the algorithm parameters
102      *
103      * @exception InvalidKeyException if the given key is inappropriate for
104      * this exemption mechanism.
105      * @exception InvalidAlgorithmParameterException if the given algorithm
106      * parameters are inappropriate for this exemption mechanism.
107      * @exception ExemptionMechanismException if problem(s) encountered in the
108      * process of initializing.
109      */
engineInit(Key key, AlgorithmParameterSpec params)110     protected abstract void engineInit(Key key, AlgorithmParameterSpec params)
111     throws InvalidKeyException, InvalidAlgorithmParameterException,
112     ExemptionMechanismException;
113 
114     /**
115      * Initializes this exemption mechanism with a key and a set of algorithm
116      * parameters.
117      *
118      * <p>If this exemption mechanism requires any algorithm parameters
119      * and <code>params</code> is null, the underlying exemption mechanism
120      * implementation is supposed to generate the required parameters
121      * itself (using provider-specific default values); in the case that
122      * algorithm parameters must be specified by the caller, an
123      * <code>InvalidAlgorithmParameterException</code> is raised.
124      *
125      * @param key the key for this exemption mechanism
126      * @param params the algorithm parameters
127      *
128      * @exception InvalidKeyException if the given key is inappropriate for
129      * this exemption mechanism.
130      * @exception InvalidAlgorithmParameterException if the given algorithm
131      * parameters are inappropriate for this exemption mechanism.
132      * @exception ExemptionMechanismException if problem(s) encountered in the
133      * process of initializing.
134      */
engineInit(Key key, AlgorithmParameters params)135     protected abstract void engineInit(Key key, AlgorithmParameters params)
136     throws InvalidKeyException, InvalidAlgorithmParameterException,
137     ExemptionMechanismException;
138 
139     /**
140      * Generates the exemption mechanism key blob.
141      *
142      * @return the new buffer with the result key blob.
143      *
144      * @exception ExemptionMechanismException if problem(s) encountered in the
145      * process of generating.
146      */
engineGenExemptionBlob()147     protected abstract byte[] engineGenExemptionBlob()
148         throws ExemptionMechanismException;
149 
150     /**
151      * Generates the exemption mechanism key blob, and stores the result in
152      * the <code>output</code> buffer, starting at <code>outputOffset</code>
153      * inclusive.
154      *
155      * <p>If the <code>output</code> buffer is too small to hold the result,
156      * a <code>ShortBufferException</code> is thrown. In this case, repeat this
157      * call with a larger output buffer. Use
158      * {@link #engineGetOutputSize(int) engineGetOutputSize} to determine
159      * how big the output buffer should be.
160      *
161      * @param output the buffer for the result
162      * @param outputOffset the offset in <code>output</code> where the result
163      * is stored
164      *
165      * @return the number of bytes stored in <code>output</code>
166      *
167      * @exception ShortBufferException if the given output buffer is too small
168      * to hold the result.
169      * @exception ExemptionMechanismException if problem(s) encountered in the
170      * process of generating.
171      */
engineGenExemptionBlob(byte[] output, int outputOffset)172     protected abstract int engineGenExemptionBlob
173     (byte[] output, int outputOffset)
174         throws ShortBufferException, ExemptionMechanismException;
175 }
176