Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto

# Windows scripts must keep CRLF; shell scripts must keep LF.
*.cmd text eol=crlf
*.bat text eol=crlf
mvnw text eol=lf
*.sh text eol=lf
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
# Keeps the SHA-pinned actions in .github/workflows current (Dependabot rewrites both the
# commit SHA and the "# vX.Y.Z" comment).
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
# Only propose releases that have been public for two weeks.
cooldown:
default-days: 14
groups:
github-actions:
patterns: ["*"]
144 changes: 144 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
name: Build, test and publish

on:
push:
branches: [master]
tags: ['v[0-9]+.[0-9]+.[0-9]+']
pull_request:

permissions:
contents: read

env:
RUST_VERSION: 1.100.0
CARGO_TERM_COLOR: always

jobs:
native:
name: native ${{ matrix.prefix }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
# Linux: cross-linked with zig against glibc 2.17.
- { prefix: linux-x86-64, os: ubuntu-24.04, target: x86_64-unknown-linux-gnu, glibc: '2.17', lib: libcurve25519.so }
- { prefix: linux-aarch64, os: ubuntu-24.04, target: aarch64-unknown-linux-gnu, glibc: '2.17', lib: libcurve25519.so }
- { prefix: linux-x86, os: ubuntu-24.04, target: i686-unknown-linux-gnu, glibc: '2.17', lib: libcurve25519.so }
- { prefix: darwin-aarch64, os: macos-15, target: aarch64-apple-darwin, macos: '11.0', lib: libcurve25519.dylib }
- { prefix: darwin-x86-64, os: macos-15, target: x86_64-apple-darwin, macos: '10.13', lib: libcurve25519.dylib }
- { prefix: win32-x86-64, os: windows-2025, target: x86_64-pc-windows-msvc, lib: curve25519.dll }
- { prefix: win32-x86, os: windows-2025, target: i686-pc-windows-msvc, lib: curve25519.dll }
- { prefix: win32-aarch64, os: windows-11-arm, target: aarch64-pc-windows-msvc, lib: curve25519.dll }
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Install toolchain (Linux, via mise)
if: matrix.glibc
uses: jdx/mise-action@b44d12e7d0d8ee7461bb893e1b1993500433185b # v5.2.7
with:
install_args: rust zig aqua:rust-cross/cargo-zigbuild

- name: Install toolchain (macOS / Windows)
if: ${{ !matrix.glibc }}
shell: bash
run: |
rustup toolchain install "$RUST_VERSION" --profile minimal
rustup default "$RUST_VERSION"

- name: Build
working-directory: native
shell: bash
env:
MACOSX_DEPLOYMENT_TARGET: ${{ matrix.macos }}
run: |
rustup target add ${{ matrix.target }}
if [ -n "${{ matrix.glibc }}" ]; then
cargo zigbuild --locked --release --target ${{ matrix.target }}.${{ matrix.glibc }}
else
cargo build --locked --release --target ${{ matrix.target }}
fi
mkdir -p ../dist/${{ matrix.prefix }}
cp target/${{ matrix.target }}/release/${{ matrix.lib }} ../dist/${{ matrix.prefix }}/

- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: native-${{ matrix.prefix }}
path: dist
if-no-files-found: error

rust-checks:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Install Rust
run: |
rustup toolchain install "$RUST_VERSION" --profile minimal --component clippy
rustup default "$RUST_VERSION"
- name: Test and lint
working-directory: native
run: |
cargo test --locked --release
cargo clippy --locked --release -- -D warnings

test:
name: test ${{ matrix.os }} java ${{ matrix.java }} ${{ matrix.arch }}
needs: native
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-24.04, ubuntu-24.04-arm, macos-15, macos-15-intel, windows-2025, windows-11-arm]
java: ['17', '25']
arch: ['']
include:
- { os: windows-2025, java: '17', arch: x86 }
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: native-*
merge-multiple: true
path: src/main/resources
- uses: actions/setup-java@77dd5b2ce225ea11469928ac317850245a74ae78 # v6.2.0
with:
# Temurin publishes no Windows on Arm JDKs; Microsoft's OpenJDK build does (17 and 25).
distribution: ${{ matrix.os == 'windows-11-arm' && 'microsoft' || 'temurin' }}
java-version: ${{ matrix.java }}
architecture: ${{ matrix.arch || null }}
cache: maven
# The output includes the hardware backend picked at runtime, e.g. "curve=avx2 sha512=avx2".
- run: ./mvnw -B -ntp -Prequire-natives verify
shell: bash

publish:
needs: [test, rust-checks]
if: github.event_name == 'push'
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: native-*
merge-multiple: true
path: src/main/resources
- uses: actions/setup-java@77dd5b2ce225ea11469928ac317850245a74ae78 # v6.2.0
with:
distribution: temurin
java-version: '17'
cache: maven
server-id: central
server-username: CENTRAL_USERNAME
server-password: CENTRAL_PASSWORD
- name: Set release version from tag
if: startsWith(github.ref, 'refs/tags/v')
run: ./mvnw -B -ntp org.codehaus.mojo:versions-maven-plugin:2.25.3:set -DnewVersion="${GITHUB_REF_NAME#v}" -DgenerateBackupPoms=false
# Tags publish a release; master publishes a -SNAPSHOT (requires snapshots to be enabled
# for the namespace on central.sonatype.com).
- name: Deploy to Maven Central
run: ./mvnw -B -ntp -Prelease,require-natives -DskipTests deploy
env:
CENTRAL_USERNAME: ${{ secrets.CENTRAL_TOKEN_USERNAME }}
CENTRAL_PASSWORD: ${{ secrets.CENTRAL_TOKEN_PASSWORD }}
MAVEN_GPG_KEY: ${{ secrets.OSSRH_GPG_KEY_ASCII }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_PASSPHRASE }}
95 changes: 0 additions & 95 deletions .github/workflows/release.yaml

This file was deleted.

8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
target/
.idea/
*.iml
obj/
build/
.gradle/
local.properties
gradle.properties
.vscode/
# Native libraries are built by CI (or native/build.sh) and bundled at package time.
src/main/resources/*/
3 changes: 3 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wrapperVersion=3.3.4
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.10.2/apache-maven-3.10.2-bin.zip
76 changes: 53 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,83 @@
# curve25519-java

A Java Curve25519 implementation that is backed by native code when available, and
pure Java when a native library is not available.
Curve25519 for Java: X25519 key agreement, XEdDSA signatures and VXEdDSA verifiable random
functions, compatible with Signal's libsignal implementation. Requires Java 17+.

The native provider is a small Rust library on top of
[curve25519-dalek](https://github.com/dalek-cryptography/curve25519-dalek) and
[RustCrypto sha2](https://github.com/RustCrypto/hashes), called through JNI. It picks the
fastest code path for the CPU at runtime:

| CPU | curve arithmetic | SHA-512 |
|---|---|---|
| x86-64 with AVX-512 IFMA (Ice Lake+, Zen 4+) | AVX-512 IFMA | AVX2 |
| x86-64 with AVX2 | AVX2 | AVX2 |
| aarch64 with ARMv8.2 SHA-512 (Apple Silicon, Graviton 3+, Neoverse) | 64-bit scalar | SHA-512 instructions |
| anything else | portable 32/64-bit scalar | portable |

`NativeCurve25519Provider.backend()` reports what was selected, e.g. `curve=avx2 sha512=avx2`.
The pure-Java provider is used when no native library is available for the platform.

The native library reproduces the legacy ref10/donna code it replaces byte for byte: same
public keys, shared secrets and signatures for the same inputs and randomness, and the same
accept/reject decisions for malformed keys and signatures.

Bundled platforms: `linux-{x86-64,aarch64,x86}` (glibc 2.17+), `darwin-{x86-64,aarch64}`,
`win32-{x86-64,x86,aarch64}`.

On JDK 24 and newer, start the JVM with `--enable-native-access=ALL-UNNAMED` (or your module
name) to silence the JVM's warning about native code being loaded.

## Building

### JVM
Toolchain versions are pinned in `mise.toml` (`mise install` sets them up), but any Rust 1.89+
and JDK 17+ will do.

Just use Gradle, for example `gradle compileJava` or `gradle jar` to build jar.
```
native/build.sh # builds the native library for this machine into src/main/resources
./mvnw verify # Java build + tests (pure-Java, native, and a native-vs-Java differential test)
```

### Native
The native library can also be loaded from an explicit file with
`-Dcurve25519.library.path=/path/to/libcurve25519.so`.

Call it from the root of project:
### Benchmarks

```
mkdir native/build
cd native/build
cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build .
./mvnw -Pbench -DskipTests test-compile exec:exec
```

### Benchmark tests
### Releasing

CI (`.github/workflows/build.yaml`) builds the native libraries for all platforms, runs the
tests on Linux/macOS/Windows (x86-64 and arm64) with JDK 17 and 25, and publishes through the
Sonatype Central Portal: tags `vX.Y.Z` publish release `X.Y.Z`, and pushes to `master` publish
the `-SNAPSHOT` from `pom.xml`. Required repository secrets:

To run it call ```gradle jmh```
* `CENTRAL_TOKEN_USERNAME`, `CENTRAL_TOKEN_PASSWORD`: a user token from central.sonatype.com
* `OSSRH_GPG_KEY_ASCII`, `OSSRH_GPG_PASSPHRASE`: the ASCII-armored signing key and its passphrase

## Using

## Obtaining an instance

The caller needs to specify a `provider` when obtaining a Curve25519 instance. There are
four built in providers:
The caller needs to specify a `provider` when obtaining a Curve25519 instance. There are
three built in providers:

1. `Curve25519.NATIVE` -- This is a JNA backed provider.
1. `Curve25519.JAVA` -- This is a pure Java 7 backed provider.
1. `Curve25519.J2ME` -- This is a J2ME compatible provider.
1. `Curve25519.BEST` -- This is a provider that attempts to use `NATIVE`,
but falls back to `JAVA` if the former is unavailable.
1. `Curve25519.NATIVE`: the native (curve25519-dalek) provider.
1. `Curve25519.JAVA`: a pure Java provider.
1. `Curve25519.BEST`: uses `NATIVE`, but falls back to `JAVA` if the former is unavailable.

The caller specifies a provider during instance creation:

```
Curve25519 cipher = Curve25519.getInstance(Curve25519.BEST);
```

Since J2ME doesn't have built-in `SecureRandom` support, J2ME users need to supply their
own source of `SecureRandom` by implementing the `SecureRandomProvider` interface and
passing it in:
A custom source of randomness can be supplied by implementing `SecureRandomProvider`:

```
Curve25519 cipher = Curve25519.getInstance(Curve25519.J2ME, new MySecureRandomProvider());
Curve25519 cipher = Curve25519.getInstance(Curve25519.BEST, new MySecureRandomProvider());
```

### Generating a Curve25519 keypair:
Expand Down Expand Up @@ -83,4 +113,4 @@ Copyright 2015 Open Whisper Systems

Licensed under the GPLv3: http://www.gnu.org/licenses/gpl-3.0.html

Updated by Waves Platform in 2019-2020
Updated by Waves Platform in 2019-2026
Loading
Loading