author | wetmore |
Wed, 13 Apr 2011 11:59:18 -0700 | |
changeset 9265 | 62d885310f4d |
child 15008 | 6a494f8ba5b5 |
permissions | -rw-r--r-- |
9265
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
1 |
/* |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
2 |
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
4 |
* |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
8 |
* |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
13 |
* accompanied this code). |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
14 |
* |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
18 |
* |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
21 |
* questions. |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
22 |
*/ |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
23 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
24 |
/** |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
25 |
* @test |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
26 |
* @bug 7031343 |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
27 |
* @summary Provide API changes to support GCM AEAD ciphers |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
28 |
* @author Brad Wetmore |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
29 |
*/ |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
30 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
31 |
import javax.crypto.*; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
32 |
import javax.crypto.spec.*; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
33 |
import java.nio.ByteBuffer; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
34 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
35 |
/* |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
36 |
* At this point in time, we can't really do any testing since only the API |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
37 |
* is available, the underlying implementation doesn't exist yet. Test |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
38 |
* what we can... |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
39 |
*/ |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
40 |
public class GCMAPI { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
41 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
42 |
// 16 elements |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
43 |
private static byte[] bytes = new byte[] { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
44 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
45 |
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
46 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
47 |
private static int failed = 0; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
48 |
private static Cipher c; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
49 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
50 |
public static void main(String[] args) throws Exception { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
51 |
c = Cipher.getInstance("AES"); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
52 |
c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[16], "AES")); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
53 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
54 |
updateAADFail((byte[]) null); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
55 |
updateAADPass(bytes); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
56 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
57 |
updateAADFail(null, 2, 4); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
58 |
updateAADFail(bytes, -2, 4); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
59 |
updateAADFail(bytes, 2, -4); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
60 |
updateAADFail(bytes, 2, 15); // one too many |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
61 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
62 |
updateAADPass(bytes, 2, 14); // ok. |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
63 |
updateAADPass(bytes, 4, 4); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
64 |
updateAADPass(bytes, 0, 0); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
65 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
66 |
ByteBuffer bb = ByteBuffer.wrap(bytes); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
67 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
68 |
updateAADFail((ByteBuffer) null); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
69 |
updateAADPass(bb); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
70 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
71 |
if (failed != 0) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
72 |
throw new Exception("Test(s) failed"); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
73 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
74 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
75 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
76 |
private static void updateAADPass(byte[] src) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
77 |
try { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
78 |
c.updateAAD(src); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
79 |
} catch (UnsupportedOperationException e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
80 |
// swallow |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
81 |
}catch (Exception e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
82 |
e.printStackTrace(); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
83 |
failed++; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
84 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
85 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
86 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
87 |
private static void updateAADFail(byte[] src) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
88 |
try { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
89 |
c.updateAAD(src); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
90 |
new Exception("Didn't Fail as Expected").printStackTrace(); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
91 |
failed++; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
92 |
} catch (IllegalArgumentException e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
93 |
// swallow |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
94 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
95 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
96 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
97 |
private static void updateAADPass(byte[] src, int offset, int len) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
98 |
try { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
99 |
c.updateAAD(src, offset, len); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
100 |
} catch (UnsupportedOperationException e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
101 |
// swallow |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
102 |
} catch (Exception e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
103 |
e.printStackTrace(); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
104 |
failed++; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
105 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
106 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
107 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
108 |
private static void updateAADFail(byte[] src, int offset, int len) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
109 |
try { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
110 |
c.updateAAD(src, offset, len); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
111 |
new Exception("Didn't Fail as Expected").printStackTrace(); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
112 |
failed++; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
113 |
} catch (IllegalArgumentException e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
114 |
// swallow |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
115 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
116 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
117 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
118 |
private static void updateAADPass(ByteBuffer src) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
119 |
try { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
120 |
c.updateAAD(src); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
121 |
} catch (UnsupportedOperationException e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
122 |
// swallow |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
123 |
}catch (Exception e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
124 |
e.printStackTrace(); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
125 |
failed++; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
126 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
127 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
128 |
|
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
129 |
private static void updateAADFail(ByteBuffer src) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
130 |
try { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
131 |
c.updateAAD(src); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
132 |
new Exception("Didn't Fail as Expected").printStackTrace(); |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
133 |
failed++; |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
134 |
} catch (IllegalArgumentException e) { |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
135 |
// swallow |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
136 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
137 |
} |
62d885310f4d
7031343: Provide API changes to support future GCM AEAD ciphers
wetmore
parents:
diff
changeset
|
138 |
} |