1 /* 2 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.crypto.provider; 27 28 import java.security.InvalidKeyException; 29 import java.security.ProviderException; 30 31 /** 32 * This abstract class represents the core of all block ciphers. It allows to 33 * intialize the cipher and encrypt/decrypt single blocks. Larger quantities 34 * are handled by modes, which are subclasses of FeedbackCipher. 35 * 36 * @author Gigi Ankeny 37 * @author Jan Luehe 38 * 39 * 40 * @see AESCrypt 41 * @see DESCrypt 42 * @see DESedeCrypt 43 * @see BlowfishCrypt 44 * @see FeedbackCipher 45 */ 46 abstract class SymmetricCipher { 47 48 SymmetricCipher() { 49 // empty 50 } 51 52 /** 53 * Retrieves this cipher's block size. 54 * 55 * @return the block size of this cipher 56 */ 57 abstract int getBlockSize(); 58 59 /** 60 * Initializes the cipher in the specified mode with the given key. 61 * 62 * @param decrypting flag indicating encryption or decryption 63 * @param algorithm the algorithm name 64 * @param key the key 65 * 66 * @exception InvalidKeyException if the given key is inappropriate for 67 * initializing this cipher 68 */ 69 abstract void init(boolean decrypting, String algorithm, byte[] key) 70 throws InvalidKeyException; 71 72 /** 73 * Encrypt one cipher block. 74 * 75 * <p>The input <code>plain</code>, starting at <code>plainOffset</code> 76 * and ending at <code>(plainOffset+blockSize-1)</code>, is encrypted. 77 * The result is stored in <code>cipher</code>, starting at 78 * <code>cipherOffset</code>. 79 * 80 * @param plain the input buffer with the data to be encrypted 81 * @param plainOffset the offset in <code>plain</code> 82 * @param cipher the buffer for the encryption result 83 * @param cipherOffset the offset in <code>cipher</code> 84 */ 85 abstract void encryptBlock(byte[] plain, int plainOffset, 86 byte[] cipher, int cipherOffset); 87 88 void encryptBlocks(byte[] plain, int plainOffset, byte[] cipher, int cipherOffset, int len) { 89 int blockSize = getBlockSize(); 90 if ((len % blockSize) != 0) { 91 throw new ProviderException("Internal error in input buffering"); 92 } 93 for (int i = 0; i < len; i += blockSize) { 94 encryptBlock(plain, plainOffset + i, cipher, cipherOffset + i); 95 } 96 } 97 98 99 /** 100 * Decrypt one cipher block. 101 * 102 * <p>The input <code>cipher</code>, starting at <code>cipherOffset</code> 103 * and ending at <code>(cipherOffset+blockSize-1)</code>, is decrypted. 104 * The result is stored in <code>plain</code>, starting at 105 * <code>plainOffset</code>. 106 * 107 * @param cipher the input buffer with the data to be decrypted 108 * @param cipherOffset the offset in <code>cipher</code> 109 * @param plain the buffer for the decryption result 110 * @param plainOffset the offset in <code>plain</code> 111 */ 112 abstract void decryptBlock(byte[] cipher, int cipherOffset, 113 byte[] plain, int plainOffset); 114 115 void decryptBlocks(byte[] cipher, int cipherOffset, byte[] plain, int plainOffset, int len) { 116 int blockSize = getBlockSize(); 117 if ((len % blockSize) != 0) { 118 throw new ProviderException("Internal error in input buffering"); 119 } 120 for (int i = 0; i < len; i += blockSize) { 121 decryptBlock(cipher, cipherOffset + i, plain, plainOffset + i); 122 } 123 } 124 125 }