jdk/src/share/classes/com/sun/crypto/provider/CipherFeedback.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
child 20752 f0f0acea9113
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 represents ciphers in cipher-feedback (CFB) mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>This mode is implemented independently of a particular cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Ciphers to which this mode should apply (e.g., DES) must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <i>plugged-in</i> using the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>NOTE: This class does not deal with buffering or padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Gigi Ankeny
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
final class CipherFeedback extends FeedbackCipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * encrypt/decrypt output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final byte[] k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * register value, initialized with iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final byte[] register;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * number of bytes for each stream unit, defaults to the blocksize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * of the embedded cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // variables for save/restore calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private byte[] registerSave = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    CipherFeedback(SymmetricCipher embeddedCipher, int numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(embeddedCipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (numBytes > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            numBytes = blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.numBytes = numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        k = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        register = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Gets the name of this feedback mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return the string <code>CFB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    String getFeedback() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return "CFB";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Initializes the cipher in the specified mode with the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * and iv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param decrypting flag indicating encryption or decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param algorithm the algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param iv the iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if ((key == null) || (iv == null) || (iv.length != blockSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new InvalidKeyException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.iv = iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // always encrypt mode for embedded cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        embeddedCipher.init(false, algorithm, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Resets the iv to its original value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * This is used when doFinal is called in the Cipher class, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * cipher can be reused (with its original iv).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.arraycopy(iv, 0, register, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Save the current content of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    void save() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (registerSave == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            registerSave = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.arraycopy(register, 0, registerSave, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Restores the content of this cipher to the previous saved one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    void restore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        System.arraycopy(registerSave, 0, register, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Performs encryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>The input plain text <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>plainOffset</code> and ending at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <code>(plainOffset + len - 1)</code>, is encrypted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * The result is stored in <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <code>cipherOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <p>It is the application's responsibility to make sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <code>plainLen</code> is a multiple of the stream unit size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <code>numBytes</code>, as any excess bytes are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <p>It is also the application's responsibility to make sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>init</code> has been called before this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * (This check is omitted here, to avoid double checking.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param plain the buffer with the input 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 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 encrypt(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
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        int i, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        len = blockSize - numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int loopCount = plainLen / numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int oddBytes = plainLen % numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            for (; loopCount > 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                 plainOffset += numBytes, cipherOffset += numBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                 loopCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                for (i = 0; i < blockSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    register[i] = cipher[i+cipherOffset] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        (byte)(k[i] ^ plain[i+plainOffset]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (oddBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                for (i=0; i<oddBytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    register[i] = cipher[i+cipherOffset] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        (byte)(k[i] ^ plain[i+plainOffset]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            for (; loopCount > 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                 plainOffset += numBytes, cipherOffset += numBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 loopCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                System.arraycopy(register, numBytes, register, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                for (i=0; i<numBytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    register[i+len] = cipher[i+cipherOffset] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        (byte)(k[i] ^ plain[i+plainOffset]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (oddBytes != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                System.arraycopy(register, numBytes, register, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                for (i=0; i<oddBytes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    register[i+len] = cipher[i+cipherOffset] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        (byte)(k[i] ^ plain[i+plainOffset]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Performs decryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p>The input cipher text <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <code>cipherOffset</code> and ending at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <code>(cipherOffset + len - 1)</code>, is decrypted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * The result is stored in <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>plainOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>It is the application's responsibility to make sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <code>cipherLen</code> is a multiple of the stream unit size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <code>numBytes</code>, as any excess bytes are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>It is also the application's responsibility to make sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <code>init</code> has been called before this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * (This check is omitted here, to avoid double checking.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param cipher the buffer with the input data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param cipherOffset the offset in <code>cipherOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param cipherLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param plain the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        byte[] plain, int plainOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int i, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        len = blockSize - numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int loopCount = cipherLen / numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int oddBytes = cipherLen % numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            for (; loopCount > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                 plainOffset += numBytes, cipherOffset += numBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                 loopCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                for (i = 0; i < blockSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    register[i] = cipher[i+cipherOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    plain[i+plainOffset]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        = (byte)(cipher[i+cipherOffset] ^ k[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (oddBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                for (i=0; i<oddBytes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    register[i] = cipher[i+cipherOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    plain[i+plainOffset]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        = (byte)(cipher[i+cipherOffset] ^ k[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            for (; loopCount > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                 plainOffset += numBytes, cipherOffset += numBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                 loopCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                System.arraycopy(register, numBytes, register, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                for (i=0; i<numBytes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    register[i+len] = cipher[i+cipherOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    plain[i+plainOffset]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        = (byte)(cipher[i+cipherOffset] ^ k[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (oddBytes != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                embeddedCipher.encryptBlock(register, 0, k, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                System.arraycopy(register, numBytes, register, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                for (i=0; i<oddBytes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    register[i+len] = cipher[i+cipherOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    plain[i+plainOffset]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        = (byte)(cipher[i+cipherOffset] ^ k[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
}