From 9ddb655a88faa4533c5360384408c2a6dafee949 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 11 Nov 2019 14:24:11 +0000
Subject: [PATCH] KeyGenerator: Use new PrivateKey constructor to cache pubkey
 Clear cached public key on private key destroy()

---
 core/java/src/net/i2p/crypto/KeyGenerator.java | 11 ++++++-----
 core/java/src/net/i2p/data/PrivateKey.java     |  1 +
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/core/java/src/net/i2p/crypto/KeyGenerator.java b/core/java/src/net/i2p/crypto/KeyGenerator.java
index fa85bd456e..710cddabb3 100644
--- a/core/java/src/net/i2p/crypto/KeyGenerator.java
+++ b/core/java/src/net/i2p/crypto/KeyGenerator.java
@@ -159,15 +159,16 @@ public final class KeyGenerator {
         BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
 
         SimpleDataStructure[] keys = new SimpleDataStructure[2];
-        keys[0] = new PublicKey();
-        keys[1] = new PrivateKey();
 
         // bigInteger.toByteArray returns SIGNED integers, but since they'return positive,
         // signed two's complement is the same as unsigned
 
         try {
-            keys[0].setData(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES));
-            keys[1].setData(SigUtil.rectify(a, PrivateKey.KEYSIZE_BYTES));
+            PublicKey pub = new PublicKey(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES));
+            keys[0] = pub;
+            keys[1] = new PrivateKey(EncType.ELGAMAL_2048,
+                                     SigUtil.rectify(a, PrivateKey.KEYSIZE_BYTES),
+                                     pub);
         } catch (InvalidKeyException ike) {
             throw new IllegalArgumentException(ike);
         }
@@ -199,7 +200,7 @@ public final class KeyGenerator {
             byte[] bpub = new byte[32];
             Curve25519.eval(bpub, 0, bpriv, null);
             pub = new PublicKey(type, bpub);
-            priv = new PrivateKey(type, bpriv);
+            priv = new PrivateKey(type, bpriv, pub);
             break;
 
           default:
diff --git a/core/java/src/net/i2p/data/PrivateKey.java b/core/java/src/net/i2p/data/PrivateKey.java
index e3a3b60f41..41c544e172 100644
--- a/core/java/src/net/i2p/data/PrivateKey.java
+++ b/core/java/src/net/i2p/data/PrivateKey.java
@@ -123,6 +123,7 @@ public class PrivateKey extends SimpleDataStructure implements Destroyable {
             Arrays.fill(data, (byte) 0);
             SimpleByteCache.release(data);
         }
+        _pubKey = null;
     }
 
     /**
-- 
GitLab