Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

45 Commits

Repository files navigation

FNR Cipher

FNR Cipher is a Java implementation for Flexible Naor and Reingold encryption scheme.

Build StatusCoverage Status


It's simple. If you give an integer, you get a encrypted integer. This is two-way operation. That means, if you give the encrypted integer also you get the original integer. The FNR algorithm preserves your data size, no expand, no shrink. All operation space limited by key bit length.

This method also known as format preserving encryption. FNR algorithm is useful for small data types (up to 128 bits) such as credit card or user ids.

FNR uses AES-128 internally in each encryption/decryption rounds. FNR Java contains optimized and minimal AES-128 pure Java cipher. It's may be prefer instead of Java Cryptography Extension in case of performance considerations or JVM limitations. This library doesn't have any dependencies and it is fit for limited envorinments like Andorid. (The JAR file only 43kb.)

The FNR Java library is binary compatible with reference C implementation.

FNR Java provides some built-in codecs for basic data types encryption.


Built-In Supported Java Types:

Java TypeCodecNotes
ByteFNRCodec.BYTENP_SIGN codec not preserve sign.
ShortFNRCodec.SHORTNP_SIGN codec not preserve sign.
CharacterFNRCodec.CHARNP_SIGN codec not preserve sign.
IntegerFNRCodec.INTNP_SIGN codec not preserve sign.
FloatFNRCodec.FLOATNP_SIGN_EXP codec not preserve sign and exponent.
LongFNRCodec.LONGNP_SIGN codec not preserve sign.
DoubleFNRCodec.DOUBLENP_SIGN_EXP codec not preserve sign and exponent.
BigIntegerFNRCodec.BIGINT_128The acceptable value range are -2^127 to 2^127-1 or 0 to 2^128-1
DateFNRCodec.DATE-
Inet4AddressFNRCodec.IPV4-
Inet6AddressFNRCodec.IPV6

Note: All numeric codecs run as litte-endian for compatibiliy with other platform like C or Go and preserve sign and exponents as default.

Install Maven Central

<dependency>
<groupId>io.octa.security</groupId>
<artifactId>fnr</artifactId>
<version>1.0.1</version>
</dependency>

Usage

Stringpassphrase = "this is a password";
Stringsalt = "this is a salt value"; // for built-in PBKDF2 key generation.byte[] aesKey = FNRUtils.createAes128KeyWithPBKDF2(passphrase, salt);
// Integer encryptionFNRKeykey = newFNRKey(aesKey, FNRCodec.INT.getRequiredKeyNumBits());
FNRTweaktweak = key.generateTweak("this is a tweak value");
intraw = 42;
intencrypted = FNRCipher.encrypt(FNRCodec.INT, key, tweak, raw);
intdecrypted = FNRCipher.decrypt(FNRCodec.INT, key, tweak, encrypted);
System.out.println("raw: " + raw); // prints 42System.out.println("encrypted: " + encrypted); // prints 1432569698System.out.println("decrypted: " + decrypted); // prints 42// IP encryptionkey = newFNRKey(aesKey, FNRCodec.IPV4.getRequiredKeyNumBits());
tweak = key.generateTweak("this is a tweak value");
Inet4AddressrawIP = (Inet4Address) Inet4Address.getByName("8.4.4.2");
Inet4AddressencryptedIP = FNRCipher.encrypt(FNRCodec.IPV4, key, tweak, rawIP);
Inet4AddressdecryptedIP = FNRCipher.decrypt(FNRCodec.IPV4, key, tweak, encryptedIP);
System.out.println("raw: " + rawIP); // prints 8.4.4.2System.out.println("encrypted: " + encryptedIP); // prints 25.123.159.248System.out.println("decrypted: " + decryptedIP); // prints 8.4.4.2

Performance

LibraryAES Encryption MethodEncryptionDecryptionNotes
Reference C implementaionOpenSSL229141.720 ops/s230386.135 ops/sOpenSSL uses CPU AES Extension
FNR JavaBuilt-In198160.740 ops/s202775.251 ops/sAES encryption with built-in minimal, optimized cipher
Java binding for Reference C implementaionOpenSSL105766.458 ops/s106495.132 ops/sI think the cause of bottleneck is JNI round-trip overhead.
FNR JavaJCE82998.094 ops/s81175.897 ops/sAES encryption with standard Java Cryptography Extension

Tested on Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz.

Java Benchmark: FNRCipherBenchmarkTest.java

JMH 1.11.3 (released 3 days ago)
VM version: JDK 1.8.0_66, VM 25.66-b17
VM invoker: /usr/lib/jvm/java-8-oracle/jre/bin/java
VM options: <none>
Warmup: 5 iterations, 1 s each
Measurement: 5 iterations, 1 s each
Timeout: 10 min per iteration
Threads: 1 thread, will synchronize iterations
Benchmark mode: Throughput, ops/time

C Benchmark: bench.c

GCC v5.2.1 compiles with -O2 and use OpenSSL 1.0.2d

FNR is designed by Sashank Dara (sadara@cisco.com), Scott Fluhrer (sfluhrer@cisco.com).

Java implementation was written by Mehmet Gurevin (mehmet.gurevin@octabase.com)

Copyright (c) 2015-2016, Octabase, Ltd. All Rights Reserved.

About

A Java implementation for Flexible Naor and Reingold encryption scheme

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages