1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.crypto.tests.javax.crypto; 19 20import java.security.AlgorithmParameters; 21import java.security.Key; 22import java.security.SecureRandom; 23import java.util.Arrays; 24 25import javax.crypto.Cipher; 26import javax.crypto.KeyGenerator; 27 28public class Cipher_Impl1Test extends junit.framework.TestCase { 29 30 static Key cipherKey; 31 final static String algorithm = "DESede"; 32 final static int keyLen = 168; 33 34 static { 35 try { 36 KeyGenerator kg = KeyGenerator.getInstance(algorithm); 37 kg.init(keyLen, new SecureRandom()); 38 cipherKey = kg.generateKey(); 39 } catch (Exception e) { 40 fail("No key " + e); 41 } 42 } 43 44 45 /** 46 * @tests javax.crypto.Cipher#getIV() 47 * @tests javax.crypto.Cipher#init(int, java.security.Key, 48 * java.security.AlgorithmParameters) 49 */ 50 public void test_getIV() throws Exception { 51 /* 52 * If this test is changed, implement the following: 53 * test_initILjava_security_KeyLjava_security_AlgorithmParameters() 54 */ 55 56 SecureRandom sr = new SecureRandom(); 57 Cipher cipher = null; 58 59 byte[] iv = new byte[8]; 60 sr.nextBytes(iv); 61 AlgorithmParameters ap = AlgorithmParameters.getInstance(algorithm); 62 ap.init(iv, "RAW"); 63 64 cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding"); 65 cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ap); 66 67 byte[] cipherIV = cipher.getIV(); 68 69 assertTrue("IVs differ", Arrays.equals(cipherIV, iv)); 70 } 71 72 /** 73 * @tests javax.crypto.Cipher#getParameters() 74 * @tests javax.crypto.Cipher#init(int, java.security.Key, 75 * java.security.AlgorithmParameters, java.security.SecureRandom) 76 */ 77 public void test_getParameters() throws Exception { 78 79 /* 80 * If this test is changed, implement the following: 81 * test_initILjava_security_KeyLjava_security_AlgorithmParametersLjava_security_SecureRandom() 82 */ 83 84 SecureRandom sr = new SecureRandom(); 85 Cipher cipher = null; 86 87 byte[] apEncoding = null; 88 89 byte[] iv = new byte[8]; 90 sr.nextBytes(iv); 91 92 AlgorithmParameters ap = AlgorithmParameters.getInstance("DESede"); 93 ap.init(iv, "RAW"); 94 apEncoding = ap.getEncoded("ASN.1"); 95 96 cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding"); 97 cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ap, sr); 98 99 byte[] cipherParmsEnc = cipher.getParameters().getEncoded("ASN.1"); 100 assertTrue("Parameters differ", Arrays.equals(apEncoding, 101 cipherParmsEnc)); 102 } 103} 104