0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.crypto.provider;
0N/A
0N/Aimport java.security.InvalidKeyException;
0N/A
0N/A/**
0N/A * This class implements the Triple DES algorithm (DES encryption, followed by
0N/A * DES decryption, followed by DES encryption) on a byte array of size
0N/A * <code>DES_BLOCK_SIZE</code>. Each DES operation has its own key.
0N/A *
0N/A * @author Gigi Ankeny
0N/A * @author Jan Luehe
0N/A *
0N/A *
0N/A * @see DESConstants
0N/A * @see DESCipher
0N/A */
0N/A
0N/Afinal class DESedeCrypt extends DESCrypt implements DESConstants {
0N/A
0N/A /*
0N/A * the expanded key used in encrypt/decrypt/encrypt phase
0N/A */
0N/A private byte[] key1 = null;
0N/A private byte[] key2 = null;
0N/A private byte[] key3 = null;
0N/A private byte[] buf1, buf2;
0N/A
0N/A /*
0N/A * constructor
0N/A */
0N/A DESedeCrypt() {
0N/A buf1 = new byte[DES_BLOCK_SIZE];
0N/A buf2 = new byte[DES_BLOCK_SIZE];
0N/A }
0N/A
0N/A void init(boolean decrypting, String algorithm, byte[] keys)
0N/A throws InvalidKeyException {
0N/A if (!algorithm.equalsIgnoreCase("DESede")
0N/A && !algorithm.equalsIgnoreCase("TripleDES")) {
0N/A throw new InvalidKeyException
0N/A ("Wrong algorithm: DESede or TripleDES required");
0N/A }
0N/A if (keys.length != DES_BLOCK_SIZE * 3) {
0N/A throw new InvalidKeyException("Wrong key size");
0N/A }
0N/A
0N/A byte[] keybuf = new byte[DES_BLOCK_SIZE];
0N/A
0N/A // retrieve the first key
0N/A key1 = new byte[128];
0N/A System.arraycopy(keys, 0, keybuf, 0, DES_BLOCK_SIZE);
0N/A expandKey(keybuf);
0N/A System.arraycopy(expandedKey, 0, key1, 0, 128);
0N/A
0N/A // check if the third key is the same
0N/A if (keyEquals(keybuf, 0, keys, DES_BLOCK_SIZE*2, DES_BLOCK_SIZE)) {
0N/A key3 = key1;
0N/A } else {
0N/A key3 = new byte[128];
0N/A System.arraycopy(keys, DES_BLOCK_SIZE*2, keybuf, 0,
0N/A DES_BLOCK_SIZE);
0N/A expandKey(keybuf);
0N/A System.arraycopy(expandedKey, 0, key3, 0, 128);
0N/A }
0N/A
0N/A // retrieve the second key
0N/A key2 = new byte[128];
0N/A System.arraycopy(keys, DES_BLOCK_SIZE, keybuf, 0, DES_BLOCK_SIZE);
0N/A expandKey(keybuf);
0N/A System.arraycopy(expandedKey, 0, key2, 0, 128);
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Performs encryption operation.
0N/A *
0N/A * <p>The input plain text <code>plain</code>, starting at
0N/A * <code>plainOffset</code> and ending at
0N/A * <code>(plainOffset + blockSize - 1)</code>, is encrypted.
0N/A * The result is stored in <code>cipher</code>, starting at
0N/A * <code>cipherOffset</code>.
0N/A *
0N/A * @param plain the buffer with the input data to be encrypted
0N/A * @param plainOffset the offset in <code>plain</code>
0N/A * @param cipher the buffer for the result
0N/A * @param cipherOffset the offset in <code>cipher</code>
0N/A */
0N/A void encryptBlock(byte[] plain, int plainOffset,
0N/A byte[] cipher, int cipherOffset)
0N/A {
0N/A expandedKey = key1;
0N/A decrypting = false;
0N/A cipherBlock(plain, plainOffset, buf1, 0);
0N/A
0N/A expandedKey = key2;
0N/A decrypting = true;
0N/A cipherBlock(buf1, 0, buf2, 0);
0N/A
0N/A expandedKey = key3;
0N/A decrypting = false;
0N/A cipherBlock(buf2, 0, cipher, cipherOffset);
0N/A }
0N/A
0N/A /**
0N/A * Performs decryption operation.
0N/A *
0N/A * <p>The input cipher text <code>cipher</code>, starting at
0N/A * <code>cipherOffset</code> and ending at
0N/A * <code>(cipherOffset + blockSize - 1)</code>, is decrypted.
0N/A * The result is stored in <code>plain</code>, starting at
0N/A * <code>plainOffset</code>.
0N/A *
0N/A * @param cipher the buffer with the input data to be decrypted
0N/A * @param cipherOffset the offset in <code>cipherOffset</code>
0N/A * @param plain the buffer for the result
0N/A * @param plainOffset the offset in <code>plain</code>
0N/A */
0N/A void decryptBlock(byte[] cipher, int cipherOffset,
0N/A byte[] plain, int plainOffset)
0N/A {
0N/A expandedKey = key3;
0N/A decrypting = true;
0N/A cipherBlock(cipher, cipherOffset, buf1, 0);
0N/A
0N/A expandedKey = key2;
0N/A decrypting = false;
0N/A cipherBlock(buf1, 0, buf2, 0);
0N/A
0N/A expandedKey = key1;
0N/A decrypting = true;
0N/A cipherBlock(buf2, 0, plain, plainOffset);
0N/A }
0N/A
0N/A private boolean keyEquals(byte[] key1, int off1,
0N/A byte[] key2, int off2, int len) {
0N/A
0N/A for (int i=0; i<len; i++) {
0N/A if (key1[i+off1] != key2[i+off2])
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A}