jdk/src/share/classes/com/sun/crypto/provider/FeedbackCipher.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
child 15008 6a494f8ba5b5
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
import javax.crypto.IllegalBlockSizeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class represents a block cipher in one of its modes. It wraps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * a SymmetricCipher maintaining the mode state and providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * the capability to encrypt amounts of data larger than a single block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see ElectronicCodeBook
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see CipherBlockChaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see CipherFeedback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see OutputFeedback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see PCBC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
abstract class FeedbackCipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // the embedded block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    final SymmetricCipher embeddedCipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // the block size of the embedded block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    final int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // the initialization vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    byte[] iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    FeedbackCipher(SymmetricCipher embeddedCipher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.embeddedCipher = embeddedCipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        blockSize = embeddedCipher.getBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    final SymmetricCipher getEmbeddedCipher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return embeddedCipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Gets the block size of the embedded cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @return the block size of the embedded cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    final int getBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Gets the name of the feedback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return the name of the feedback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    abstract String getFeedback();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Save the current content of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    abstract void save();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Restores the content of this cipher to the previous saved one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    abstract void restore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Initializes the cipher in the specified mode with the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * and iv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param decrypting flag indicating encryption or decryption mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param algorithm the algorithm name (never null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param key the key (never null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param iv the iv (either null or blockSize bytes long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    abstract void init(boolean decrypting, String algorithm, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                       byte[] iv) throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Gets the initialization vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return the initialization vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    final byte[] getIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Resets the iv to its original value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * This is used when doFinal is called in the Cipher class, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * cipher can be reused (with its original iv).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    abstract void reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Performs encryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>The input <code>plain</code>, starting at <code>plainOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * and ending at <code>(plainOffset+plainLen-1)</code>, is encrypted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The result is stored in <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <code>cipherOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p>The subclass that implements Cipher should ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>init</code> has been called before this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param plain the input buffer with the data to be encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param plainLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param cipher the buffer for the encryption result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param cipherOffset the offset in <code>cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    abstract void encrypt(byte[] plain, int plainOffset, int plainLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                          byte[] cipher, int cipherOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Performs encryption operation for the last time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p>NOTE: For cipher feedback modes which does not perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * special handling for the last few blocks, this is essentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * the same as <code>encrypt(...)</code>. Given most modes do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * not do special handling, the default impl for this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * to simply call <code>encrypt(...)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param plain the input buffer with the data to be encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param plainLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param cipher the buffer for the encryption result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param cipherOffset the offset in <code>cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     void encryptFinal(byte[] plain, int plainOffset, int plainLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                       byte[] cipher, int cipherOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         throws IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Performs decryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <p>The input <code>cipher</code>, starting at <code>cipherOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * and ending at <code>(cipherOffset+cipherLen-1)</code>, is decrypted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * The result is stored in <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <code>plainOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>The subclass that implements Cipher should ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <code>init</code> has been called before this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param cipher the input buffer with the data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param cipherOffset the offset in <code>cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param cipherLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param plain the buffer for the decryption result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    abstract void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                          byte[] plain, int plainOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Performs decryption operation for the last time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>NOTE: For cipher feedback modes which does not perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * special handling for the last few blocks, this is essentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the same as <code>encrypt(...)</code>. Given most modes do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * not do special handling, the default impl for this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * to simply call <code>decrypt(...)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param cipher the input buffer with the data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param cipherOffset the offset in <code>cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param cipherLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param plain the buffer for the decryption result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     void decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                       byte[] plain, int plainOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         throws IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}