jdk/test/javax/crypto/Cipher/GCMAPI.java
changeset 9265 62d885310f4d
child 15008 6a494f8ba5b5
equal deleted inserted replaced
9264:f49ad2f5d240 9265:62d885310f4d
       
     1 /*
       
     2  * Copyright (c) 2011, 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 
       
    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 
       
    76     private static void updateAADPass(byte[] src) {
       
    77         try {
       
    78             c.updateAAD(src);
       
    79         } catch (UnsupportedOperationException e) {
       
    80             // swallow
       
    81         }catch (Exception e) {
       
    82             e.printStackTrace();
       
    83             failed++;
       
    84         }
       
    85     }
       
    86 
       
    87     private static void updateAADFail(byte[] src) {
       
    88         try {
       
    89             c.updateAAD(src);
       
    90             new Exception("Didn't Fail as Expected").printStackTrace();
       
    91             failed++;
       
    92         } catch (IllegalArgumentException e) {
       
    93             // swallow
       
    94         }
       
    95     }
       
    96 
       
    97     private static void updateAADPass(byte[] src, int offset, int len) {
       
    98         try {
       
    99             c.updateAAD(src, offset, len);
       
   100         } catch (UnsupportedOperationException e) {
       
   101             // swallow
       
   102         } catch (Exception e) {
       
   103             e.printStackTrace();
       
   104             failed++;
       
   105         }
       
   106     }
       
   107 
       
   108     private static void updateAADFail(byte[] src, int offset, int len) {
       
   109         try {
       
   110             c.updateAAD(src, offset, len);
       
   111             new Exception("Didn't Fail as Expected").printStackTrace();
       
   112             failed++;
       
   113         } catch (IllegalArgumentException e) {
       
   114             // swallow
       
   115         }
       
   116     }
       
   117 
       
   118     private static void updateAADPass(ByteBuffer src) {
       
   119         try {
       
   120             c.updateAAD(src);
       
   121         } catch (UnsupportedOperationException e) {
       
   122             // swallow
       
   123         }catch (Exception e) {
       
   124             e.printStackTrace();
       
   125             failed++;
       
   126         }
       
   127     }
       
   128 
       
   129     private static void updateAADFail(ByteBuffer src) {
       
   130         try {
       
   131             c.updateAAD(src);
       
   132             new Exception("Didn't Fail as Expected").printStackTrace();
       
   133             failed++;
       
   134         } catch (IllegalArgumentException e) {
       
   135             // swallow
       
   136         }
       
   137     }
       
   138 }