changeset 95:e9bcd67287e8

2007-07-18 Lillian Angel <langel@redhat.com> * jce/gnu/javax/crypto/jce/GnuCrypto.java: Added PBEWithMD5AndDES and PBE providers. * jce/gnu/javax/crypto/jce/cipher/CipherAdapter.java: (engineUpdate): Fixed blockCount and buffer size. (engineUpdate): Fixed blockCount. * jce/gnu/javax/crypto/key/GnuPBEKey.java (getFormat): Implemented. (getEncoded): Implemented. * jce/gnu/javax/crypto/jce/PBESecretKeyFactory.java: New file. * jce/gnu/javax/crypto/jce/cipher/PBE.java: New file.
author Lillian Angel <langel@redhat.com>
date Wed, 18 Jul 2007 13:58:33 -0400
parents 88d973cc80fa
children fea4b10e38f2
files ChangeLog jce/gnu/javax/crypto/jce/GnuCrypto.java jce/gnu/javax/crypto/jce/PBESecretKeyFactory.java jce/gnu/javax/crypto/jce/cipher/CipherAdapter.java jce/gnu/javax/crypto/jce/cipher/PBE.java jce/gnu/javax/crypto/key/GnuPBEKey.java
diffstat 6 files changed, 233 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Jul 17 15:19:45 2007 -0400
+++ b/ChangeLog	Wed Jul 18 13:58:33 2007 -0400
@@ -1,3 +1,16 @@
+2007-07-18  Lillian Angel  <langel@redhat.com>
+
+	* jce/gnu/javax/crypto/jce/GnuCrypto.java: Added PBEWithMD5AndDES and
+	PBE providers.
+	* jce/gnu/javax/crypto/jce/cipher/CipherAdapter.java: 
+	(engineUpdate): Fixed blockCount and buffer size.
+	(engineUpdate): Fixed blockCount.
+	* jce/gnu/javax/crypto/key/GnuPBEKey.java
+	(getFormat): Implemented.
+	(getEncoded): Implemented.
+	* jce/gnu/javax/crypto/jce/PBESecretKeyFactory.java: New file.
+	* jce/gnu/javax/crypto/jce/cipher/PBE.java: New file.
+
 2007-07-17  Kyle Galloway  <kgallowa@redhat.com>
 
 	* acinclude.m4: Added FIND_FREETYPE macro to check for Freetype libs
--- a/jce/gnu/javax/crypto/jce/GnuCrypto.java	Tue Jul 17 15:19:45 2007 -0400
+++ b/jce/gnu/javax/crypto/jce/GnuCrypto.java	Wed Jul 18 13:58:33 2007 -0400
@@ -316,6 +316,8 @@
             gnu.javax.crypto.jce.cipher.PBES2.HMacWhirlpool.TripleDES.class.getName());
         put("Cipher.PBEWithHMacWhirlpoolAndTwofish",
             gnu.javax.crypto.jce.cipher.PBES2.HMacWhirlpool.Twofish.class.getName());
+        put("Cipher.PBE",
+        		gnu.javax.crypto.jce.cipher.PBE.MD5.DES.class.getName());
 
         // Key Wrapping Algorithm cipher
         put("Cipher." + Registry.AES128_KWA,
@@ -349,6 +351,9 @@
         put("SecretKeyFactory.PBKDF2WithHMacWhirlpool",
             gnu.javax.crypto.jce.PBKDF2SecretKeyFactory.HMacWhirlpool.class.getName());
 
+        put("SecretKeyFactory.PBEWithMD5AndDES",
+        		gnu.javax.crypto.jce.PBESecretKeyFactory.class.getName());
+        
         // Simple SecretKeyFactory implementations.
         put("SecretKeyFactory.Anubis",
             gnu.javax.crypto.jce.key.AnubisSecretKeyFactoryImpl.class.getName());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jce/gnu/javax/crypto/jce/PBESecretKeyFactory.java	Wed Jul 18 13:58:33 2007 -0400
@@ -0,0 +1,84 @@
+/* PBESecretKeyFactory.java -- 
+   Copyright (C) 2007  Free Software Foundation, Inc.
+
+This file is a part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or (at
+your option) any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+USA
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version.  */
+
+
+package gnu.javax.crypto.jce;
+
+import gnu.javax.crypto.key.GnuPBEKey;
+
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+
+import javax.crypto.SecretKey;
+import javax.crypto.SecretKeyFactorySpi;
+import javax.crypto.spec.PBEKeySpec;
+
+
+public class PBESecretKeyFactory
+    extends SecretKeyFactorySpi
+{
+  protected String name;
+
+  public PBESecretKeyFactory()
+  {
+    super();
+  }
+  
+  protected PBESecretKeyFactory(String name)
+  {
+    this.name = name;
+  }
+
+  protected SecretKey engineGenerateSecret(KeySpec spec)
+      throws InvalidKeySpecException
+  {
+    if (! (spec instanceof PBEKeySpec))
+      throw new InvalidKeySpecException("not a PBEKeySpec");
+    return new GnuPBEKey((PBEKeySpec) spec);
+  }
+
+  protected KeySpec engineGetKeySpec(SecretKey key, Class clazz)
+      throws InvalidKeySpecException
+  {
+    throw new InvalidKeySpecException("not supported");
+  }
+
+  protected SecretKey engineTranslateKey(SecretKey key)
+  {
+    return null;
+  }
+}
--- a/jce/gnu/javax/crypto/jce/cipher/CipherAdapter.java	Tue Jul 17 15:19:45 2007 -0400
+++ b/jce/gnu/javax/crypto/jce/cipher/CipherAdapter.java	Wed Jul 18 13:58:33 2007 -0400
@@ -378,7 +378,7 @@
     if (inLen == 0) // nothing to process
       return new byte[0];
     final int blockSize = mode.currentBlockSize();
-    int blockCount = (partLen + inLen) / blockSize;
+    int blockCount = (int) (Math.ceil((double) (partLen + inLen) / (double) blockSize));
 
     // always keep data for unpadding in padded decryption mode;
     // might even be a complete block
@@ -386,7 +386,7 @@
         && ((Integer) attributes.get(IMode.STATE)).intValue() == IMode.DECRYPTION
         && (partLen + inLen) % blockSize == 0)
       blockCount--;
-
+    
     final byte[] out = new byte[blockCount * blockSize];
     try
       {
@@ -405,15 +405,13 @@
     if (inLen == 0) // nothing to process
       return 0;
     final int blockSize = mode.currentBlockSize();
-    int blockCount = (partLen + inLen) / blockSize;
-
+    int blockCount = (int) (Math.ceil((double) (partLen + inLen) / (double) blockSize));
     // always keep data for unpadding in padded decryption mode;
     // might even be a complete block
     if (pad != null
         && ((Integer) attributes.get(IMode.STATE)).intValue() == IMode.DECRYPTION
         && (partLen + inLen) % blockSize == 0)
       blockCount--;
-
     final int result = blockCount * blockSize;
     if (result > out.length - outOff)
       throw new ShortBufferException();
@@ -426,7 +424,10 @@
     final byte[] buf;
     // we have enough bytes for at least 1 block
     if (partLen == 0) // if no cached bytes use input
-      buf = in;
+    {
+      buf = new byte[result];
+      System.arraycopy(in, 0, buf, 0, in.length);
+    }
     else // prefix input with cached bytes
       {
         buf = new byte[partLen + inLen];
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jce/gnu/javax/crypto/jce/cipher/PBE.java	Wed Jul 18 13:58:33 2007 -0400
@@ -0,0 +1,121 @@
+/* PBE.java -- 
+   Copyright (C) 2007  Free Software Foundation, Inc.
+
+This file is a part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or (at
+your option) any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+USA
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version.  */
+
+
+package gnu.javax.crypto.jce.cipher;
+
+import gnu.javax.crypto.key.GnuPBEKey;
+
+import java.security.AlgorithmParameters;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.Key;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.spec.AlgorithmParameterSpec;
+
+import javax.crypto.Cipher;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.interfaces.PBEKey;
+
+/**
+ */
+public abstract class PBE
+    extends CipherAdapter
+{
+  Cipher cipher;
+  MessageDigest hash;
+  
+  protected PBE(String cipher, String hash)
+  {
+	super(cipher);
+	try
+	{
+		this.cipher = Cipher.getInstance(cipher);
+		this.hash = MessageDigest.getInstance(hash);
+	}
+	catch (NoSuchAlgorithmException e)
+	{
+	}
+	catch (NoSuchPaddingException e)
+	{
+	}
+  } 
+
+  protected void engineInit(int opmode, Key key, SecureRandom random)
+      throws InvalidKeyException
+  {
+    if (! (key instanceof GnuPBEKey))
+      throw new InvalidKeyException("not a GNU PBE key");
+    super.engineInit(opmode, (GnuPBEKey) key, random);
+  }
+
+  protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
+                            SecureRandom random) throws InvalidKeyException,
+      InvalidAlgorithmParameterException
+  {
+    if (! (key instanceof GnuPBEKey))
+        throw new InvalidKeyException("not a GNU PBE key");
+      super.engineInit(opmode, (GnuPBEKey) key, params, random);
+  }
+
+  protected void engineInit(int opmode, Key key, AlgorithmParameters params,
+                            SecureRandom random) throws InvalidKeyException,
+      InvalidAlgorithmParameterException
+  {
+    if (! (key instanceof GnuPBEKey))
+        throw new InvalidKeyException("not a GNU PBE key");
+      super.engineInit(opmode, (GnuPBEKey) key, params, random);
+  }
+  
+  public static class MD5 extends PBE
+  {
+	public MD5(String cipher) 
+    { 
+      super(cipher, "MD5"); 
+    }
+
+	public static class DES extends MD5
+    {
+      public DES() 
+      { 
+    	super("DES"); 
+      }
+    }
+  } 
+}
--- a/jce/gnu/javax/crypto/key/GnuPBEKey.java	Tue Jul 17 15:19:45 2007 -0400
+++ b/jce/gnu/javax/crypto/key/GnuPBEKey.java	Wed Jul 18 13:58:33 2007 -0400
@@ -85,11 +85,12 @@
 
   public String getFormat ()
   {
-    return "NONE"; // FIXME?
+    return "RAW";
   }
 
   public byte[] getEncoded ()
   {
-    return null; // FIXME?
+	String pass = new String(getPassword());
+	return pass.getBytes();
   }
 }