equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
41 * @author Valerie Peng |
41 * @author Valerie Peng |
42 * |
42 * |
43 * |
43 * |
44 * @see AESCipher |
44 * @see AESCipher |
45 */ |
45 */ |
46 public final class AESWrapCipher extends CipherSpi { |
46 abstract class AESWrapCipher extends CipherSpi { |
47 |
47 public static final class General extends AESWrapCipher { |
|
48 public General() { |
|
49 super(-1); |
|
50 } |
|
51 } |
|
52 public static final class AES128 extends AESWrapCipher { |
|
53 public AES128() { |
|
54 super(16); |
|
55 } |
|
56 } |
|
57 public static final class AES192 extends AESWrapCipher { |
|
58 public AES192() { |
|
59 super(24); |
|
60 } |
|
61 } |
|
62 public static final class AES256 extends AESWrapCipher { |
|
63 public AES256() { |
|
64 super(32); |
|
65 } |
|
66 } |
48 private static final byte[] IV = { |
67 private static final byte[] IV = { |
49 (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, |
68 (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, |
50 (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6 |
69 (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6 |
51 }; |
70 }; |
52 |
71 |
60 /* |
79 /* |
61 * are we encrypting or decrypting? |
80 * are we encrypting or decrypting? |
62 */ |
81 */ |
63 private boolean decrypting = false; |
82 private boolean decrypting = false; |
64 |
83 |
|
84 /* |
|
85 * needed to support AES oids which associates a fixed key size |
|
86 * to the cipher object. |
|
87 */ |
|
88 private final int fixedKeySize; // in bytes, -1 if no restriction |
|
89 |
65 /** |
90 /** |
66 * Creates an instance of AES KeyWrap cipher with default |
91 * Creates an instance of AES KeyWrap cipher with default |
67 * mode, i.e. "ECB" and padding scheme, i.e. "NoPadding". |
92 * mode, i.e. "ECB" and padding scheme, i.e. "NoPadding". |
68 */ |
93 */ |
69 public AESWrapCipher() { |
94 public AESWrapCipher(int keySize) { |
70 cipher = new AESCrypt(); |
95 cipher = new AESCrypt(); |
|
96 fixedKeySize = keySize; |
|
97 |
71 } |
98 } |
72 |
99 |
73 /** |
100 /** |
74 * Sets the mode of this cipher. Only "ECB" mode is accepted for this |
101 * Sets the mode of this cipher. Only "ECB" mode is accepted for this |
75 * cipher. |
102 * cipher. |
168 decrypting = true; |
195 decrypting = true; |
169 } else { |
196 } else { |
170 throw new UnsupportedOperationException("This cipher can " + |
197 throw new UnsupportedOperationException("This cipher can " + |
171 "only be used for key wrapping and unwrapping"); |
198 "only be used for key wrapping and unwrapping"); |
172 } |
199 } |
|
200 AESCipher.checkKeySize(key, fixedKeySize); |
173 cipher.init(decrypting, key.getAlgorithm(), key.getEncoded()); |
201 cipher.init(decrypting, key.getAlgorithm(), key.getEncoded()); |
174 } |
202 } |
175 |
203 |
176 /** |
204 /** |
177 * Initializes this cipher with a key, a set of algorithm parameters, |
205 * Initializes this cipher with a key, a set of algorithm parameters, |