From: <gel...@mx...> - 2003-02-04 18:30:26
|
gelderen 03/02/04 13:38:31 Modified: jce/src/cryptix.jce.provider.cipher Mode.java ModeCBC.java ModeCFB.java ModeOFB.java Log: 2) ClassCastException from CoreInit() method of Mode instance when using RC2 cipher algorithm. Submitted-By: Kevin Dana <kd...@ag...> Revision Changes Path 1.18 +52 -18 projects/jce/src/cryptix.jce.provider.cipher/Mode.java Index: Mode.java =================================================================== RCS file: /home/cryptix-cvs/cvsroot/projects/jce/src/cryptix.jce.provider.cipher/Mode.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- Mode.java 6 Aug 2001 18:06:50 -0000 1.17 +++ Mode.java 4 Feb 2003 18:38:31 -0000 1.18 @@ -1,4 +1,4 @@ -/* $Id: Mode.java,v 1.17 2001/08/06 18:06:50 edwin Exp $ +/* $Id: Mode.java,v 1.18 2003/02/04 18:38:31 gelderen Exp $ * * Copyright (C) 1995-2000 The Cryptix Foundation Limited. * All rights reserved. @@ -16,6 +16,10 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.RC2ParameterSpec; +import javax.crypto.spec.RC5ParameterSpec; + /** * <p> @@ -45,9 +49,10 @@ * * @author Jeroen C. van Gelderen (gel...@cr...) * @author Paul Waserbrot (pw...@cr...) - * @version $Revision: 1.17 $ + * @author Kevin Dana, Agorics Inc. (Agorics mod: 16164) + * @version $Revision: 1.18 $ */ -abstract class Mode +/*package*/ abstract class Mode { /** Underlying block cipher */ protected final BlockCipher cipher; @@ -65,13 +70,13 @@ protected int bufCount; - Mode(BlockCipher cipher) { + /*package*/ Mode(BlockCipher cipher) { this.cipher = cipher; CIPHER_BLOCK_SIZE = cipher.coreGetBlockSize(); } - static Mode getInstance(String mode, BlockCipher cipher) + /*package*/ static Mode getInstance(String mode, BlockCipher cipher) throws NoSuchAlgorithmException { try { @@ -101,8 +106,9 @@ } - void init(boolean decrypt, Key key, AlgorithmParameterSpec params, - SecureRandom random) + /*package*/ void init(boolean decrypt, Key key, + AlgorithmParameterSpec params, + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { this.decrypt = decrypt; @@ -110,44 +116,72 @@ } - final byte[] getIV() { + /*package*/ final byte[] getIV() { return coreGetIV(); } - final AlgorithmParameterSpec getParamSpec() { + /*package*/ final AlgorithmParameterSpec getParamSpec() { return coreGetParamSpec(); } - final int getOutputSize(int inputLen) { + + /*package*/ final int getOutputSize(int inputLen) { return coreGetOutputSize(inputLen); } - final int getBlockSize() { + /*package*/ final int getBlockSize() { return CIPHER_BLOCK_SIZE; } - final int update(byte[] input, int inputOffset, int inputLen, - byte[] output, int outputOffset) { + /*package*/ final int update(byte[] input, int inputOffset, int inputLen, + byte[] output, int outputOffset) { return coreUpdate(input, inputOffset, inputLen, output, outputOffset); } - final int getBufSize() { + /*package*/ final int getBufSize() { return bufCount; - } - + } - protected byte [] generateIV() { + protected byte [] generateIV() { byte [] b = new byte[CIPHER_BLOCK_SIZE]; SecureRandom sr = new SecureRandom(); sr.nextBytes(b); return b; - } + } + + protected final byte[] extractIV(AlgorithmParameterSpec params) + throws InvalidAlgorithmParameterException + { + /* + * -- AlgorithmParameterSpec is a blank interface + * and the Java JCE does not provide a common superinterface + * for AlgorithmParameterSpec subclasses that provide + * initialization vector (IV) byte arrays, + * so test for each known type that has a "getIV()" method + * + * -- The current API creates a combinatorial explosion. The JCE + * API should be amended with a composite AlgorithmParameterSpec + * class so that the getIV functionality doesn't have to be + * replicated in each ParameterSpec. + */ + if (params instanceof IvParameterSpec) { + return ((IvParameterSpec)params).getIV(); + } else if (params instanceof RC2ParameterSpec) { + return ((RC2ParameterSpec)params).getIV(); + } else if (params instanceof RC5ParameterSpec) { + return ((RC5ParameterSpec)params).getIV(); + } else { + throw new InvalidAlgorithmParameterException( + "Don't know how to get an IV from a " + + params.getClass().getName()); + } + } // Abstract methods //............................................................................ 1.13 +7 -10 projects/jce/src/cryptix.jce.provider.cipher/ModeCBC.java Index: ModeCBC.java =================================================================== RCS file: /home/cryptix-cvs/cvsroot/projects/jce/src/cryptix.jce.provider.cipher/ModeCBC.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- ModeCBC.java 6 Aug 2001 18:06:50 -0000 1.12 +++ ModeCBC.java 4 Feb 2003 18:38:31 -0000 1.13 @@ -1,4 +1,4 @@ -/* $Id: ModeCBC.java,v 1.12 2001/08/06 18:06:50 edwin Exp $ +/* $Id: ModeCBC.java,v 1.13 2003/02/04 18:38:31 gelderen Exp $ * * Copyright (C) 1995-2000 The Cryptix Foundation Limited. * All rights reserved. @@ -22,13 +22,14 @@ * * @author Jeroen C. van Gelderen (gel...@cr...) * @author Paul Waserbrot (pw...@cr...) - * @version $Revision: 1.12 $ + * @author Kevin Dana, Agorics Inc. (Agorics mod: 16164) + * @version $Revision: 1.13 $ */ final class ModeCBC extends Mode { /** buffers incomplete blocks */ - private final byte[] buf; // we count the buffer with bufCount from Mode.java + private final byte[] buf; // we count the buf with bufCount from Mode.java /** previous ciphertext block (during decryption only) */ @@ -54,21 +55,17 @@ final void coreInit(boolean decrypt, Key key, AlgorithmParameterSpec params, - SecureRandom random) + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { cipher.coreInit(key, decrypt); + // set IV + IV = extractIV(params); if(decrypt) { - // set IV - IvParameterSpec iv = (IvParameterSpec)params; - IV = iv.getIV(); System.arraycopy(IV, 0, prevBlock, 0, CIPHER_BLOCK_SIZE); bufCount = 0; } else { - // set IV - IvParameterSpec iv = (IvParameterSpec)params; - IV = iv.getIV(); System.arraycopy(IV, 0, buf, 0, CIPHER_BLOCK_SIZE); bufCount = 0; } 1.5 +5 -5 projects/jce/src/cryptix.jce.provider.cipher/ModeCFB.java Index: ModeCFB.java =================================================================== RCS file: /home/cryptix-cvs/cvsroot/projects/jce/src/cryptix.jce.provider.cipher/ModeCFB.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- ModeCFB.java 6 Aug 2001 18:06:50 -0000 1.4 +++ ModeCFB.java 4 Feb 2003 18:38:31 -0000 1.5 @@ -1,4 +1,4 @@ -/* $Id: ModeCFB.java,v 1.4 2001/08/06 18:06:50 edwin Exp $ +/* $Id: ModeCFB.java,v 1.5 2003/02/04 18:38:31 gelderen Exp $ * * Copyright (C) 1995-2000 The Cryptix Foundation Limited. * All rights reserved. @@ -21,7 +21,8 @@ /** * @author Jeroen C. van Gelderen (gel...@cr...) - * @version $Revision: 1.4 $ + * @author Kevin Dana, Agorics Inc. (Agorics mod: 16164) + * @version $Revision: 1.5 $ */ /*package*/ class ModeCFB extends Mode { @@ -157,7 +158,7 @@ void coreInit(boolean decrypt, Key key, AlgorithmParameterSpec params, - SecureRandom random) + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { // always use cipher in encrypt mode @@ -166,8 +167,7 @@ this.decrypt = decrypt; // set IV - IvParameterSpec iv = (IvParameterSpec)params; - iVec = iv.getIV(); + iVec = extractIV(params); int iVecLen = iVec.length; if(iVecLen != CIPHER_BLOCK_SIZE) throw new InvalidAlgorithmParameterException( 1.13 +4 -4 projects/jce/src/cryptix.jce.provider.cipher/ModeOFB.java Index: ModeOFB.java =================================================================== RCS file: /home/cryptix-cvs/cvsroot/projects/jce/src/cryptix.jce.provider.cipher/ModeOFB.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- ModeOFB.java 6 Aug 2001 18:06:50 -0000 1.12 +++ ModeOFB.java 4 Feb 2003 18:38:31 -0000 1.13 @@ -1,4 +1,4 @@ -/* $Id: ModeOFB.java,v 1.12 2001/08/06 18:06:50 edwin Exp $ +/* $Id: ModeOFB.java,v 1.13 2003/02/04 18:38:31 gelderen Exp $ * * Copyright (C) 1995-2000 The Cryptix Foundation Limited. * All rights reserved. @@ -23,7 +23,8 @@ * * @author Jeroen C. van Gelderen (gel...@cr...) * @author Paul Waserbrot (pw...@cr...) - * @version $Revision: 1.12 $ + * @author Kevin Dana, Agorics Inc. (Agorics mod: 16164) + * @version $Revision: 1.13 $ */ final class ModeOFB extends Mode @@ -60,8 +61,7 @@ cipher.coreInit(key, false); // set IV and crypt once to generate initial key stream bytes - IvParameterSpec iv = (IvParameterSpec)params; - IV = iv.getIV(); + IV = extractIV(params); System.arraycopy(IV, 0, keyStreamBuf, 0, CIPHER_BLOCK_SIZE); cipher.coreCrypt(keyStreamBuf, 0, keyStreamBuf, 0); keyStreamBufOffset = 0; |