1 /*
   2  * Copyright (c) 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
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8012900
  27  * @library ../UTIL
  28  * @build TestUtil
  29  * @run main TestCICOWithGCMAndAAD
  30  * @summary Test CipherInputStream/OutputStream with AES GCM mode with AAD.
  31  * @author Valerie Peng
  32  */
  33 import java.io.*;
  34 import java.security.*;
  35 import java.util.*;
  36 import javax.crypto.*;
  37 
  38 public class TestCICOWithGCMAndAAD {
  39     public static void main(String[] args) throws Exception {
  40         //init Secret Key
  41         KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE");
  42         kg.init(128);
  43         SecretKey key = kg.generateKey();
  44 
  45         //Do initialization of the plainText
  46         byte[] plainText = new byte[700];
  47         Random rdm = new Random();
  48         rdm.nextBytes(plainText);
  49 
  50         byte[] aad = new byte[128];
  51         rdm.nextBytes(aad);
  52         byte[] aad2 = aad.clone();
  53         aad2[50]++;
  54 
  55         Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
  56         encCipher.init(Cipher.ENCRYPT_MODE, key);
  57         encCipher.updateAAD(aad);
  58         Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
  59         decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());
  60         decCipher.updateAAD(aad);
  61 
  62         byte[] recovered = test(encCipher, decCipher, plainText);
  63         if (!Arrays.equals(plainText, recovered)) {
  64             throw new Exception("sameAAD: diff check failed!");
  65         } else System.out.println("sameAAD: passed");
  66 
  67         encCipher.init(Cipher.ENCRYPT_MODE, key);
  68         encCipher.updateAAD(aad2);
  69         recovered = test(encCipher, decCipher, plainText);
  70         if (recovered != null && recovered.length != 0) {
  71             throw new Exception("diffAAD: no data should be returned!");
  72         } else System.out.println("diffAAD: passed");
  73    }
  74 
  75    private static byte[] test(Cipher encCipher, Cipher decCipher, byte[] plainText)
  76             throws Exception {
  77         //init cipher streams
  78         ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);
  79         CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);
  80         ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
  81         CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);
  82 
  83         //do test
  84         byte[] buffer = new byte[200];
  85         int len = ciInput.read(buffer);
  86         System.out.println("read " + len + " bytes from input buffer");
  87 
  88         while (len != -1) {
  89             ciOutput.write(buffer, 0, len);
  90             System.out.println("wite " + len + " bytes to output buffer");
  91             len = ciInput.read(buffer);
  92             if (len != -1) {
  93                 System.out.println("read " + len + " bytes from input buffer");
  94             } else {
  95                 System.out.println("finished reading");
  96             }
  97         }
  98 
  99         ciOutput.flush();
 100         ciInput.close();
 101         ciOutput.close();
 102 
 103         return baOutput.toByteArray();
 104     }
 105 }