jdk/src/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
changeset 20752 f0f0acea9113
parent 10336 0bb1999251f8
child 21837 0c41fa97176a
equal deleted inserted replaced
20751:56898cefcda9 20752:f0f0acea9113
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   133      * @param plain the buffer with the input data to be encrypted
   133      * @param plain the buffer with the input data to be encrypted
   134      * @param plainOffset the offset in <code>plain</code>
   134      * @param plainOffset the offset in <code>plain</code>
   135      * @param plainLen the length of the input data
   135      * @param plainLen the length of the input data
   136      * @param cipher the buffer for the result
   136      * @param cipher the buffer for the result
   137      * @param cipherOffset the offset in <code>cipher</code>
   137      * @param cipherOffset the offset in <code>cipher</code>
   138      */
   138      * @return the length of the encrypted data
   139     void encrypt(byte[] plain, int plainOffset, int plainLen,
   139      */
   140                  byte[] cipher, int cipherOffset)
   140     int encrypt(byte[] plain, int plainOffset, int plainLen,
       
   141                 byte[] cipher, int cipherOffset)
   141     {
   142     {
   142         int i;
   143         int i;
   143         int endIndex = plainOffset + plainLen;
   144         int endIndex = plainOffset + plainLen;
   144 
   145 
   145         for (; plainOffset < endIndex;
   146         for (; plainOffset < endIndex;
   148                 k[i] = (byte)(plain[i+plainOffset] ^ r[i]);
   149                 k[i] = (byte)(plain[i+plainOffset] ^ r[i]);
   149             }
   150             }
   150             embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
   151             embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
   151             System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
   152             System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
   152         }
   153         }
       
   154         return plainLen;
   153     }
   155     }
   154 
   156 
   155     /**
   157     /**
   156      * Performs decryption operation.
   158      * Performs decryption operation.
   157      *
   159      *
   172      * @param cipher the buffer with the input data to be decrypted
   174      * @param cipher the buffer with the input data to be decrypted
   173      * @param cipherOffset the offset in <code>cipherOffset</code>
   175      * @param cipherOffset the offset in <code>cipherOffset</code>
   174      * @param cipherLen the length of the input data
   176      * @param cipherLen the length of the input data
   175      * @param plain the buffer for the result
   177      * @param plain the buffer for the result
   176      * @param plainOffset the offset in <code>plain</code>
   178      * @param plainOffset the offset in <code>plain</code>
       
   179      * @return the length of the decrypted data
   177      *
   180      *
   178      * @exception IllegalBlockSizeException if input data whose length does
   181      * @exception IllegalBlockSizeException if input data whose length does
   179      * not correspond to the embedded cipher's block size is passed to the
   182      * not correspond to the embedded cipher's block size is passed to the
   180      * embedded cipher
   183      * embedded cipher
   181      */
   184      */
   182     void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
   185     int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
   183                  byte[] plain, int plainOffset)
   186                 byte[] plain, int plainOffset)
   184     {
   187     {
   185         int i;
   188         int i;
   186         byte[] cipherOrig=null;
   189         byte[] cipherOrig=null;
   187         int endIndex = cipherOffset + cipherLen;
   190         int endIndex = cipherOffset + cipherLen;
   188 
   191 
   193             // This is necessary because in this constellation, a
   196             // This is necessary because in this constellation, a
   194             // ciphertext block (or parts of it) will be overridden by
   197             // ciphertext block (or parts of it) will be overridden by
   195             // the plaintext result.
   198             // the plaintext result.
   196             cipherOrig = cipher.clone();
   199             cipherOrig = cipher.clone();
   197         }
   200         }
   198 
       
   199         for (; cipherOffset < endIndex;
   201         for (; cipherOffset < endIndex;
   200              cipherOffset += blockSize, plainOffset += blockSize) {
   202              cipherOffset += blockSize, plainOffset += blockSize) {
   201             embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);
   203             embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);
   202             for (i = 0; i < blockSize; i++) {
   204             for (i = 0; i < blockSize; i++) {
   203                 plain[i+plainOffset] = (byte)(k[i] ^ r[i]);
   205                 plain[i+plainOffset] = (byte)(k[i] ^ r[i]);
   206                 System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
   208                 System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
   207             } else {
   209             } else {
   208                 System.arraycopy(cipherOrig, cipherOffset, r, 0, blockSize);
   210                 System.arraycopy(cipherOrig, cipherOffset, r, 0, blockSize);
   209             }
   211             }
   210         }
   212         }
       
   213         return cipherLen;
   211     }
   214     }
   212 }
   215 }