jdk/src/share/classes/com/sun/crypto/provider/DESedeCrypt.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class implements the Triple DES algorithm (DES encryption, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * DES decryption, followed by DES encryption) on a byte array of size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <code>DES_BLOCK_SIZE</code>. Each DES operation has its own key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Gigi Ankeny
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see DESConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see DESCipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
final class DESedeCrypt extends DESCrypt implements DESConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * the expanded key used in encrypt/decrypt/encrypt phase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private byte[] key1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private byte[] key2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private byte[] key3 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte[] buf1, buf2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    DESedeCrypt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        buf1 = new byte[DES_BLOCK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        buf2 = new byte[DES_BLOCK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void init(boolean decrypting, String algorithm, byte[] keys)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (!algorithm.equalsIgnoreCase("DESede")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    && !algorithm.equalsIgnoreCase("TripleDES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                ("Wrong algorithm: DESede or TripleDES required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (keys.length != DES_BLOCK_SIZE * 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new InvalidKeyException("Wrong key size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        byte[] keybuf = new byte[DES_BLOCK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // retrieve the first key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        key1 = new byte[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.arraycopy(keys, 0, keybuf, 0, DES_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        expandKey(keybuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.arraycopy(expandedKey, 0, key1, 0, 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // check if the third key is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (keyEquals(keybuf, 0, keys, DES_BLOCK_SIZE*2, DES_BLOCK_SIZE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            key3 = key1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            key3 = new byte[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.arraycopy(keys, DES_BLOCK_SIZE*2, keybuf, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                             DES_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            expandKey(keybuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.arraycopy(expandedKey, 0, key3, 0, 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // retrieve the second key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        key2 = new byte[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.arraycopy(keys, DES_BLOCK_SIZE, keybuf, 0, DES_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        expandKey(keybuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.arraycopy(expandedKey, 0, key2, 0, 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Performs encryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>The input plain text <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>plainOffset</code> and ending at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>(plainOffset + blockSize - 1)</code>, is encrypted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The result is stored in <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <code>cipherOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param plain the buffer with the input data to be encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param cipher the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param cipherOffset the offset in <code>cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void encryptBlock(byte[] plain, int plainOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                 byte[] cipher, int cipherOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        expandedKey = key1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        cipherBlock(plain, plainOffset, buf1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        expandedKey = key2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        decrypting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        cipherBlock(buf1, 0, buf2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        expandedKey = key3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        cipherBlock(buf2, 0, cipher, cipherOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Performs decryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p>The input cipher text <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>cipherOffset</code> and ending at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>(cipherOffset + blockSize - 1)</code>, is decrypted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * The result is stored in <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <code>plainOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param cipher the buffer with the input data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param cipherOffset the offset in <code>cipherOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param plain the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void decryptBlock(byte[] cipher, int cipherOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                 byte[] plain, int plainOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        expandedKey = key3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        decrypting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        cipherBlock(cipher, cipherOffset, buf1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        expandedKey = key2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        decrypting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        cipherBlock(buf1, 0, buf2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        expandedKey = key1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        decrypting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        cipherBlock(buf2, 0, plain, plainOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private boolean keyEquals(byte[] key1, int off1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                              byte[] key2, int off2, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        for (int i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (key1[i+off1] != key2[i+off2])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}