author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 15008 | jdk/src/share/classes/com/sun/crypto/provider/AESCipher.java@6a494f8ba5b5 |
child 39750 | 982b75e31495 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15008 | 2 |
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.security.spec.*; |
|
30 |
import javax.crypto.*; |
|
31 |
import javax.crypto.spec.*; |
|
32 |
import javax.crypto.BadPaddingException; |
|
15008 | 33 |
import java.nio.ByteBuffer; |
2 | 34 |
|
35 |
/** |
|
36 |
* This class implements the AES algorithm in its various modes |
|
37 |
* (<code>ECB</code>, <code>CFB</code>, <code>OFB</code>, <code>CBC</code>, |
|
38 |
* <code>PCBC</code>) and padding schemes (<code>PKCS5Padding</code>, |
|
39 |
* <code>NoPadding</code>, <code>ISO10126Padding</code>). |
|
40 |
* |
|
41 |
* @author Valerie Peng |
|
42 |
* |
|
43 |
* |
|
44 |
* @see AESCrypt |
|
45 |
* @see CipherBlockChaining |
|
46 |
* @see ElectronicCodeBook |
|
47 |
* @see CipherFeedback |
|
48 |
* @see OutputFeedback |
|
49 |
*/ |
|
50 |
||
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
51 |
abstract class AESCipher extends CipherSpi { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
52 |
public static final class General extends AESCipher { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
53 |
public General() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
54 |
super(-1); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
55 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
56 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
57 |
abstract static class OidImpl extends AESCipher { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
58 |
protected OidImpl(int keySize, String mode, String padding) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
59 |
super(keySize); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
60 |
try { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
61 |
engineSetMode(mode); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
62 |
engineSetPadding(padding); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
63 |
} catch (GeneralSecurityException gse) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
64 |
// internal error; re-throw as provider exception |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
65 |
ProviderException pe =new ProviderException("Internal Error"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
66 |
pe.initCause(gse); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
67 |
throw pe; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
68 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
69 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
70 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
71 |
public static final class AES128_ECB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
72 |
public AES128_ECB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
73 |
super(16, "ECB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
74 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
75 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
76 |
public static final class AES192_ECB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
77 |
public AES192_ECB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
78 |
super(24, "ECB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
79 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
80 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
81 |
public static final class AES256_ECB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
82 |
public AES256_ECB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
83 |
super(32, "ECB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
84 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
85 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
86 |
public static final class AES128_CBC_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
87 |
public AES128_CBC_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
88 |
super(16, "CBC", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
89 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
90 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
91 |
public static final class AES192_CBC_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
92 |
public AES192_CBC_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
93 |
super(24, "CBC", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
94 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
95 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
96 |
public static final class AES256_CBC_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
97 |
public AES256_CBC_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
98 |
super(32, "CBC", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
99 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
100 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
101 |
public static final class AES128_OFB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
102 |
public AES128_OFB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
103 |
super(16, "OFB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
104 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
105 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
106 |
public static final class AES192_OFB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
107 |
public AES192_OFB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
108 |
super(24, "OFB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
109 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
110 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
111 |
public static final class AES256_OFB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
112 |
public AES256_OFB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
113 |
super(32, "OFB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
114 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
115 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
116 |
public static final class AES128_CFB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
117 |
public AES128_CFB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
118 |
super(16, "CFB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
119 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
120 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
121 |
public static final class AES192_CFB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
122 |
public AES192_CFB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
123 |
super(24, "CFB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
124 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
125 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
126 |
public static final class AES256_CFB_NoPadding extends OidImpl { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
127 |
public AES256_CFB_NoPadding() { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
128 |
super(32, "CFB", "NOPADDING"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
129 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
130 |
} |
15008 | 131 |
public static final class AES128_GCM_NoPadding extends OidImpl { |
132 |
public AES128_GCM_NoPadding() { |
|
133 |
super(16, "GCM", "NOPADDING"); |
|
134 |
} |
|
135 |
} |
|
136 |
public static final class AES192_GCM_NoPadding extends OidImpl { |
|
137 |
public AES192_GCM_NoPadding() { |
|
138 |
super(24, "GCM", "NOPADDING"); |
|
139 |
} |
|
140 |
} |
|
141 |
public static final class AES256_GCM_NoPadding extends OidImpl { |
|
142 |
public AES256_GCM_NoPadding() { |
|
143 |
super(32, "GCM", "NOPADDING"); |
|
144 |
} |
|
145 |
} |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
146 |
|
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
147 |
// utility method used by AESCipher and AESWrapCipher |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
148 |
static final void checkKeySize(Key key, int fixedKeySize) |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
149 |
throws InvalidKeyException { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
150 |
if (fixedKeySize != -1) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
151 |
if (key == null) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
152 |
throw new InvalidKeyException("The key must not be null"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
153 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
154 |
byte[] value = key.getEncoded(); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
155 |
if (value == null) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
156 |
throw new InvalidKeyException("Key encoding must not be null"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
157 |
} else if (value.length != fixedKeySize) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
158 |
throw new InvalidKeyException("The key must be " + |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
159 |
fixedKeySize*8 + " bits"); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
160 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
161 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
162 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
163 |
|
2 | 164 |
/* |
165 |
* internal CipherCore object which does the real work. |
|
166 |
*/ |
|
167 |
private CipherCore core = null; |
|
168 |
||
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
169 |
/* |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
170 |
* needed to support AES oids which associates a fixed key size |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
171 |
* to the cipher object. |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
172 |
*/ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
173 |
private final int fixedKeySize; // in bytes, -1 if no restriction |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
174 |
|
2 | 175 |
/** |
176 |
* Creates an instance of AES cipher with default ECB mode and |
|
177 |
* PKCS5Padding. |
|
178 |
*/ |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
179 |
protected AESCipher(int keySize) { |
2 | 180 |
core = new CipherCore(new AESCrypt(), AESConstants.AES_BLOCK_SIZE); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
181 |
fixedKeySize = keySize; |
2 | 182 |
} |
183 |
||
184 |
/** |
|
185 |
* Sets the mode of this cipher. |
|
186 |
* |
|
187 |
* @param mode the cipher mode |
|
188 |
* |
|
189 |
* @exception NoSuchAlgorithmException if the requested cipher mode does |
|
190 |
* not exist |
|
191 |
*/ |
|
192 |
protected void engineSetMode(String mode) |
|
193 |
throws NoSuchAlgorithmException { |
|
194 |
core.setMode(mode); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Sets the padding mechanism of this cipher. |
|
199 |
* |
|
200 |
* @param padding the padding mechanism |
|
201 |
* |
|
202 |
* @exception NoSuchPaddingException if the requested padding mechanism |
|
203 |
* does not exist |
|
204 |
*/ |
|
205 |
protected void engineSetPadding(String paddingScheme) |
|
206 |
throws NoSuchPaddingException { |
|
207 |
core.setPadding(paddingScheme); |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Returns the block size (in bytes). |
|
212 |
* |
|
213 |
* @return the block size (in bytes), or 0 if the underlying algorithm is |
|
214 |
* not a block cipher |
|
215 |
*/ |
|
216 |
protected int engineGetBlockSize() { |
|
217 |
return AESConstants.AES_BLOCK_SIZE; |
|
218 |
} |
|
219 |
||
220 |
/** |
|
221 |
* Returns the length in bytes that an output buffer would need to be in |
|
222 |
* order to hold the result of the next <code>update</code> or |
|
223 |
* <code>doFinal</code> operation, given the input length |
|
224 |
* <code>inputLen</code> (in bytes). |
|
225 |
* |
|
226 |
* <p>This call takes into account any unprocessed (buffered) data from a |
|
227 |
* previous <code>update</code> call, and padding. |
|
228 |
* |
|
229 |
* <p>The actual output length of the next <code>update</code> or |
|
230 |
* <code>doFinal</code> call may be smaller than the length returned by |
|
231 |
* this method. |
|
232 |
* |
|
233 |
* @param inputLen the input length (in bytes) |
|
234 |
* |
|
235 |
* @return the required output buffer size (in bytes) |
|
236 |
*/ |
|
237 |
protected int engineGetOutputSize(int inputLen) { |
|
238 |
return core.getOutputSize(inputLen); |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Returns the initialization vector (IV) in a new buffer. |
|
243 |
* |
|
244 |
* <p>This is useful in the case where a random IV has been created |
|
245 |
* (see <a href = "#init">init</a>), |
|
246 |
* or in the context of password-based encryption or |
|
247 |
* decryption, where the IV is derived from a user-provided password. |
|
248 |
* |
|
249 |
* @return the initialization vector in a new buffer, or null if the |
|
250 |
* underlying algorithm does not use an IV, or if the IV has not yet |
|
251 |
* been set. |
|
252 |
*/ |
|
253 |
protected byte[] engineGetIV() { |
|
254 |
return core.getIV(); |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Returns the parameters used with this cipher. |
|
259 |
* |
|
260 |
* <p>The returned parameters may be the same that were used to initialize |
|
261 |
* this cipher, or may contain the default set of parameters or a set of |
|
262 |
* randomly generated parameters used by the underlying cipher |
|
263 |
* implementation (provided that the underlying cipher implementation |
|
264 |
* uses a default set of parameters or creates new parameters if it needs |
|
265 |
* parameters but was not initialized with any). |
|
266 |
* |
|
267 |
* @return the parameters used with this cipher, or null if this cipher |
|
268 |
* does not use any parameters. |
|
269 |
*/ |
|
270 |
protected AlgorithmParameters engineGetParameters() { |
|
271 |
return core.getParameters("AES"); |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Initializes this cipher with a key and a source of randomness. |
|
276 |
* |
|
277 |
* <p>The cipher is initialized for one of the following four operations: |
|
278 |
* encryption, decryption, key wrapping or key unwrapping, depending on |
|
279 |
* the value of <code>opmode</code>. |
|
280 |
* |
|
281 |
* <p>If this cipher requires an initialization vector (IV), it will get |
|
282 |
* it from <code>random</code>. |
|
283 |
* This behaviour should only be used in encryption or key wrapping |
|
284 |
* mode, however. |
|
285 |
* When initializing a cipher that requires an IV for decryption or |
|
286 |
* key unwrapping, the IV |
|
287 |
* (same IV that was used for encryption or key wrapping) must be provided |
|
288 |
* explicitly as a |
|
289 |
* parameter, in order to get the correct result. |
|
290 |
* |
|
291 |
* <p>This method also cleans existing buffer and other related state |
|
292 |
* information. |
|
293 |
* |
|
294 |
* @param opmode the operation mode of this cipher (this is one of |
|
295 |
* the following: |
|
296 |
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>, |
|
297 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) |
|
298 |
* @param key the secret key |
|
299 |
* @param random the source of randomness |
|
300 |
* |
|
301 |
* @exception InvalidKeyException if the given key is inappropriate for |
|
302 |
* initializing this cipher |
|
303 |
*/ |
|
304 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
|
305 |
throws InvalidKeyException { |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
306 |
checkKeySize(key, fixedKeySize); |
2 | 307 |
core.init(opmode, key, random); |
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Initializes this cipher with a key, a set of |
|
312 |
* algorithm parameters, and a source of randomness. |
|
313 |
* |
|
314 |
* <p>The cipher is initialized for one of the following four operations: |
|
315 |
* encryption, decryption, key wrapping or key unwrapping, depending on |
|
316 |
* the value of <code>opmode</code>. |
|
317 |
* |
|
318 |
* <p>If this cipher (including its underlying feedback or padding scheme) |
|
319 |
* requires any random bytes, it will get them from <code>random</code>. |
|
320 |
* |
|
321 |
* @param opmode the operation mode of this cipher (this is one of |
|
322 |
* the following: |
|
323 |
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>, |
|
324 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) |
|
325 |
* @param key the encryption key |
|
326 |
* @param params the algorithm parameters |
|
327 |
* @param random the source of randomness |
|
328 |
* |
|
329 |
* @exception InvalidKeyException if the given key is inappropriate for |
|
330 |
* initializing this cipher |
|
331 |
* @exception InvalidAlgorithmParameterException if the given algorithm |
|
332 |
* parameters are inappropriate for this cipher |
|
333 |
*/ |
|
334 |
protected void engineInit(int opmode, Key key, |
|
335 |
AlgorithmParameterSpec params, |
|
336 |
SecureRandom random) |
|
337 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
338 |
checkKeySize(key, fixedKeySize); |
2 | 339 |
core.init(opmode, key, params, random); |
340 |
} |
|
341 |
||
342 |
protected void engineInit(int opmode, Key key, |
|
343 |
AlgorithmParameters params, |
|
344 |
SecureRandom random) |
|
345 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
346 |
checkKeySize(key, fixedKeySize); |
2 | 347 |
core.init(opmode, key, params, random); |
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* Continues a multiple-part encryption or decryption operation |
|
352 |
* (depending on how this cipher was initialized), processing another data |
|
353 |
* part. |
|
354 |
* |
|
355 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
356 |
* buffer, starting at <code>inputOffset</code>, are processed, and the |
|
357 |
* result is stored in a new buffer. |
|
358 |
* |
|
359 |
* @param input the input buffer |
|
360 |
* @param inputOffset the offset in <code>input</code> where the input |
|
361 |
* starts |
|
362 |
* @param inputLen the input length |
|
363 |
* |
|
364 |
* @return the new buffer with the result |
|
365 |
* |
|
366 |
* @exception IllegalStateException if this cipher is in a wrong state |
|
367 |
* (e.g., has not been initialized) |
|
368 |
*/ |
|
369 |
protected byte[] engineUpdate(byte[] input, int inputOffset, |
|
370 |
int inputLen) { |
|
371 |
return core.update(input, inputOffset, inputLen); |
|
372 |
} |
|
373 |
||
374 |
/** |
|
375 |
* Continues a multiple-part encryption or decryption operation |
|
376 |
* (depending on how this cipher was initialized), processing another data |
|
377 |
* part. |
|
378 |
* |
|
379 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
380 |
* buffer, starting at <code>inputOffset</code>, are processed, and the |
|
381 |
* result is stored in the <code>output</code> buffer, starting at |
|
382 |
* <code>outputOffset</code>. |
|
383 |
* |
|
384 |
* @param input the input buffer |
|
385 |
* @param inputOffset the offset in <code>input</code> where the input |
|
386 |
* starts |
|
387 |
* @param inputLen the input length |
|
388 |
* @param output the buffer for the result |
|
389 |
* @param outputOffset the offset in <code>output</code> where the result |
|
390 |
* is stored |
|
391 |
* |
|
392 |
* @return the number of bytes stored in <code>output</code> |
|
393 |
* |
|
394 |
* @exception ShortBufferException if the given output buffer is too small |
|
395 |
* to hold the result |
|
396 |
*/ |
|
397 |
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, |
|
398 |
byte[] output, int outputOffset) |
|
399 |
throws ShortBufferException { |
|
400 |
return core.update(input, inputOffset, inputLen, output, |
|
401 |
outputOffset); |
|
402 |
} |
|
403 |
||
404 |
/** |
|
405 |
* Encrypts or decrypts data in a single-part operation, |
|
406 |
* or finishes a multiple-part operation. |
|
407 |
* The data is encrypted or decrypted, depending on how this cipher was |
|
408 |
* initialized. |
|
409 |
* |
|
410 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
411 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that |
|
412 |
* may have been buffered during a previous <code>update</code> operation, |
|
413 |
* are processed, with padding (if requested) being applied. |
|
414 |
* The result is stored in a new buffer. |
|
415 |
* |
|
416 |
* <p>The cipher is reset to its initial state (uninitialized) after this |
|
417 |
* call. |
|
418 |
* |
|
419 |
* @param input the input buffer |
|
420 |
* @param inputOffset the offset in <code>input</code> where the input |
|
421 |
* starts |
|
422 |
* @param inputLen the input length |
|
423 |
* |
|
424 |
* @return the new buffer with the result |
|
425 |
* |
|
426 |
* @exception IllegalBlockSizeException if this cipher is a block cipher, |
|
427 |
* no padding has been requested (only in encryption mode), and the total |
|
428 |
* input length of the data processed by this cipher is not a multiple of |
|
429 |
* block size |
|
430 |
* @exception BadPaddingException if this cipher is in decryption mode, |
|
431 |
* and (un)padding has been requested, but the decrypted data is not |
|
432 |
* bounded by the appropriate padding bytes |
|
433 |
*/ |
|
434 |
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) |
|
435 |
throws IllegalBlockSizeException, BadPaddingException { |
|
436 |
return core.doFinal(input, inputOffset, inputLen); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Encrypts or decrypts data in a single-part operation, |
|
441 |
* or finishes a multiple-part operation. |
|
442 |
* The data is encrypted or decrypted, depending on how this cipher was |
|
443 |
* initialized. |
|
444 |
* |
|
445 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
446 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that |
|
447 |
* may have been buffered during a previous <code>update</code> operation, |
|
448 |
* are processed, with padding (if requested) being applied. |
|
449 |
* The result is stored in the <code>output</code> buffer, starting at |
|
450 |
* <code>outputOffset</code>. |
|
451 |
* |
|
452 |
* <p>The cipher is reset to its initial state (uninitialized) after this |
|
453 |
* call. |
|
454 |
* |
|
455 |
* @param input the input buffer |
|
456 |
* @param inputOffset the offset in <code>input</code> where the input |
|
457 |
* starts |
|
458 |
* @param inputLen the input length |
|
459 |
* @param output the buffer for the result |
|
460 |
* @param outputOffset the offset in <code>output</code> where the result |
|
461 |
* is stored |
|
462 |
* |
|
463 |
* @return the number of bytes stored in <code>output</code> |
|
464 |
* |
|
465 |
* @exception IllegalBlockSizeException if this cipher is a block cipher, |
|
466 |
* no padding has been requested (only in encryption mode), and the total |
|
467 |
* input length of the data processed by this cipher is not a multiple of |
|
468 |
* block size |
|
469 |
* @exception ShortBufferException if the given output buffer is too small |
|
470 |
* to hold the result |
|
471 |
* @exception BadPaddingException if this cipher is in decryption mode, |
|
472 |
* and (un)padding has been requested, but the decrypted data is not |
|
473 |
* bounded by the appropriate padding bytes |
|
474 |
*/ |
|
475 |
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, |
|
476 |
byte[] output, int outputOffset) |
|
477 |
throws IllegalBlockSizeException, ShortBufferException, |
|
478 |
BadPaddingException { |
|
479 |
return core.doFinal(input, inputOffset, inputLen, output, |
|
480 |
outputOffset); |
|
481 |
} |
|
482 |
||
483 |
/** |
|
484 |
* Returns the key size of the given key object. |
|
485 |
* |
|
486 |
* @param key the key object. |
|
487 |
* |
|
488 |
* @return the key size of the given key object. |
|
489 |
* |
|
490 |
* @exception InvalidKeyException if <code>key</code> is invalid. |
|
491 |
*/ |
|
492 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
|
493 |
byte[] encoded = key.getEncoded(); |
|
494 |
if (!AESCrypt.isKeySizeValid(encoded.length)) { |
|
495 |
throw new InvalidKeyException("Invalid AES key length: " + |
|
496 |
encoded.length + " bytes"); |
|
497 |
} |
|
498 |
return encoded.length * 8; |
|
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* Wrap a key. |
|
503 |
* |
|
504 |
* @param key the key to be wrapped. |
|
505 |
* |
|
506 |
* @return the wrapped key. |
|
507 |
* |
|
508 |
* @exception IllegalBlockSizeException if this cipher is a block |
|
509 |
* cipher, no padding has been requested, and the length of the |
|
510 |
* encoding of the key to be wrapped is not a |
|
511 |
* multiple of the block size. |
|
512 |
* |
|
513 |
* @exception InvalidKeyException if it is impossible or unsafe to |
|
514 |
* wrap the key with this cipher (e.g., a hardware protected key is |
|
515 |
* being passed to a software only cipher). |
|
516 |
*/ |
|
517 |
protected byte[] engineWrap(Key key) |
|
518 |
throws IllegalBlockSizeException, InvalidKeyException { |
|
519 |
return core.wrap(key); |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Unwrap a previously wrapped key. |
|
524 |
* |
|
525 |
* @param wrappedKey the key to be unwrapped. |
|
526 |
* |
|
527 |
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for. |
|
528 |
* |
|
529 |
* @param wrappedKeyType the type of the wrapped key. |
|
530 |
* This is one of <code>Cipher.SECRET_KEY</code>, |
|
531 |
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>. |
|
532 |
* |
|
533 |
* @return the unwrapped key. |
|
534 |
* |
|
535 |
* @exception NoSuchAlgorithmException if no installed providers |
|
536 |
* can create keys of type <code>wrappedKeyType</code> for the |
|
537 |
* <code>wrappedKeyAlgorithm</code>. |
|
538 |
* |
|
539 |
* @exception InvalidKeyException if <code>wrappedKey</code> does not |
|
540 |
* represent a wrapped key of type <code>wrappedKeyType</code> for |
|
541 |
* the <code>wrappedKeyAlgorithm</code>. |
|
542 |
*/ |
|
543 |
protected Key engineUnwrap(byte[] wrappedKey, |
|
544 |
String wrappedKeyAlgorithm, |
|
545 |
int wrappedKeyType) |
|
546 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
547 |
return core.unwrap(wrappedKey, wrappedKeyAlgorithm, |
|
548 |
wrappedKeyType); |
|
549 |
} |
|
15008 | 550 |
|
551 |
/** |
|
552 |
* Continues a multi-part update of the Additional Authentication |
|
553 |
* Data (AAD), using a subset of the provided buffer. |
|
554 |
* <p> |
|
555 |
* Calls to this method provide AAD to the cipher when operating in |
|
556 |
* modes such as AEAD (GCM/CCM). If this cipher is operating in |
|
557 |
* either GCM or CCM mode, all AAD must be supplied before beginning |
|
558 |
* operations on the ciphertext (via the {@code update} and {@code |
|
559 |
* doFinal} methods). |
|
560 |
* |
|
561 |
* @param src the buffer containing the AAD |
|
562 |
* @param offset the offset in {@code src} where the AAD input starts |
|
563 |
* @param len the number of AAD bytes |
|
564 |
* |
|
565 |
* @throws IllegalStateException if this cipher is in a wrong state |
|
566 |
* (e.g., has not been initialized), does not accept AAD, or if |
|
567 |
* operating in either GCM or CCM mode and one of the {@code update} |
|
568 |
* methods has already been called for the active |
|
569 |
* encryption/decryption operation |
|
570 |
* @throws UnsupportedOperationException if this method |
|
571 |
* has not been overridden by an implementation |
|
572 |
* |
|
573 |
* @since 1.8 |
|
574 |
*/ |
|
575 |
@Override |
|
576 |
protected void engineUpdateAAD(byte[] src, int offset, int len) { |
|
577 |
core.updateAAD(src, offset, len); |
|
578 |
} |
|
579 |
||
580 |
/** |
|
581 |
* Continues a multi-part update of the Additional Authentication |
|
582 |
* Data (AAD). |
|
583 |
* <p> |
|
584 |
* Calls to this method provide AAD to the cipher when operating in |
|
585 |
* modes such as AEAD (GCM/CCM). If this cipher is operating in |
|
586 |
* either GCM or CCM mode, all AAD must be supplied before beginning |
|
587 |
* operations on the ciphertext (via the {@code update} and {@code |
|
588 |
* doFinal} methods). |
|
589 |
* <p> |
|
590 |
* All {@code src.remaining()} bytes starting at |
|
591 |
* {@code src.position()} are processed. |
|
592 |
* Upon return, the input buffer's position will be equal |
|
593 |
* to its limit; its limit will not have changed. |
|
594 |
* |
|
595 |
* @param src the buffer containing the AAD |
|
596 |
* |
|
597 |
* @throws IllegalStateException if this cipher is in a wrong state |
|
598 |
* (e.g., has not been initialized), does not accept AAD, or if |
|
599 |
* operating in either GCM or CCM mode and one of the {@code update} |
|
600 |
* methods has already been called for the active |
|
601 |
* encryption/decryption operation |
|
602 |
* @throws UnsupportedOperationException if this method |
|
603 |
* has not been overridden by an implementation |
|
604 |
* |
|
605 |
* @since 1.8 |
|
606 |
*/ |
|
607 |
@Override |
|
608 |
protected void engineUpdateAAD(ByteBuffer src) { |
|
609 |
if (src != null) { |
|
610 |
int aadLen = src.limit() - src.position(); |
|
611 |
if (aadLen != 0) { |
|
612 |
if (src.hasArray()) { |
|
613 |
int aadOfs = src.arrayOffset() + src.position(); |
|
614 |
core.updateAAD(src.array(), aadOfs, aadLen); |
|
615 |
src.position(src.limit()); |
|
616 |
} else { |
|
617 |
byte[] aad = new byte[aadLen]; |
|
618 |
src.get(aad); |
|
619 |
core.updateAAD(aad, 0, aadLen); |
|
620 |
} |
|
621 |
} |
|
622 |
} |
|
623 |
} |
|
2 | 624 |
} |
15008 | 625 |