Argon2 Android library.

This commit is contained in:
Alan Evans 2020-01-15 11:11:42 -05:00 committed by GitHub
parent a96e4f7a0d
commit 6e9c0d4048
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
49 changed files with 897 additions and 327 deletions

3
.gitignore vendored
View file

@ -9,8 +9,7 @@
/.idea/assetWizardSettings.xml
.DS_Store
/build
obj
/captures
.externalNativeBuild
.cxx
app/obj
argon2/obj

3
.gitmodules vendored
View file

@ -0,0 +1,3 @@
[submodule "phc-winner-argon2"]
path = phc-winner-argon2
url = https://github.com/P-H-C/phc-winner-argon2.git

27
README.md Normal file
View file

@ -0,0 +1,27 @@
Argon2 Library
==
Wrapper around the [reference C implementation of Argon2](https://github.com/P-H-C/phc-winner-argon2).
Android Usage
--
```gradle
implementation 'org.signal:argon2:13.0'
```
```java
Argon2 argon2 = new Argon2.Builder(Version.V13)
.type(Type.Argon2id)
.memoryCost(MemoryCost.MiB_32)
.parallelism(1)
.iterations(1)
.build();
Argon2.Result result = argon2.hash(password, salt);
byte[] hash = result.getHash();
String hashHex = result.getHashHex();
String encoded = result.getEncoded();
```

View file

@ -1,30 +1,25 @@
apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
apply from: 'deploy.gradle'
android {
compileSdkVersion 29
compileSdkVersion 28
defaultConfig {
applicationId "org.signal.argon2.testbench"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
sourceSets {
main.jniLibs.srcDirs = ['libs']
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

View file

@ -0,0 +1,76 @@
apply plugin: 'maven'
apply plugin: 'signing'
version = '13.0-SNAPSHOT'
group = 'org.signal'
archivesBaseName = 'argon2'
def isReleaseBuild() {
return version.contains("SNAPSHOT") == false
}
def getReleaseRepositoryUrl() {
return hasProperty('sonatypeRepo') ? sonatypeRepo
: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
}
def getRepositoryUsername() {
return hasProperty('whisperSonatypeUsername') ? whisperSonatypeUsername : ""
}
def getRepositoryPassword() {
return hasProperty('whisperSonatypePassword') ? whisperSonatypePassword : ""
}
signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask('uploadArchives') }
sign configurations.archives
}
uploadArchives {
configuration = configurations.archives
repositories.mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: getReleaseRepositoryUrl()) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
pom.project {
name archivesBaseName
description 'Argon2 Android library wrapping https://github.com/P-H-C/phc-winner-argon2'
url 'https://github.com/signalapp/argon2'
scm {
url 'scm:git@github.com:signalapp/argon2.git'
connection 'scm:git@github.com:signalapp/argon2.git'
developerConnection 'scm:git@github.com:signalapp/argon2.git'
}
licenses {
license {
name 'GPLv3'
url 'https://www.gnu.org/licenses/gpl-3.0.txt'
distribution 'repo'
}
}
developers {
developer {
name 'Alan Evans'
}
}
}
}
}
task installArchives(type: Upload) {
group 'Deploy'
description 'Installs the artifacts to the local Maven repository.'
configuration = configurations['archives']
repositories {
mavenDeployer {
repository url: "file://${System.properties['user.home']}/.m2/repository"
}
}
}

View file

@ -0,0 +1,19 @@
LOCAL_DIR := $(call my-dir)
ARGON2_DIR := $(LOCAL_DIR)/../../../phc-winner-argon2
include $(CLEAR_VARS)
LOCAL_MODULE := argon2
LOCAL_C_INCLUDES := $(ARGON2_DIR)/include/
LOCAL_CFLAGS += -Wall
LOCAL_SRC_FILES := $(LOCAL_DIR)/org_signal_argon2_Argon2Native.c \
$(ARGON2_DIR)/src/blake2/blake2b.c \
$(ARGON2_DIR)/src/argon2.c \
$(ARGON2_DIR)/src/core.c \
$(ARGON2_DIR)/src/encoding.c \
$(ARGON2_DIR)/src/genkat.c \
$(ARGON2_DIR)/src/ref.c \
$(ARGON2_DIR)/src/thread.c
include $(BUILD_SHARED_LIBRARY)

View file

@ -0,0 +1,4 @@
# Built with NDK 19.2.5345600
APP_ABI := armeabi-v7a x86 arm64-v8a x86_64
APP_PLATFORM := android-19
APP_OPTIM := release

View file

@ -0,0 +1,71 @@
#include <stdio.h>
#include <string.h>
#include "org_signal_argon2_Argon2Native.h"
#include "argon2.h"
#define ENCODED_LEN 512
JNIEXPORT jint JNICALL Java_org_signal_argon2_Argon2Native_hash
(JNIEnv *env,
jclass clazz,
jint t,
jint m,
jint parallelism,
jbyteArray jPwd,
jbyteArray jSalt,
jbyteArray jHash,
jobject jEncoded,
jint argon_type,
jint version)
{
jsize pwd_size = (*env)->GetArrayLength(env, jPwd);
jsize salt_size = (*env)->GetArrayLength(env, jSalt);
jsize outLen = (*env)->GetArrayLength(env, jHash);
jbyte* pwdElements = (*env)->GetByteArrayElements(env, jPwd, NULL);
jbyte* saltElements = (*env)->GetByteArrayElements(env, jSalt, NULL);
unsigned char out[outLen];
char encoded[ENCODED_LEN];
int ret = argon2_hash(t, m, parallelism,
pwdElements, pwd_size,
saltElements, salt_size,
out, outLen,
encoded, ENCODED_LEN,
argon_type,
version);
(*env)->ReleaseByteArrayElements(env, jPwd, pwdElements, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, jSalt, saltElements, JNI_ABORT);
if (ret == ARGON2_OK) {
(*env)->SetByteArrayRegion(env, jHash, 0, outLen, (jbyte *)out);
jclass stringBufferClass = (*env)->GetObjectClass(env, jEncoded);
jmethodID appendMethod = (*env)->GetMethodID(env, stringBufferClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
(*env)->CallObjectMethod(env, jEncoded, appendMethod, (*env)->NewStringUTF(env, encoded));
}
return ret;
}
JNIEXPORT jint JNICALL Java_org_signal_argon2_Argon2Native_verify
(JNIEnv *env, jclass clazz, jstring jEncoded, jbyteArray jPwd, jint argon_type)
{
const char *encoded = (*env)->GetStringUTFChars(env, jEncoded, NULL);
jsize pwd_size = (*env)->GetArrayLength(env, jPwd);
jbyte *pwd = (*env)->GetByteArrayElements(env, jPwd, NULL);
int ret = argon2_verify((char *)encoded, pwd, pwd_size, argon_type);
(*env)->ReleaseByteArrayElements(env, jPwd, pwd, JNI_ABORT);
(*env)->ReleaseStringUTFChars(env, jEncoded, encoded);
return ret;
}
JNIEXPORT jstring JNICALL Java_org_signal_argon2_Argon2Native_resultToString
(JNIEnv *env, jclass clazz, jint argonResult)
{
return (*env)->NewStringUTF(env, argon2_error_message(argonResult));
}

View file

@ -0,0 +1,39 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_signal_argon2_Argon2Native */
#ifndef _Included_org_signal_argon2_Argon2Native
#define _Included_org_signal_argon2_Argon2Native
#ifdef __cplusplus
extern "C" {
#endif
#undef org_signal_argon2_Argon2Native_OK
#define org_signal_argon2_Argon2Native_OK 0L
/*
* Class: org_signal_argon2_Argon2Native
* Method: hash
* Signature: (III[B[B[BLjava/lang/StringBuffer;II)I
*/
JNIEXPORT jint JNICALL Java_org_signal_argon2_Argon2Native_hash
(JNIEnv *, jclass, jint, jint, jint, jbyteArray, jbyteArray, jbyteArray, jobject, jint, jint);
/*
* Class: org_signal_argon2_Argon2Native
* Method: verify
* Signature: (Ljava/lang/String;[BI)I
*/
JNIEXPORT jint JNICALL Java_org_signal_argon2_Argon2Native_verify
(JNIEnv *, jclass, jstring, jbyteArray, jint);
/*
* Class: org_signal_argon2_Argon2Native
* Method: resultToString
* Signature: (I)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_signal_argon2_Argon2Native_resultToString
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,56 @@
package org.signal.argon2;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.signal.argon2.TestUtils.utf8;
import static org.signal.argon2.Type.Argon2id;
@RunWith(AndroidJUnit4.class)
public final class Argon2BuilderTest {
@Test
public void memory_too_low() {
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
.type(Argon2id);
assertThatThrownBy(() -> builder.memoryCostKiB(-1))
.isExactlyInstanceOf(IllegalArgumentException.class);
}
@Test
public void memory_too_high() {
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
.type(Argon2id);
assertThatThrownBy(() -> builder.memoryCostOrder(31))
.isExactlyInstanceOf(IllegalArgumentException.class);
}
@Test
public void using_MemoryCost() throws Argon2Exception {
String hash1 = new Argon2.Builder(Version.V13)
.type(Argon2id)
.memoryCost(MemoryCost.MiB(20))
.parallelism(1)
.iterations(1)
.build()
.hash(utf8("signal"), utf8("somesalt"))
.getEncoded();
String hash2 = new Argon2.Builder(Version.V13)
.type(Argon2id)
.memoryCostKiB(20 * 1024)
.parallelism(1)
.iterations(1)
.build()
.hash(utf8("signal"), utf8("somesalt"))
.getEncoded();
assertEquals(hash1, hash2);
}
}

View file

@ -0,0 +1,247 @@
package org.signal.argon2;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.signal.argon2.TestUtils.ascii;
import static org.signal.argon2.Type.Argon2i;
import static org.signal.argon2.Type.Argon2id;
/**
* Cases ported from test.c
*/
@RunWith(AndroidJUnit4.class)
public final class Argon2Test {
@Test
public void argon_version_10_2i() throws Argon2Exception {
Version version = Version.V10;
Type type = Argon2i;
hashtest(version, 2, 16, 1, "password", "somesalt",
"f6c4db4a54e2a370627aff3db6176b94a2a209a62c8e36152711802f7b30c694",
"$argon2i$m=65536,t=2,p=1$c29tZXNhbHQ" +
"$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ", type);
//#ifdef TEST_LARGE_RAM
// hashtest(version, 2, 20, 1, "password", "somesalt",
// "9690ec55d28d3ed32562f2e73ea62b02b018757643a2ae6e79528459de8106e9",
// "$argon2i$m=1048576,t=2,p=1$c29tZXNhbHQ$lpDsVdKNPtMlYvLnPqYrArAYdXZDoq5ueVKEWd6BBuk", Argon2i);
//#endif
hashtest(version, 2, 18, 1, "password", "somesalt",
"3e689aaa3d28a77cf2bc72a51ac53166761751182f1ee292e3f677a7da4c2467",
"$argon2i$m=262144,t=2,p=1$c29tZXNhbHQ" +
"$Pmiaqj0op3zyvHKlGsUxZnYXURgvHuKS4/Z3p9pMJGc", type);
hashtest(version, 2, 8, 1, "password", "somesalt",
"fd4dd83d762c49bdeaf57c47bdcd0c2f1babf863fdeb490df63ede9975fccf06",
"$argon2i$m=256,t=2,p=1$c29tZXNhbHQ" +
"$/U3YPXYsSb3q9XxHvc0MLxur+GP960kN9j7emXX8zwY", type);
hashtest(version, 2, 8, 2, "password", "somesalt",
"b6c11560a6a9d61eac706b79a2f97d68b4463aa3ad87e00c07e2b01e90c564fb",
"$argon2i$m=256,t=2,p=2$c29tZXNhbHQ" +
"$tsEVYKap1h6scGt5ovl9aLRGOqOth+AMB+KwHpDFZPs", type);
hashtest(version, 1, 16, 1, "password", "somesalt",
"81630552b8f3b1f48cdb1992c4c678643d490b2b5eb4ff6c4b3438b5621724b2",
"$argon2i$m=65536,t=1,p=1$c29tZXNhbHQ" +
"$gWMFUrjzsfSM2xmSxMZ4ZD1JCytetP9sSzQ4tWIXJLI", type);
hashtest(version, 4, 16, 1, "password", "somesalt",
"f212f01615e6eb5d74734dc3ef40ade2d51d052468d8c69440a3a1f2c1c2847b",
"$argon2i$m=65536,t=4,p=1$c29tZXNhbHQ" +
"$8hLwFhXm6110c03D70Ct4tUdBSRo2MaUQKOh8sHChHs", type);
hashtest(version, 2, 16, 1, "differentpassword", "somesalt",
"e9c902074b6754531a3a0be519e5baf404b30ce69b3f01ac3bf21229960109a3",
"$argon2i$m=65536,t=2,p=1$c29tZXNhbHQ" +
"$6ckCB0tnVFMaOgvlGeW69ASzDOabPwGsO/ISKZYBCaM", type);
hashtest(version, 2, 16, 1, "password", "diffsalt",
"79a103b90fe8aef8570cb31fc8b22259778916f8336b7bdac3892569d4f1c497",
"$argon2i$m=65536,t=2,p=1$ZGlmZnNhbHQ" +
"$eaEDuQ/orvhXDLMfyLIiWXeJFvgza3vaw4kladTxxJc", type);
}
@Test
public void argon_version_latest_2i() throws Argon2Exception {
Version version = Version.LATEST;
Type type = Argon2i;
hashtest(version, 2, 16, 1, "password", "somesalt",
"c1628832147d9720c5bd1cfd61367078729f6dfb6f8fea9ff98158e0d7816ed0",
"$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ" +
"$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA", type);
//#ifdef TEST_LARGE_RAM
// hashtest(version, 2, 20, 1, "password", "somesalt",
// "d1587aca0922c3b5d6a83edab31bee3c4ebaef342ed6127a55d19b2351ad1f41",
// "$argon2i$v=19$m=1048576,t=2,p=1$c29tZXNhbHQ"
// "$0Vh6ygkiw7XWqD7asxvuPE667zQu1hJ6VdGbI1GtH0E", Argon2i);
//#endif
hashtest(version, 2, 18, 1, "password", "somesalt",
"296dbae80b807cdceaad44ae741b506f14db0959267b183b118f9b24229bc7cb",
"$argon2i$v=19$m=262144,t=2,p=1$c29tZXNhbHQ" +
"$KW266AuAfNzqrUSudBtQbxTbCVkmexg7EY+bJCKbx8s", type);
hashtest(version, 2, 8, 1, "password", "somesalt",
"89e9029f4637b295beb027056a7336c414fadd43f6b208645281cb214a56452f",
"$argon2i$v=19$m=256,t=2,p=1$c29tZXNhbHQ" +
"$iekCn0Y3spW+sCcFanM2xBT63UP2sghkUoHLIUpWRS8", type);
hashtest(version, 2, 8, 2, "password", "somesalt",
"4ff5ce2769a1d7f4c8a491df09d41a9fbe90e5eb02155a13e4c01e20cd4eab61",
"$argon2i$v=19$m=256,t=2,p=2$c29tZXNhbHQ" +
"$T/XOJ2mh1/TIpJHfCdQan76Q5esCFVoT5MAeIM1Oq2E", type);
hashtest(version, 1, 16, 1, "password", "somesalt",
"d168075c4d985e13ebeae560cf8b94c3b5d8a16c51916b6f4ac2da3ac11bbecf",
"$argon2i$v=19$m=65536,t=1,p=1$c29tZXNhbHQ" +
"$0WgHXE2YXhPr6uVgz4uUw7XYoWxRkWtvSsLaOsEbvs8", type);
hashtest(version, 4, 16, 1, "password", "somesalt",
"aaa953d58af3706ce3df1aefd4a64a84e31d7f54175231f1285259f88174ce5b",
"$argon2i$v=19$m=65536,t=4,p=1$c29tZXNhbHQ" +
"$qqlT1YrzcGzj3xrv1KZKhOMdf1QXUjHxKFJZ+IF0zls", type);
hashtest(version, 2, 16, 1, "differentpassword", "somesalt",
"14ae8da01afea8700c2358dcef7c5358d9021282bd88663a4562f59fb74d22ee",
"$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ" +
"$FK6NoBr+qHAMI1jc73xTWNkCEoK9iGY6RWL1n7dNIu4", type);
hashtest(version, 2, 16, 1, "password", "diffsalt",
"b0357cccfbef91f3860b0dba447b2348cbefecadaf990abfe9cc40726c521271",
"$argon2i$v=19$m=65536,t=2,p=1$ZGlmZnNhbHQ" +
"$sDV8zPvvkfOGCw26RHsjSMvv7K2vmQq/6cxAcmxSEnE", type);
}
@Test
public void argon_version_latest_2id() throws Argon2Exception {
Version version = Version.LATEST;
Type type = Argon2id;
hashtest(version, 2, 16, 1, "password", "somesalt",
"09316115d5cf24ed5a15a31a3ba326e5cf32edc24702987c02b6566f61913cf7",
"$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ" +
"$CTFhFdXPJO1aFaMaO6Mm5c8y7cJHAph8ArZWb2GRPPc", type);
hashtest(version, 2, 18, 1, "password", "somesalt",
"78fe1ec91fb3aa5657d72e710854e4c3d9b9198c742f9616c2f085bed95b2e8c",
"$argon2id$v=19$m=262144,t=2,p=1$c29tZXNhbHQ" +
"$eP4eyR+zqlZX1y5xCFTkw9m5GYx0L5YWwvCFvtlbLow", type);
hashtest(version, 2, 8, 1, "password", "somesalt",
"9dfeb910e80bad0311fee20f9c0e2b12c17987b4cac90c2ef54d5b3021c68bfe",
"$argon2id$v=19$m=256,t=2,p=1$c29tZXNhbHQ" +
"$nf65EOgLrQMR/uIPnA4rEsF5h7TKyQwu9U1bMCHGi/4", type);
hashtest(version, 2, 8, 2, "password", "somesalt",
"6d093c501fd5999645e0ea3bf620d7b8be7fd2db59c20d9fff9539da2bf57037",
"$argon2id$v=19$m=256,t=2,p=2$c29tZXNhbHQ" +
"$bQk8UB/VmZZF4Oo79iDXuL5/0ttZwg2f/5U52iv1cDc", type);
hashtest(version, 1, 16, 1, "password", "somesalt",
"f6a5adc1ba723dddef9b5ac1d464e180fcd9dffc9d1cbf76cca2fed795d9ca98",
"$argon2id$v=19$m=65536,t=1,p=1$c29tZXNhbHQ" +
"$9qWtwbpyPd3vm1rB1GThgPzZ3/ydHL92zKL+15XZypg", type);
hashtest(version, 4, 16, 1, "password", "somesalt",
"9025d48e68ef7395cca9079da4c4ec3affb3c8911fe4f86d1a2520856f63172c",
"$argon2id$v=19$m=65536,t=4,p=1$c29tZXNhbHQ" +
"$kCXUjmjvc5XMqQedpMTsOv+zyJEf5PhtGiUghW9jFyw", type);
hashtest(version, 2, 16, 1, "differentpassword", "somesalt",
"0b84d652cf6b0c4beaef0dfe278ba6a80df6696281d7e0d2891b817d8c458fde",
"$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ" +
"$C4TWUs9rDEvq7w3+J4umqA32aWKB1+DSiRuBfYxFj94", type);
hashtest(version, 2, 16, 1, "password", "diffsalt",
"bdf32b05ccc42eb15d58fd19b1f856b113da1e9a5874fdcc544308565aa8141c",
"$argon2id$v=19$m=65536,t=2,p=1$ZGlmZnNhbHQ" +
"$vfMrBczELrFdWP0ZsfhWsRPaHppYdP3MVEMIVlqoFBw", type);
}
/**
* Test harness will assert:
* argon2_hash() returns ARGON2_OK
* HEX output matches expected
* encoded output matches expected
* Argon2.verify() correctly verifies value
*/
private static void hashtest(Version version, int t, int m, int p, String password, String salt, String hexref, String mcRef, Type type) throws Argon2Exception {
Argon2 argon2 = new Argon2.Builder(version)
.type(type)
.iterations(t)
.memoryCostKiB(1 << m)
.parallelism(p)
.hashLength(32)
.build();
Argon2.Result result = argon2.hash(ascii(password), ascii(salt));
assertEquals(hexref, result.getHashHex());
assertArrayEquals(TestUtils.hexToBytes(hexref), result.getHash());
if (version != Version.V10) {
assertEquals(mcRef, result.getEncoded());
}
assertTrue(Argon2.verify(result.getEncoded(), ascii(password), type));
assertTrue(Argon2.verify(mcRef, ascii(password), type));
}
@Test
public void argon_version_10_2i_verify_errors() {
// Handle an invalid encoding correctly (it is missing a $)
assertFalse(Argon2.verify("$argon2i$m=65536,t=2,p=1c29tZXNhbHQ" +
"$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ",
ascii("password"), Argon2i));
// Handle an invalid encoding correctly (it is missing a $)
assertFalse(Argon2.verify("$argon2i$m=65536,t=2,p=1$c29tZXNhbHQ" +
"9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ",
ascii("password"), Argon2i));
// Handle an invalid encoding correctly (salt is too short)
assertFalse(Argon2.verify("$argon2i$m=65536,t=2,p=1$" +
"$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ",
ascii("password"), Argon2i));
// Handle an mismatching hash (the encoded password is "passwore")
assertFalse(Argon2.verify("$argon2i$m=65536,t=2,p=1$c29tZXNhbHQ" +
"$b2G3seW+uPzerwQQC+/E1K50CLLO7YXy0JRcaTuswRo",
ascii("password"), Argon2i));
}
@Test
public void argon_version_13_2i_verify_errors() {
/* Handle an invalid encoding correctly (it is missing a $) */
assertFalse(Argon2.verify("$argon2i$v=19$m=65536,t=2,p=1c29tZXNhbHQ"+
"$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA",
ascii("password"), Argon2i));
/* Handle an invalid encoding correctly (it is missing a $) */
assertFalse(Argon2.verify("$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ"+
"wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA",
ascii("password"), Argon2i));
/* Handle an invalid encoding correctly (salt is too short) */
assertFalse(Argon2.verify("$argon2i$v=19$m=65536,t=2,p=1$"+
"$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ",
ascii("password"), Argon2i));
/* Handle an mismatching hash (the encoded password is "passwore") */
assertFalse(Argon2.verify("$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ"+
"$8iIuixkI73Js3G1uMbezQXD0b8LG4SXGsOwoQkdAQIM",
ascii("password"), Argon2i));
}
@Test
public void memory_too_little() {
Argon2 argon2 = new Argon2.Builder(Version.LATEST)
.type(Argon2id)
.memoryCostOrder(2)
.build();
assertThatThrownBy(() -> argon2.hash(ascii("password"), ascii("diffsalt")))
.isExactlyInstanceOf(Argon2Exception.class)
.hasMessageContaining("Memory cost is too small");
}
@Test
public void salt_too_short() {
Argon2 argon2 = new Argon2.Builder(Version.LATEST)
.type(Argon2id)
.build();
assertThatThrownBy(() -> argon2.hash(ascii("password"), ascii("s")))
.isExactlyInstanceOf(Argon2Exception.class)
.hasMessageContaining("Salt is too short");
}
}

View file

@ -0,0 +1,23 @@
package org.signal.argon2;
import java.nio.charset.StandardCharsets;
public final class TestUtils {
public static byte[] ascii(String s) {
return s.getBytes(StandardCharsets.US_ASCII);
}
public static byte[] utf8(String s) {
return s.getBytes(StandardCharsets.UTF_8);
}
public static byte[] hexToBytes(String hex) {
byte[] data = new byte[hex.length() / 2];
for (int i = 0; i < data.length; i ++) {
data[i] = (byte) ((Character.digit(hex.charAt(i * 2), 16) << 4)
+ Character.digit(hex.charAt(i * 2 + 1), 16));
}
return data;
}
}

View file

@ -0,0 +1 @@
<manifest package="org.signal.argon2" />

View file

@ -0,0 +1,170 @@
package org.signal.argon2;
import java.util.Locale;
public final class Argon2 {
private final int tCostIterations;
private final int mCostKiB;
private final int parallelism;
private final int hashLength;
private final Type type;
private final Version version;
private Argon2(Builder builder) {
this.tCostIterations = builder.tCostIterations;
this.mCostKiB = builder.mCostKiB;
this.parallelism = builder.parallelism;
this.hashLength = builder.hashLength;
this.type = builder.type;
this.version = builder.version;
}
public static boolean verify(String encoded, byte[] password, Type type) {
return Argon2Native.verify(encoded, password, type.nativeValue) == Argon2Native.OK;
}
public static class Builder {
private final Version version;
private int tCostIterations = 3;
private int mCostKiB = 1 << 12;
private int parallelism = 1;
private int hashLength = 32;
private Type type = Type.Argon2i;
public Builder(Version version) {
this.version = version;
}
/**
* Type of Argon to use {@link Type#Argon2i} is the default.
*/
public Builder type(Type type) {
this.type = type;
return this;
}
/**
* Sets parallelism to {@param n} threads (default 1)
*/
public Builder parallelism(int n) {
this.parallelism = n;
return this;
}
/**
* Sets the memory usage of 2^{@param n} KiB (default 12)
*
* @param n This function accepts [0..30]. 0 is 1 KiB and 30 is 1 TiB.
*/
public Builder memoryCostOrder(int n) {
if (n < 0) throw new IllegalArgumentException("n too small, minimum 0");
if (n > 30) throw new IllegalArgumentException("n too high, maximum 30");
return memoryCostKiB(1 << n);
}
/**
* Sets the memory usage of {@param kib} KiB.
*/
public Builder memoryCostKiB(int kib) {
if (kib < 4) throw new IllegalArgumentException("kib too small, minimum 4");
if (kib % 4 != 0) throw new IllegalArgumentException("kib must be multiple of 4");
this.mCostKiB = kib;
return this;
}
/**
* Sets the memory usage using the {@link MemoryCost} enum.
*/
public Builder memoryCost(MemoryCost memoryCost) {
return memoryCostKiB(memoryCost.getKiB());
}
/**
* Sets the number of iterations to {@param n} (default = 3)
*/
public Builder iterations(int n) {
this.tCostIterations = n;
return this;
}
/**
* Output hash length, default 32.
*/
public Builder hashLength(int hashLength) {
this.hashLength = hashLength;
return this;
}
public Argon2 build() {
return new Argon2(this);
}
}
public Result hash(byte[] password, byte[] salt) throws Argon2Exception {
StringBuffer encoded = new StringBuffer();
byte[] hash = new byte[hashLength];
int result = Argon2Native.hash(tCostIterations, mCostKiB, parallelism,
password,
salt,
hash,
encoded,
type.nativeValue,
version.nativeValue);
if (result != Argon2Native.OK) {
throw new Argon2Exception(result, Argon2Native.resultToString(result));
}
return new Result(encoded.toString(), hash);
}
public final class Result {
private final String encoded;
private final byte[] hash;
private Result(String encoded, byte[] hash) {
this.encoded = encoded;
this.hash = hash;
}
public String getEncoded() {
return encoded;
}
public byte[] getHash() {
return hash;
}
public String getHashHex() {
return toHex(hash);
}
@Override
public String toString() {
return String.format(Locale.US,
"Type: %s%n" +
"Iterations: %d%n" +
"Memory: %d KiB%n" +
"Parallelism: %d%n" +
"Hash: %s%n" +
"Encoded: %s%n",
type,
tCostIterations,
mCostKiB,
parallelism,
getHashHex(),
encoded);
}
}
private static String toHex(byte[] hash) {
StringBuilder stringBuilder = new StringBuilder(hash.length * 2);
for (byte b : hash) {
stringBuilder.append(String.format(Locale.US, "%02x", b));
}
return stringBuilder.toString();
}
}

View file

@ -0,0 +1,10 @@
package org.signal.argon2;
import java.util.Locale;
public final class Argon2Exception extends Exception {
Argon2Exception(int nativeErrorValue, String nativeErrorMessage) {
super(String.format(Locale.US, "Argon failed %d: %s", nativeErrorValue, nativeErrorMessage));
}
}

View file

@ -0,0 +1,24 @@
package org.signal.argon2;
final class Argon2Native {
static final int OK = 0;
static {
System.loadLibrary("argon2");
}
static native int hash(int tCost,
int mCost,
int parallelism,
byte[] pwd,
byte[] salt,
byte[] hash,
StringBuffer encoded,
int argon2Type,
int version);
static native int verify(String encoded, byte[] pwd, int argon2Type);
static native String resultToString(int argonResult);
}

View file

@ -0,0 +1,31 @@
package org.signal.argon2;
/**
* For use in the {@link Argon2.Builder#memoryCost(MemoryCost)} method for readability.
*/
public final class MemoryCost {
private final int kib;
public static MemoryCost KiB(int kib) {
return new MemoryCost(kib);
}
public static MemoryCost MiB(int mib) {
return new MemoryCost(mib * 1024);
}
private MemoryCost(int kib) {
this.kib = kib;
}
/** Number of bytes */
public long toBytes() {
return kib * 1024L;
}
/** Number of Kibibytes */
public int getKiB() {
return kib;
}
}

View file

@ -0,0 +1,16 @@
package org.signal.argon2;
/**
* Argon2 primitive type.
*/
public enum Type {
Argon2d(0),
Argon2i(1),
Argon2id(2);
final int nativeValue;
Type(int nativeValue) {
this.nativeValue = nativeValue;
}
}

View file

@ -0,0 +1,16 @@
package org.signal.argon2;
/**
* Version of the Argon2 algorithm.
*/
public enum Version {
V10(0x10),
V13(0x13),
LATEST(0x13);
final int nativeValue;
Version(int nativeValue) {
this.nativeValue = nativeValue;
}
}

View file

@ -0,0 +1,50 @@
package org.signal.argon2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public final class MemoryCostToBytesTest {
private final MemoryCost memoryCost;
private final int expectedBytes;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{ MemoryCost.KiB_8, 8 * 1024 },
{ MemoryCost.KiB_16, 16 * 1024 },
{ MemoryCost.KiB_32, 32 * 1024 },
{ MemoryCost.KiB_64, 64 * 1024 },
{ MemoryCost.KiB_128, 128 * 1024 },
{ MemoryCost.KiB_256, 256 * 1024 },
{ MemoryCost.KiB_512, 512 * 1024 },
{ MemoryCost.MiB_1, 1024 * 1024 },
{ MemoryCost.MiB_2, 2 * 1024 * 1024 },
{ MemoryCost.MiB_4, 4 * 1024 * 1024 },
{ MemoryCost.MiB_8, 8 * 1024 * 1024 },
{ MemoryCost.MiB_16, 16 * 1024 * 1024 },
{ MemoryCost.MiB_32, 32 * 1024 * 1024 },
{ MemoryCost.MiB_64, 64 * 1024 * 1024 },
{ MemoryCost.MiB_128, 128 * 1024 * 1024 }
});
}
public MemoryCostToBytesTest(MemoryCost memoryCost, int expectedBytes) {
this.memoryCost = memoryCost;
this.expectedBytes = expectedBytes;
}
@Test
public void toBytes() {
assertEquals(expectedBytes, memoryCost.toBytes());
}
}

1
app/.gitignore vendored
View file

@ -1 +0,0 @@
/build

View file

@ -1,21 +0,0 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View file

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.signal.argon2.testbench">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View file

@ -1,14 +0,0 @@
package org.signal.argon2.testbench;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public final class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

View file

@ -1,34 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeWidth="1"
android:strokeColor="#00000000">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>

View file

@ -1,170 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#008577"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,29L89,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,39L89,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,49L89,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,59L89,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,69L89,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,79L89,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,19L29,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,19L39,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,19L49,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,19L59,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,19L69,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,19L79,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
</vector>

View file

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

View file

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>

View file

@ -1,3 +0,0 @@
<resources>
<string name="app_name">Argon2TestBench</string>
</resources>

View file

@ -1,11 +0,0 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

1
phc-winner-argon2 Submodule

@ -0,0 +1 @@
Subproject commit 62358ba2123abd17fccf2a108a301d4b52c01a7c

View file

@ -1,2 +1,3 @@
include ':app'
rootProject.name='Argon2TestBench'
include ':android'
include ':android:argon2'
rootProject.name='Argon2Android'