I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Unverified Commit 8b9f61b1 authored by zzz's avatar zzz
Browse files

Crypto: Use pooled SHA256 instances in Noise lib

parent b2e80fce
No related branches found
No related tags found
No related merge requests found
...@@ -30,6 +30,9 @@ import java.util.Arrays; ...@@ -30,6 +30,9 @@ import java.util.Arrays;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import net.i2p.crypto.SHA256Generator;
/** /**
* Utility functions for the Noise protocol library. * Utility functions for the Noise protocol library.
*/ */
...@@ -72,15 +75,8 @@ public final class Noise { ...@@ -72,15 +75,8 @@ public final class Noise {
*/ */
public static MessageDigest createHash(String name) throws NoSuchAlgorithmException 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")) { if (name.equals("SHA256")) {
try { return SHA256Generator.getInstance().acquire();
return MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
}
} }
throw new NoSuchAlgorithmException("Unknown Noise hash algorithm name: " + name); throw new NoSuchAlgorithmException("Unknown Noise hash algorithm name: " + name);
} }
...@@ -88,6 +84,18 @@ public final class Noise { ...@@ -88,6 +84,18 @@ public final class Noise {
// The rest of this class consists of internal utility functions // The rest of this class consists of internal utility functions
// that are not part of the public API. // 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. * Destroys the contents of a byte array.
* *
......
...@@ -62,6 +62,7 @@ class SymmetricState implements Destroyable, Cloneable { ...@@ -62,6 +62,7 @@ class SymmetricState implements Destroyable, Cloneable {
md.digest(INIT_HASH_N, 0, 32); md.digest(INIT_HASH_N, 0, 32);
md.update(INIT_CK_XK_SSU2, 0, 32); md.update(INIT_CK_XK_SSU2, 0, 32);
md.digest(INIT_HASH_XK_SSU2, 0, 32); md.digest(INIT_HASH_XK_SSU2, 0, 32);
Noise.releaseHash(md);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
...@@ -87,6 +88,7 @@ class SymmetricState implements Destroyable, Cloneable { ...@@ -87,6 +88,7 @@ class SymmetricState implements Destroyable, Cloneable {
MessageDigest hash = Noise.createHash("SHA256"); MessageDigest hash = Noise.createHash("SHA256");
hash.update(protocolNameBytes, 0, protocolNameBytes.length); hash.update(protocolNameBytes, 0, protocolNameBytes.length);
hash.digest(rv, 0, 32); hash.digest(rv, 0, 32);
Noise.releaseHash(hash);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
...@@ -403,6 +405,7 @@ class SymmetricState implements Destroyable, Cloneable { ...@@ -403,6 +405,7 @@ class SymmetricState implements Destroyable, Cloneable {
public void destroy() { public void destroy() {
cipher.destroy(); cipher.destroy();
hash.reset(); hash.reset();
Noise.releaseHash(hash);
Noise.destroy(ck); Noise.destroy(ck);
Noise.destroy(h); Noise.destroy(h);
Noise.destroy(prev_h); Noise.destroy(prev_h);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment