diff --git a/router/java/src/com/southernstorm/noise/protocol/Noise.java b/router/java/src/com/southernstorm/noise/protocol/Noise.java index 595604a7e2cfc06b73864ab6fa44d1f1bef982a6..8b38662f3e27656edfd970c0c83da04ea8ea10a6 100644 --- a/router/java/src/com/southernstorm/noise/protocol/Noise.java +++ b/router/java/src/com/southernstorm/noise/protocol/Noise.java @@ -30,6 +30,9 @@ import java.util.Arrays; import javax.crypto.BadPaddingException; +import net.i2p.crypto.SHA256Generator; + + /** * Utility functions for the Noise protocol library. */ @@ -72,15 +75,8 @@ public final class Noise { */ public static MessageDigest createHash(String name) throws NoSuchAlgorithmException { - // Look for a JCA/JCE provider first and if that doesn't work, - // use the fallback implementations in this library instead. - // The only algorithm that is required to be implemented by a - // JDK is "SHA-256", although "SHA-512" is fairly common as well. if (name.equals("SHA256")) { - try { - return MessageDigest.getInstance("SHA-256"); - } catch (NoSuchAlgorithmException e) { - } + return SHA256Generator.getInstance().acquire(); } throw new NoSuchAlgorithmException("Unknown Noise hash algorithm name: " + name); } @@ -88,6 +84,18 @@ public final class Noise { // The rest of this class consists of internal utility functions // that are not part of the public API. + /** + * I2P Release a hash object back to the pool. + * + * @since 0.9.66 + */ + static void releaseHash(MessageDigest hash) + { + if (hash.getAlgorithm().equals("SHA-256")) { + SHA256Generator.getInstance().release(hash); + } + } + /** * Destroys the contents of a byte array. * diff --git a/router/java/src/com/southernstorm/noise/protocol/SymmetricState.java b/router/java/src/com/southernstorm/noise/protocol/SymmetricState.java index 8d3eb3695e9432af62fcdaf3b6d01c9025a3d19f..218f65a5385679af91b1dc3c6e7d32c30d3f915a 100644 --- a/router/java/src/com/southernstorm/noise/protocol/SymmetricState.java +++ b/router/java/src/com/southernstorm/noise/protocol/SymmetricState.java @@ -62,6 +62,7 @@ class SymmetricState implements Destroyable, Cloneable { md.digest(INIT_HASH_N, 0, 32); md.update(INIT_CK_XK_SSU2, 0, 32); md.digest(INIT_HASH_XK_SSU2, 0, 32); + Noise.releaseHash(md); } catch (Exception e) { throw new IllegalStateException(e); } @@ -87,6 +88,7 @@ class SymmetricState implements Destroyable, Cloneable { MessageDigest hash = Noise.createHash("SHA256"); hash.update(protocolNameBytes, 0, protocolNameBytes.length); hash.digest(rv, 0, 32); + Noise.releaseHash(hash); } catch (Exception e) { throw new IllegalStateException(e); } @@ -403,6 +405,7 @@ class SymmetricState implements Destroyable, Cloneable { public void destroy() { cipher.destroy(); hash.reset(); + Noise.releaseHash(hash); Noise.destroy(ck); Noise.destroy(h); Noise.destroy(prev_h);