From 62649a63438ca3fbfd253dbcaf03afe2e4c1c936 Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 22 May 2019 11:04:17 +0000 Subject: [PATCH] Crypto: Add X25519 DH method, prep for enc. ls2 auth. --- .../src/net/i2p/crypto/x25519/X25519DH.java | 34 +++++++++++++++++++ .../src/net/i2p/crypto/x25519/package.html | 7 ++++ 2 files changed, 41 insertions(+) create mode 100644 core/java/src/net/i2p/crypto/x25519/X25519DH.java create mode 100644 core/java/src/net/i2p/crypto/x25519/package.html diff --git a/core/java/src/net/i2p/crypto/x25519/X25519DH.java b/core/java/src/net/i2p/crypto/x25519/X25519DH.java new file mode 100644 index 000000000..3436032b4 --- /dev/null +++ b/core/java/src/net/i2p/crypto/x25519/X25519DH.java @@ -0,0 +1,34 @@ +package net.i2p.crypto.x25519; + +import com.southernstorm.noise.crypto.x25519.Curve25519; + +import net.i2p.crypto.EncType; +import net.i2p.data.PrivateKey; +import net.i2p.data.PublicKey; +import net.i2p.data.SessionKey; + +/** + * DH wrapper around Noise's Curve25519 with I2P types. + * + * @since 0.9.41 + */ +public class X25519DH { + + private static final EncType TYPE = EncType.ECIES_X25519; + + private X25519DH() {} + + /** + * DH + * + * @return ECIES_X25519 + * @throws IllegalArgumentException if not ECIES_X25519 + */ + public static SessionKey dh(PrivateKey priv, PublicKey pub) { + if (priv.getType() != TYPE || pub.getType() != TYPE) + throw new IllegalArgumentException(); + byte[] rv = new byte[32]; + Curve25519.eval(rv, 0, priv.getData(), pub.getData()); + return new SessionKey(rv); + } +} diff --git a/core/java/src/net/i2p/crypto/x25519/package.html b/core/java/src/net/i2p/crypto/x25519/package.html new file mode 100644 index 000000000..286c605d8 --- /dev/null +++ b/core/java/src/net/i2p/crypto/x25519/package.html @@ -0,0 +1,7 @@ + +

+ DH for X25519. +

+ Since 0.9.41. +

+