CVSROOT: /cvsroot/classpath Module name: classpath Changes by: Raif S. Naffah <raif> 06/06/17 03:56:19
Modified files: . : ChangeLog gnu/javax/crypto/jce/cipher: CipherAdapter.java Log message: 2006-06-17 Raif S. Naffah <[EMAIL PROTECTED]> On behalf of Vivek Lakshmanan <[EMAIL PROTECTED]> * gnu/javax/crypto/jce/cipher/CipherAdapter.java (engineInit(int, Key, SecureRandom)): Seperate common initialization logic into engineInitHandler and reuse the code in engineInit(int, Key, AlgorithmSpec, SecureRandom). (engineInitHandler): New method. (engineInit(int, Key, AlgorithmParameterSpec, SecureRandom)): When param is null, use random or default information when possible. CVSWeb URLs: http://cvs.savannah.gnu.org/viewcvs/classpath/ChangeLog?cvsroot=classpath&r1=1.7857&r2=1.7858 http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/javax/crypto/jce/cipher/CipherAdapter.java?cvsroot=classpath&r1=1.3&r2=1.4 Patches: Index: ChangeLog =================================================================== RCS file: /cvsroot/classpath/classpath/ChangeLog,v retrieving revision 1.7857 retrieving revision 1.7858 diff -u -b -r1.7857 -r1.7858 --- ChangeLog 16 Jun 2006 20:46:54 -0000 1.7857 +++ ChangeLog 17 Jun 2006 03:56:18 -0000 1.7858 @@ -1,3 +1,14 @@ +2006-06-17 Raif S. Naffah <[EMAIL PROTECTED]> + + On behalf of Vivek Lakshmanan <[EMAIL PROTECTED]> + * gnu/javax/crypto/jce/cipher/CipherAdapter.java + (engineInit(int, Key, SecureRandom)): Seperate common initialization logic + into engineInitHandler and reuse the code in + engineInit(int, Key, AlgorithmSpec, SecureRandom). + (engineInitHandler): New method. + (engineInit(int, Key, AlgorithmParameterSpec, SecureRandom)): When param is + null, use random or default information when possible. + 2006-06-16 Francis Kung <[EMAIL PROTECTED]> * examples/gnu/classpath/examples/swing/Demo.java: Index: gnu/javax/crypto/jce/cipher/CipherAdapter.java =================================================================== RCS file: /cvsroot/classpath/classpath/gnu/javax/crypto/jce/cipher/CipherAdapter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -b -r1.3 -r1.4 --- gnu/javax/crypto/jce/cipher/CipherAdapter.java 4 Jun 2006 06:57:26 -0000 1.3 +++ gnu/javax/crypto/jce/cipher/CipherAdapter.java 17 Jun 2006 03:56:18 -0000 1.4 @@ -38,6 +38,7 @@ package gnu.javax.crypto.jce.cipher; +import gnu.java.security.Registry; import gnu.javax.crypto.cipher.IBlockCipher; import gnu.javax.crypto.cipher.CipherFactory; import gnu.javax.crypto.jce.spec.BlockCipherParameterSpec; @@ -246,6 +247,30 @@ protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException { + try + { + engineInit(opmode, key, (AlgorithmParameterSpec) null, random); + } + catch (InvalidAlgorithmParameterException e) + { + throw new InvalidKeyException(e.getMessage(), e); + } + } + + /** + * Executes initialization logic after all parameters have been handled by the + * engineInit()s. + * + * @param opmode the desired mode of operation for this instance. + * @param key the key material to use for initialization. + * @param random a source of randmoness to use if/when needed. + * @throws InvalidKeyException if <code>key</code> is invalid or the cipher + * needs extra parameters which can not be derived from + * <code>key</code>; e.g. an IV. + */ + private void engineInitHandler(int opmode, Key key, SecureRandom random) + throws InvalidKeyException + { switch (opmode) { case Cipher.ENCRYPT_MODE: @@ -280,11 +305,29 @@ { if (params == null) { + // All cipher modes require parameters (like an IV) except ECB. When + // these cant be derived from the given key then it must be generated + // randomly if in ENCRYPT or WRAP mode. Parameters that have defaults + // for our cipher must be set to these defaults. + if(! mode.name().toLowerCase().startsWith(Registry.ECB_MODE + "(")) + { + switch (opmode) + { + case Cipher.ENCRYPT_MODE: + case Cipher.WRAP_MODE: byte[] iv = new byte[blockLen]; random.nextBytes(iv); attributes.put(IMode.IV, iv); + break; + default: + throw new InvalidAlgorithmParameterException( + "Required algorithm parameters are missing for mode: " + + mode.name()); + } + } + // Add default for block length etc. blockLen = cipher.defaultBlockSize(); - attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(blockLen)); + attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, Integer.valueOf(blockLen)); keyLen = 0; } else if (params instanceof BlockCipherParameterSpec) @@ -304,7 +347,7 @@ attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(blockLen)); keyLen = 0; } - engineInit(opmode, key, random); + engineInitHandler(opmode, key, random); } protected void engineInit(int opmode, Key key, AlgorithmParameters params,