https://github.com/marschall/getrandom-provider
A SecureRandomSPI that makes the getrandom() system call available to SecureRandom.
https://github.com/marschall/getrandom-provider
java jce linux secure-random-generator
Last synced: about 2 months ago
JSON representation
A SecureRandomSPI that makes the getrandom() system call available to SecureRandom.
- Host: GitHub
- URL: https://github.com/marschall/getrandom-provider
- Owner: marschall
- Created: 2017-12-10T16:24:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-04-09T17:19:41.000Z (about 3 years ago)
- Last Synced: 2025-01-21T08:03:12.628Z (4 months ago)
- Topics: java, jce, linux, secure-random-generator
- Language: Java
- Homepage:
- Size: 197 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# getrandom() SecureRandomSPI [](https://maven-badges.herokuapp.com/maven-central/com.github.marschall/getrandom-provider) [](https://www.javadoc.io/doc/com.github.marschall/getrandom-provider)
A `SecureRandomSPI` that makes [getrandom()](http://man7.org/linux/man-pages/man2/getrandom.2.html) system call available to `SecureRandom`.
* uses syscall, does not depend on glibc wrapper
* tries to use stack allocation rather than allocation on the C heap
* is marked as thread safe so concurrent access through `SecureRandom` will not synchronize in Java 9 and later, however there seems to be a kernel lock on /dev/urandom so you will likely not get additional parallelism
* unlike the `NativePRNG` variants
* does not use a file handle
* does not have a global lock, but see comments on the kernel lock above
* does not additionally mix with `SHA1PRNG`
* blocks until the urandom source has been initialized
* zeros out native memory
* supports the ServiceLoader mechanism
* is a Java 9 module but works on Java 8
* no dependencies outside the `java.base` moduleLikely benefits from [random number generator enhancements for Linux 5.17 and 5.18](https://www.zx2c4.com/projects/linux-rng-5.17-5.18/).
## Usage
A nonblocking (/dev/urandom) instance of the provider can be acquired using
```java
SecureRandom.getInstance("geturandom"); // GetrandomProvider.GETURANDOM
```Alternatively a blocking (/dev/random) instance of the provider can be acquired using
```java
SecureRandom.getInstance("getrandom"); // GetrandomProvider.GETRANDOM
```The /dev/urandom variant is the preferred one, the /dev/random variant is only added for completeness, see [Myths about /dev/urandom](https://www.2uo.de/myths-about-urandom/).
## Configuration
The provider can be configured in two different ways
1. programmatic configuration
1. static configurationFor best startup performance it is recommended to extract the .so from the JAR and add it to a folder present in the `LD_LIBRARY_PATH` environment variable or the `java.library.path` system property. Otherwise this library will extract the .so to a temporary folder the first time it is called.
### Programmatic Configuration
The provider can be registered programmatically using
```java
Security.addProvider(new GetrandomProvider());
```### Static Configuration Java 8
The provider can be configured statically in the `java.security` file by adding the provider at the end
```
security.provider.N=com.github.marschall.getrandom.GetrandomProvider
````N` should be the value of the last provider incremented by 1. For Oracle/OpenJDK 8 on Linux `N` should likely be 10.
This can be done [per JVM installation](https://docs.oracle.com/en/java/javase/11/security/howtoimplaprovider.html#GUID-831AA25F-F702-442D-A2E4-8DA6DEA16F33) or [per JVM Instance](https://docs.oracle.com/en/java/javase/11/security/java-authentication-and-authorization-service-jaas-reference-guide.html#GUID-106F4B32-B9A3-4B75-BDBF-29B252BB3F53).
Note that for this to work the provider JAR needs to be in the class path or extension folder.
### Static Configuration Java 9+
The provider can be configured statically in the `java.security` file by adding the provider at the end
```
security.provider.N=getrandom
````N` should be the value of the last provider incremented by 1. For Oracle/OpenJDK 9 on Linux `N` should likely be 13.
This can be done [per JVM installation](https://docs.oracle.com/javase/9/security/howtoimplaprovider.htm#GUID-831AA25F-F702-442D-A2E4-8DA6DEA16F33) or [per JVM Instance](https://dzone.com/articles/how-override-java-security).
The provider uses the ServiceLoader mechanism therefore using the `getrandom` string is enough, there is no need to use the fully qualified class name.
Note that for this to work the provider JAR needs to be in the class path or module path.
### Performance
[Performance](https://github.com/marschall/random-provider-benchmarks/tree/master/src/main/output/meltdown) compared to `NativePRNGNonBlocking` is lower for small, single threaded workloads due to the syscall overhead and additionally negatively impacted by the meltdown patches. However for large or threaded workloads performance is higher.
### Usage for Tomcat Session Ids
This security provider can be used for session id generation in Tomcat. In order for that several things need to be configured:
1. the JAR needs to be added to the class path
1. the .so should be added to the Java library path (`java.library.path`)
1. the provider needs to be installed into the JVM via `java.security.properties`
1. Tomcat needs to be configured to use the algorithmPoints 1, 2 and 3 can be configured in `CATALINA_BASE/bin/setenv.sh`
```sh
#!/bin/shCLASSPATH="/path/to/getrandom-provider-0.1.1.jar"
CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/path/to/folder/with/so -Djava.security.properties=/path/to/jvm.java.security"export CLASSPATH
export CATALINA_OPTS
```Point can be configured on [the Manager Component](https://tomcat.apache.org/tomcat-8.5-doc/config/manager.html) in `conf/context.xml` by setting `secureRandomAlgorithm` to `geturandom`
```xml
```