jdk/test/com/sun/crypto/provider/Cipher/TestCipher.java
author rhalade
Fri, 28 Aug 2015 13:35:54 -0700
changeset 32411 d29d9a2a6f1f
child 38858 f2ddc41670a8
permissions -rw-r--r--
8048601: Tests for JCE crypto ciphers (part 1) Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32411
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     1
/*
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     4
 *
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     8
 *
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    13
 * accompanied this code).
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    14
 *
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    18
 *
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    21
 * questions.
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    22
 */
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    23
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    24
import static java.lang.System.out;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    25
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    26
import java.security.InvalidAlgorithmParameterException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    27
import java.security.InvalidKeyException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    28
import java.security.NoSuchAlgorithmException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    29
import java.security.NoSuchProviderException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    31
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    32
import javax.crypto.BadPaddingException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    33
import javax.crypto.Cipher;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    34
import javax.crypto.IllegalBlockSizeException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    35
import javax.crypto.KeyGenerator;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    36
import javax.crypto.NoSuchPaddingException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    37
import javax.crypto.SecretKey;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    38
import javax.crypto.ShortBufferException;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    39
import javax.crypto.spec.IvParameterSpec;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    40
import javax.crypto.spec.SecretKeySpec;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    41
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    42
/**
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    43
 * This is a abstract class used to test various ciphers
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    44
 */
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    45
public abstract class TestCipher {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    46
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    47
    private final String SUNJCE = "SunJCE";
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    48
    private final String ALGORITHM;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    49
    private final String[] MODES;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    50
    private final String[] PADDINGS;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    51
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    52
    /* Used to test cipher with different key strengths
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    53
       Key size tested is increment of KEYCUTTER from MINIMUM_KEY_SIZE to
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    54
       maximum allowed keysize.
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    55
       DES/DESede/Blowfish work with currently selected key sizes.
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    56
    */
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    57
    private final int variousKeySize;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    58
    private final int KEYCUTTER = 8;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    59
    private final int MINIMUM_KEY_SIZE = 32;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    60
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    61
    // Used to assert that Encryption/Decryption works with same buffer
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    62
    // TEXT_LEN is multiple of blocks in order to work against ciphers w/ NoPadding
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    63
    private final int TEXT_LEN = 800;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    64
    private final int ENC_OFFSET = 6;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    65
    private final int STORAGE_OFFSET = 3;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    66
    private final int PAD_BYTES = 16;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    67
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    68
    private final byte[] IV;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    69
    private final byte[] INPUT_TEXT;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    70
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    71
    TestCipher(String algo, String[] modes, String[] paddings,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    72
            boolean keyStrength) throws NoSuchAlgorithmException {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    73
        ALGORITHM = algo;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    74
        MODES = modes;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    75
        PADDINGS = paddings;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    76
        this.variousKeySize
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    77
                = keyStrength ? Cipher.getMaxAllowedKeyLength(ALGORITHM) : 0;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    78
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    79
        IV = generateBytes(8);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    80
        INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    81
    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    82
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    83
    TestCipher(String algo, String[] modes, String[] paddings) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    84
        ALGORITHM = algo;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    85
        MODES = modes;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    86
        PADDINGS = paddings;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    87
        variousKeySize = 0;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    88
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    89
        IV = generateBytes(8);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    90
        INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    91
    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    92
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    93
    private static byte[] generateBytes(int length) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    94
        byte[] bytes = new byte[length];
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    95
        for (int i = 0; i < length; i++) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    96
            bytes[i] = (byte) (i & 0xff);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    97
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    98
        return bytes;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
    99
    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   100
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   101
    private boolean isKeyStrenthSupported() {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   102
        return (variousKeySize != 0);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   103
    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   104
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   105
    public void runAll() throws InvalidKeyException,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   106
            NoSuchPaddingException, InvalidAlgorithmParameterException,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   107
            ShortBufferException, IllegalBlockSizeException,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   108
            BadPaddingException, NoSuchAlgorithmException,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   109
            NoSuchProviderException {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   110
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   111
        for (String mode : MODES) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   112
            for (String padding : PADDINGS) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   113
                if (!isKeyStrenthSupported()) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   114
                    runTest(mode, padding, 0);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   115
                } else {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   116
                    int keySize = variousKeySize;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   117
                    while (keySize >= MINIMUM_KEY_SIZE) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   118
                        out.println("With Key Strength: " + keySize);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   119
                        runTest(mode, padding, keySize);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   120
                        keySize -= KEYCUTTER;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   121
                    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   122
                }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   123
            }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   124
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   125
    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   126
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   127
    private void runTest(String mo, String pad, int keySize)
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   128
            throws NoSuchPaddingException, BadPaddingException,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   129
            ShortBufferException, IllegalBlockSizeException,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   130
            InvalidAlgorithmParameterException, InvalidKeyException,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   131
            NoSuchAlgorithmException, NoSuchProviderException {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   132
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   133
        String TRANSFORMATION = ALGORITHM + "/" + mo + "/" + pad;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   134
        out.println("Testing: " + TRANSFORMATION);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   135
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   136
        // Initialization
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   137
        Cipher ci = Cipher.getInstance(TRANSFORMATION, SUNJCE);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   138
        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM, SUNJCE);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   139
        if (keySize != 0) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   140
            kg.init(keySize);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   141
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   142
        SecretKey key = kg.generateKey();
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   143
        SecretKeySpec skeySpec = new SecretKeySpec(key.getEncoded(), ALGORITHM);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   144
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   145
        AlgorithmParameterSpec aps = new IvParameterSpec(IV);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   146
        if (mo.equalsIgnoreCase("ECB")) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   147
            ci.init(Cipher.ENCRYPT_MODE, key);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   148
        } else {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   149
            ci.init(Cipher.ENCRYPT_MODE, key, aps);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   150
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   151
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   152
        // Encryption
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   153
        int PAD_LEN = 0;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   154
        if (pad.equalsIgnoreCase("PKCS5Padding")) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   155
            // Need to consider pad bytes
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   156
            PAD_LEN = 8;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   157
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   158
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   159
        byte[] plainText = INPUT_TEXT.clone();
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   160
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   161
        // Generate cipher and save to separate buffer
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   162
        byte[] cipherText = ci.doFinal(INPUT_TEXT, ENC_OFFSET, TEXT_LEN);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   163
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   164
        // Generate cipher and save to same buffer
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   165
        int offset = ci.update(
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   166
                INPUT_TEXT, ENC_OFFSET, TEXT_LEN, INPUT_TEXT, STORAGE_OFFSET);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   167
        ci.doFinal(INPUT_TEXT, offset + STORAGE_OFFSET);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   168
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   169
        if (!equalsBlock(
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   170
                INPUT_TEXT, STORAGE_OFFSET, cipherText, 0, cipherText.length)) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   171
            throw new RuntimeException(
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   172
                    "Different ciphers generated with same buffer");
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   173
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   174
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   175
        // Decryption
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   176
        if (mo.equalsIgnoreCase("ECB")) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   177
            ci.init(Cipher.DECRYPT_MODE, skeySpec);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   178
        } else {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   179
            ci.init(Cipher.DECRYPT_MODE, skeySpec, aps);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   180
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   181
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   182
        // Recover text from cipher and save to separate buffer
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   183
        byte[] recoveredText = ci.doFinal(cipherText, 0, cipherText.length);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   184
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   185
        if (!equalsBlock(
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   186
                plainText, ENC_OFFSET, recoveredText, 0,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   187
                recoveredText.length)) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   188
            throw new RuntimeException(
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   189
                    "Recovered text not same as plain text");
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   190
        } else {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   191
            out.println("Recovered and plain text are same");
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   192
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   193
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   194
        // Recover text from cipher and save to same buffer
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   195
        ci.update(INPUT_TEXT, STORAGE_OFFSET, TEXT_LEN + PAD_LEN, INPUT_TEXT,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   196
                ENC_OFFSET);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   197
        ci.doFinal(INPUT_TEXT, ENC_OFFSET);
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   198
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   199
        if (!equalsBlock(
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   200
                plainText, ENC_OFFSET, recoveredText, 0,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   201
                recoveredText.length)) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   202
            throw new RuntimeException(
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   203
                    "Recovered text not same as plain text with same buffer");
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   204
        } else {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   205
            out.println("Recovered and plain text are same with same buffer");
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   206
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   207
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   208
        out.println("Test Passed.");
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   209
    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   210
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   211
    private static boolean equalsBlock(byte[] b1, int off1, byte[] b2, int off2,
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   212
            int len) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   213
        for (int i = off1, j = off2, k = 0; k < len; i++, j++, k++) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   214
            if (b1[i] != b2[j]) {
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   215
                return false;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   216
            }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   217
        }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   218
        return true;
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   219
    }
d29d9a2a6f1f 8048601: Tests for JCE crypto ciphers (part 1)
rhalade
parents:
diff changeset
   220
}