Hi,

This patch fixes the Diffie Hellman implementation. The problem was that it would not use the incoming intermediate key as-is, and would instead perform an emphemeral key generation each time.

I've limited the implementation to two-party only; I'm not quite sure how to do multi-party DH correctly, but two-party is what everyone uses.

Committed,

2005-10-02  Casey Marshall  <[EMAIL PROTECTED]>

    * gnu/javax/crypto/DiffieHellmanImpl.java (random): removed.
    (<init>): removed 'random' initialization.
    (engineDoPhase): use params from the incoming key; generate the
    intermediate result from the incoming key, not a random one; only
    support one-phase DH, for now.
    (engineInit): removed 'random' initialization.

Index: gnu/javax/crypto/DiffieHellmanImpl.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/javax/crypto/DiffieHellmanImpl.java,v
retrieving revision 1.2
diff -u -B -b -r1.2 DiffieHellmanImpl.java
--- gnu/javax/crypto/DiffieHellmanImpl.java     21 Sep 2005 20:55:40 -0000      
1.2
+++ gnu/javax/crypto/DiffieHellmanImpl.java     2 Oct 2005 19:45:02 -0000
@@ -67,9 +67,6 @@
   /** The private key being used for this agreement. */
   private DHPrivateKey key;
 
-  /** The random-number generator used to generate secrets. */
-  private SecureRandom random;
-
   /** The current result. */
   private BigInteger result;
 
@@ -80,7 +77,6 @@
   public DiffieHellmanImpl ()
   {
     key = null;
-    random = null;
     result = null;
     last_phase_done = false;
   }
@@ -99,23 +95,20 @@
       throw new InvalidKeyException ("expecting 
javax.crypto.interfaces.DHPublicKey");
     DHPublicKey pub = (DHPublicKey) incoming;
     DHParameterSpec s1 = key.getParams();
-    DHParameterSpec s2 = key.getParams();
+    DHParameterSpec s2 = pub.getParams();
     if (!s1.getG().equals (s2.getG())
         || !s1.getP().equals (s2.getP())
         || s1.getL() != s2.getL())
       throw new InvalidKeyException ("supplied key is not compatible");
 
-    BigInteger randval = new BigInteger (s1.getL(), random);
-    BigInteger out = s1.getG().modPow (key.getX(), s1.getP());
-    if (result == null)
-      result = s1.getG();
-    result = result.modPow (pub.getY(), s1.getP());
+    result = pub.getY().modPow (key.getX(), s1.getP());
     if (lastPhase)
       {
        last_phase_done = true;
        return null;
       }
-    return new GnuDHPublicKey (s1, out, null);
+
+    throw new IllegalArgumentException ("only supports two-party Diffie 
Hellman");
   }
 
   protected byte[] engineGenerateSecret ()
@@ -153,10 +146,6 @@
     if (!(key instanceof DHPrivateKey))
       throw new InvalidKeyException ("not a 
javax.crypto.interfaces.DHPrivateKey");
     this.key = (DHPrivateKey) key;
-    if (random != null)
-      this.random = random;
-    else if (this.random == null)
-      this.random = new SecureRandom();
     result = null;
     last_phase_done = false;
   }
_______________________________________________
Classpath-patches mailing list
Classpath-patches@gnu.org
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to