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