4589N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Cipher implementation class. This class currently supports 0N/A * DES, DESede, AES, ARCFOUR, and Blowfish. 3645N/A * This class is designed to support ECB, CBC, CTR with NoPadding 3645N/A * and ECB, CBC with PKCS5Padding. It will use its own padding impl 3645N/A * if the native mechanism does not support padding. 3645N/A * Note that PKCS#11 currently only supports ECB, CBC, and CTR. 3645N/A * There are no provisions for other modes such as CFB, OFB, and PCBC. 0N/A * @author Andreas Sterbenz 0N/A // mode constant for ECB mode 0N/A // mode constant for CBC mode 3645N/A // mode constant for CTR mode 0N/A // padding constant for NoPadding 0N/A // padding constant for PKCS5Padding 149N/A // ENC: format the specified buffer with padding bytes and return the 149N/A // actual padding length 149N/A // DEC: return the length of trailing padding bytes given the specified 149N/A (
"PKCS#5 padding not supported with stream ciphers");
149N/A // sanity check padding bytes 0N/A // name of the key algorithm, e.g. DES instead of algorithm DES/CBC/... 0N/A // associated session, if any 0N/A // key, if init() was called 0N/A // flag indicating whether an operation is initialized 0N/A // falg indicating encrypt or decrypt mode 0N/A // mode, one of MODE_* above (MODE_ECB for stream ciphers) 0N/A // block size, 0 for stream ciphers 0N/A // padding type, on of PAD_* above (PAD_NONE for stream ciphers) 149N/A // when the padding is requested but unsupported by the native mechanism, 149N/A // we use the following to do padding and necessary data buffering. 149N/A // padding object which generate padding and unpad the decrypted data 149N/A // buffer for holding back the block which contains padding bytes 3645N/A // original IV, if in MODE_CBC or MODE_CTR 149N/A // number of bytes buffered internally by the native mechanism and padBuffer 149N/A // if we do the padding 0N/A }
else {
// DES, DESede, Blowfish 149N/A // Disallow change of mode for now since currently it's explicitly 149N/A // defined in transformation strings 0N/A (
"CBC mode not supported with stream ciphers");
3645N/A (
"PKCS#5 padding not supported with CTR mode");
149N/A // no native padding support; use our own padding impl 0N/A // NoSuchAlgorithmException, NoSuchProviderException 0N/A // InvalidParameterSpecException 0N/A (
"Only IvParameterSpec supported");
0N/A (
"Could not decode IV", e);
0N/A // actual init() implementation 149N/A (
"IV not used with stream ciphers");
149N/A (
"IV not used in ECB mode");
3645N/A }
else {
// MODE_CBC or MODE_CTR 3645N/A "IV must be specified for decryption in CBC mode" :
3645N/A "IV must be specified for decryption in CTR mode");
0N/A // generate random IV 149N/A (
"IV length must match block size");
0N/A // cancel operation by finishing it 3332N/A // release session when initialization failed 0N/A // if update(inLen) is called, how big does the output buffer have to be? 149N/A // minus the number of bytes in the last incomplete block. 0N/A // if doFinal(inLen) is called, how big does the output buffer have to be? 149N/A // add the number of bytes to make the last block complete. 3332N/A // reset the states to the pre-initialized values 149N/A // convert since the output length is calculated by updateLength() 149N/A // convert since the output length is calculated by doFinalLength() 149N/A // NSS throws up when called with data not in multiple 149N/A // of blocks. Try to work around this by holding the 149N/A // extra data in padBuffer. 149N/A // update 'padBuffer' if using our own padding impl. 149N/A // NSS throws up when called with data not in multiple 149N/A // of blocks. Try to work around this by holding the 149N/A // extra data in padBuffer. 149N/A // update 'padBuffer' if using our own padding impl. 149N/A // Reset input buffer to its original position for 0N/A // XXX key unwrapping