MOD: added new crypto wrapper files, builds, time to test
This commit is contained in:
parent
ff2b314b53
commit
948d10ef08
|
@ -3,27 +3,16 @@ CCC Agile
|
|||
=========
|
||||
|
||||
|
||||
FEATURES
|
||||
--------
|
||||
* ndk cmake builds (C++, LLVM support, ABI)
|
||||
* .aar library packaging, includes java .class files (and other resources)
|
||||
|
||||
|
||||
|
||||
|
||||
WORKING
|
||||
-------
|
||||
=======
|
||||
* NC: add more JNI crypto features: SHA, RSA, CHACHA
|
||||
* UI: adding encrypt screen
|
||||
* l@@k at Android Weekly emails
|
||||
|
||||
|
||||
Milestone
|
||||
^^^^^^^^^
|
||||
* WolfSSL validate with JNI
|
||||
|
||||
|
||||
|
||||
BACKLOG
|
||||
-------
|
||||
=======
|
||||
|
||||
Tasks
|
||||
^^^^^
|
||||
|
@ -50,9 +39,6 @@ Tasks
|
|||
|
||||
Milestones
|
||||
^^^^^^^^^^
|
||||
* JNI wrapper tests
|
||||
- RSA Public Key Generation
|
||||
- AES crypt
|
||||
|
||||
* normalizedcrypto
|
||||
- maybe wrap nextcrypto with a *VERY* simple API
|
||||
|
@ -64,7 +50,8 @@ Milestones
|
|||
|
||||
|
||||
COMPLETED
|
||||
---------
|
||||
=========
|
||||
|
||||
* TEST: add test code for JNI calls
|
||||
- tested AES, everything else should work accordingly
|
||||
* combine wolfssl-lib + wolfssl-jni == ccc-ndk-jni (not ccc-jni which would be for java)
|
||||
|
@ -76,5 +63,15 @@ COMPLETED
|
|||
- discover which gradle to target --> wolfssl was main gradle app target
|
||||
|
||||
|
||||
Milestones
|
||||
^^^^^^^^^^
|
||||
* JNI wrapper tests
|
||||
- RSA Public Key Generation
|
||||
- AES crypt
|
||||
* X Android application skeleton
|
||||
* X WolfSSL validate with JNI
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
=============
|
||||
Project Notes
|
||||
=============
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
=========================
|
||||
Next Crypto Project Notes
|
||||
=========================
|
||||
|
||||
|
||||
FEATURES
|
||||
--------
|
||||
* ndk cmake builds (C++, LLVM support, ABI)
|
||||
* .aar library packaging, includes java .class files (and other resources)
|
||||
|
||||
|
||||
|
||||
|
||||
|
Binary file not shown.
|
@ -1,6 +1,5 @@
|
|||
#Sun Feb 26 23:05:09 PST 2017
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env bash
|
||||
#!/usr/bin/env sh
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
|
@ -28,16 +28,16 @@ APP_NAME="Gradle"
|
|||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS=""
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn ( ) {
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die ( ) {
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
|
@ -154,11 +154,19 @@ if $cygwin ; then
|
|||
esac
|
||||
fi
|
||||
|
||||
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
||||
function splitJvmOpts() {
|
||||
JVM_OPTS=("$@")
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
||||
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
||||
APP_ARGS=$(save "$@")
|
||||
|
||||
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||
cd "$(dirname "$0")"
|
||||
fi
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
|
@ -1,4 +1,4 @@
|
|||
// include this at the top, for shared libraries
|
||||
include ':wolfssl-lib'
|
||||
// include ':wolfssl-lib'
|
||||
// include ':wolfssl-jni'
|
||||
include ':ccc-jni'
|
||||
|
|
|
@ -46,5 +46,5 @@ task copyLibs(type: Copy) {
|
|||
|
||||
|
||||
dependencies {
|
||||
// testImplementation 'junit:junit:4.12'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
/* Asn.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Asn implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 1.0, March 2015
|
||||
*/
|
||||
public class Asn extends WolfObject {
|
||||
|
||||
public static final int MAX_ENCODED_SIG_SIZE = 512;
|
||||
|
||||
public static native void encodeSignature(ByteBuffer encoded,
|
||||
ByteBuffer hash, long hashSize, int hashOID);
|
||||
|
||||
public static native long encodeSignature(byte[] encoded,
|
||||
byte[] hash, long hashSize, int hashOID);
|
||||
|
||||
public static native int getCTC_HashOID(int type);
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
/* Chacha.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Chacha implementation.
|
||||
*
|
||||
* @author Daniele Lacamera
|
||||
* @version 1.0, March 2018
|
||||
*/
|
||||
public class Chacha extends NativeStruct {
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
|
||||
public Chacha() {
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
free();
|
||||
|
||||
super.releaseNativeStruct();
|
||||
}
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
private native void wc_Chacha_init();
|
||||
|
||||
private native void wc_Chacha_free();
|
||||
|
||||
private native byte[] wc_Chacha_process(byte in[]);
|
||||
|
||||
private native void wc_Chacha_setKey(byte[] Key);
|
||||
|
||||
private native void wc_Chacha_setIV(byte[] IV);
|
||||
|
||||
|
||||
|
||||
|
||||
protected void init() {
|
||||
if (state == WolfCryptState.UNINITIALIZED) {
|
||||
wc_Chacha_init();
|
||||
state = WolfCryptState.INITIALIZED;
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Native resources already initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void free() {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
wc_Chacha_free();
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void setKey(byte[] Key) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_Chacha_setKey(Key);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void setIV(byte[] IV) {
|
||||
wc_Chacha_setIV(IV);
|
||||
}
|
||||
|
||||
public byte[] process(byte[] in) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_Chacha_process(in);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,172 @@
|
|||
/* Curve25519.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.spec.EllipticCurve;
|
||||
import java.security.spec.ECParameterSpec;
|
||||
import java.security.spec.ECFieldFp;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt curve25519 implementation.
|
||||
*
|
||||
* @author Daniele Lacamera
|
||||
* @version 1.0, March 2018
|
||||
*/
|
||||
public class Curve25519 extends NativeStruct {
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
|
||||
public Curve25519() {
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
free();
|
||||
|
||||
super.releaseNativeStruct();
|
||||
}
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
private native void wc_curve25519_init();
|
||||
|
||||
private native void wc_curve25519_free();
|
||||
|
||||
private native void wc_curve25519_make_key(Rng rng, int size);
|
||||
|
||||
private native void wc_curve25519_make_key_ex(Rng rng, int size, int endian);
|
||||
|
||||
private native void wc_curve25519_check_key();
|
||||
|
||||
private native byte[] wc_curve25519_make_shared_secret(Curve25519 pubKey);
|
||||
|
||||
private native void wc_curve25519_import_private(byte[] privKey, byte[] key);
|
||||
private native void wc_curve25519_import_private_only(byte[] privKey);
|
||||
private native void wc_curve25519_import_public(byte[] pubKey);
|
||||
|
||||
|
||||
private native byte[] wc_curve25519_export_private();
|
||||
private native byte[] wc_curve25519_export_public();
|
||||
|
||||
|
||||
protected void init() {
|
||||
if (state == WolfCryptState.UNINITIALIZED) {
|
||||
wc_curve25519_init();
|
||||
state = WolfCryptState.INITIALIZED;
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Native resources already initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void free() {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
wc_curve25519_free();
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void makeKey(Rng rng, int size) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_curve25519_make_key(rng, size);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void makeKeyWithEndian(Rng rng, int size, int endian) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_curve25519_make_key_ex(rng, size, endian);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void checkKey() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
wc_curve25519_check_key();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPrivate(byte[] privKey, byte[] xKey) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_curve25519_import_private(privKey, xKey);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPrivateOnly(byte[] privKey) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_curve25519_import_private_only(privKey);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPublic(byte[] pubKey) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_curve25519_import_public(pubKey);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] exportPrivate() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_curve25519_export_private();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] exportPublic() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_curve25519_export_public();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] makeSharedSecret(Curve25519 pubKey) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_curve25519_make_shared_secret(pubKey);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/* Des3.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Des3 implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 1.0, February 2015
|
||||
*/
|
||||
public class Des3 extends BlockCipher {
|
||||
|
||||
public static final int KEY_SIZE = 24;
|
||||
public static final int BLOCK_SIZE = 8;
|
||||
public static final int ENCRYPT_MODE = 0;
|
||||
public static final int DECRYPT_MODE = 1;
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
|
||||
private int opmode;
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
protected native void native_set_key(byte[] key, byte[] iv, int opmode);
|
||||
|
||||
protected native int native_update(int opmode, byte[] input, int offset,
|
||||
int length, byte[] output, int outputOffset);
|
||||
|
||||
protected native int native_update(int opmode, ByteBuffer input,
|
||||
int offset, int length, ByteBuffer output, int outputOffset);
|
||||
|
||||
public Des3() {
|
||||
}
|
||||
|
||||
public Des3(byte[] key, byte[] iv, int opmode) {
|
||||
setKey(key, iv, opmode);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
/* Dh.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt DH implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Dh extends NativeStruct {
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
private byte[] privateKey = null;
|
||||
private byte[] publicKey = null;
|
||||
private int pSize = 0;
|
||||
|
||||
public Dh() {
|
||||
init();
|
||||
}
|
||||
|
||||
public Dh(byte[] p, byte[] g) {
|
||||
init();
|
||||
setParams(p, g);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
free();
|
||||
|
||||
super.releaseNativeStruct();
|
||||
}
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
private native void wc_InitDhKey();
|
||||
|
||||
private native void wc_FreeDhKey();
|
||||
|
||||
private native void wc_DhSetKey(byte[] p, byte[] g);
|
||||
|
||||
private native void wc_DhGenerateKeyPair(Rng rng, int pSize);
|
||||
|
||||
private native byte[] wc_DhAgree(byte[] priv, byte[] pub);
|
||||
|
||||
protected void init() {
|
||||
if (state == WolfCryptState.UNINITIALIZED) {
|
||||
wc_InitDhKey();
|
||||
state = WolfCryptState.INITIALIZED;
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Native resources already initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void free() {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
wc_FreeDhKey();
|
||||
|
||||
setPrivateKey(new byte[0]);
|
||||
setPublicKey(new byte[0]);
|
||||
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void setPrivateKey(byte[] priv) {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
if (privateKey != null)
|
||||
for (int i = 0; i < privateKey.length; i++)
|
||||
privateKey[i] = 0;
|
||||
|
||||
privateKey = priv.clone();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available parameters to perform opetarion.");
|
||||
}
|
||||
}
|
||||
|
||||
public void setPublicKey(byte[] pub) {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
if (publicKey != null)
|
||||
for (int i = 0; i < publicKey.length; i++)
|
||||
publicKey[i] = 0;
|
||||
|
||||
publicKey = pub.clone();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available parameters to perform opetarion.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getPublicKey() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
public byte[] getPrivateKey() {
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
public void setParams(byte[] p, byte[] g) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_DhSetKey(p, g);
|
||||
this.pSize = p.length;
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has parameters.");
|
||||
}
|
||||
}
|
||||
|
||||
public void makeKey(Rng rng) {
|
||||
if (privateKey == null) {
|
||||
/* use size of P to allocate key buffer size */
|
||||
wc_DhGenerateKeyPair(rng, this.pSize);
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] makeSharedSecret(Dh pubKey) {
|
||||
byte[] publicKey = pubKey.getPublicKey();
|
||||
|
||||
if (privateKey != null || publicKey != null) {
|
||||
return wc_DhAgree(privateKey, publicKey);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,296 @@
|
|||
/* Ecc.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.spec.EllipticCurve;
|
||||
import java.security.spec.ECParameterSpec;
|
||||
import java.security.spec.ECFieldFp;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt ecc implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, February 2017
|
||||
*/
|
||||
public class Ecc extends NativeStruct {
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
|
||||
public Ecc() {
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
free();
|
||||
|
||||
super.releaseNativeStruct();
|
||||
}
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
private native void wc_ecc_init();
|
||||
|
||||
private native void wc_ecc_free();
|
||||
|
||||
private native void wc_ecc_make_key(Rng rng, int size);
|
||||
|
||||
private native void wc_ecc_make_key_ex(Rng rng, int size, String curveName);
|
||||
|
||||
private native void wc_ecc_check_key();
|
||||
|
||||
private native byte[] wc_ecc_shared_secret(Ecc pubKey);
|
||||
|
||||
private native void wc_ecc_import_private(byte[] privKey, byte[] x963Key,
|
||||
String curveName);
|
||||
|
||||
private native byte[] wc_ecc_export_private();
|
||||
|
||||
private native void wc_ecc_import_x963(byte[] key);
|
||||
|
||||
private native byte[] wc_ecc_export_x963();
|
||||
|
||||
private native void wc_EccPrivateKeyDecode(byte[] key);
|
||||
|
||||
private native byte[] wc_EccKeyToDer();
|
||||
|
||||
private native void wc_EccPublicKeyDecode(byte[] key);
|
||||
|
||||
private native byte[] wc_EccPublicKeyToDer();
|
||||
|
||||
private native byte[] wc_ecc_sign_hash(byte[] hash, Rng rng);
|
||||
|
||||
private native boolean wc_ecc_verify_hash(byte[] hash, byte[] signature);
|
||||
|
||||
private static native int wc_ecc_get_curve_size_from_name(String name);
|
||||
|
||||
private native byte[] wc_ecc_private_key_to_pkcs8();
|
||||
|
||||
private static native String wc_ecc_get_curve_name_from_id(int curve_id);
|
||||
|
||||
private static native int wc_ecc_get_curve_id_from_params(int fieldSize,
|
||||
byte[] prime, byte[] Af, byte[] Bf, byte[] order,
|
||||
byte[] Gx, byte[] Gy, int cofactor);
|
||||
|
||||
protected void init() {
|
||||
if (state == WolfCryptState.UNINITIALIZED) {
|
||||
wc_ecc_init();
|
||||
state = WolfCryptState.INITIALIZED;
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Native resources already initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void free() {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
wc_ecc_free();
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void makeKey(Rng rng, int size) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ecc_make_key(rng, size);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void makeKeyOnCurve(Rng rng, int size, String curveName) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ecc_make_key_ex(rng, size, curveName.toUpperCase());
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void checkKey() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
wc_ecc_check_key();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPrivate(byte[] privKey, byte[] x963Key) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ecc_import_private(privKey, x963Key, null);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPrivateOnCurve(byte[] privKey, byte[] x963Key,
|
||||
String curveName) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ecc_import_private(privKey, x963Key, curveName);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] exportPrivate() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_ecc_export_private();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importX963(byte[] key) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ecc_import_x963(key);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] exportX963() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_ecc_export_x963();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void privateKeyDecode(byte[] key) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_EccPrivateKeyDecode(key);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] privateKeyEncode() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_EccKeyToDer();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void publicKeyDecode(byte[] key) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_EccPublicKeyDecode(key);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] publicKeyEncode() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_EccPublicKeyToDer();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] makeSharedSecret(Ecc pubKey) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_ecc_shared_secret(pubKey);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] sign(byte[] hash, Rng rng) {
|
||||
byte[] signature = new byte[0];
|
||||
|
||||
if (state == WolfCryptState.READY) {
|
||||
signature = wc_ecc_sign_hash(hash, rng);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
|
||||
return signature;
|
||||
}
|
||||
|
||||
public boolean verify(byte[] hash, byte[] signature) {
|
||||
boolean result = false;
|
||||
|
||||
if (state == WolfCryptState.READY) {
|
||||
result = wc_ecc_verify_hash(hash, signature);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int getCurveSizeFromName(String curveName) {
|
||||
/* Ecc object doesn't need to be initialied before call */
|
||||
return wc_ecc_get_curve_size_from_name(curveName);
|
||||
}
|
||||
|
||||
public byte[] privateKeyEncodePKCS8() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_ecc_private_key_to_pkcs8();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public static String getCurveName(ECParameterSpec spec)
|
||||
throws InvalidAlgorithmParameterException
|
||||
{
|
||||
int curve_id;
|
||||
|
||||
/* Ecc object doesn't need to be initialied before call */
|
||||
if (!(spec.getCurve().getField() instanceof ECFieldFp)) {
|
||||
throw new InvalidAlgorithmParameterException(
|
||||
"Currently only ECFieldFp fields supported");
|
||||
}
|
||||
ECFieldFp field = (ECFieldFp)spec.getCurve().getField();
|
||||
EllipticCurve curve = spec.getCurve();
|
||||
|
||||
curve_id = wc_ecc_get_curve_id_from_params(
|
||||
field.getFieldSize(),
|
||||
field.getP().toByteArray(),
|
||||
curve.getA().toByteArray(),
|
||||
curve.getB().toByteArray(),
|
||||
spec.getOrder().toByteArray(),
|
||||
spec.getGenerator().getAffineX().toByteArray(),
|
||||
spec.getGenerator().getAffineY().toByteArray(),
|
||||
spec.getCofactor());
|
||||
|
||||
return wc_ecc_get_curve_name_from_id(curve_id);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,183 @@
|
|||
/* Ed25519.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt ed25519 implementation.
|
||||
*
|
||||
* @author Daniele Lacamera
|
||||
* @version 1.0, March 2018
|
||||
*/
|
||||
public class Ed25519 extends NativeStruct {
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
|
||||
public Ed25519() {
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
free();
|
||||
|
||||
super.releaseNativeStruct();
|
||||
}
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
private native void wc_ed25519_init();
|
||||
|
||||
private native void wc_ed25519_free();
|
||||
|
||||
private native void wc_ed25519_make_key(Rng rng, int size);
|
||||
|
||||
private native void wc_ed25519_check_key();
|
||||
|
||||
private native void wc_ed25519_import_private(byte[] privKey, byte[] key);
|
||||
private native void wc_ed25519_import_private_only(byte[] privKey);
|
||||
private native void wc_ed25519_import_public(byte[] privKey);
|
||||
|
||||
private native byte[] wc_ed25519_sign_msg(byte[] msg);
|
||||
private native boolean wc_ed25519_verify_msg(byte[] sig, byte[] msg);
|
||||
|
||||
private native byte[] wc_ed25519_export_private();
|
||||
private native byte[] wc_ed25519_export_private_only();
|
||||
private native byte[] wc_ed25519_export_public();
|
||||
|
||||
protected void init() {
|
||||
if (state == WolfCryptState.UNINITIALIZED) {
|
||||
wc_ed25519_init();
|
||||
state = WolfCryptState.INITIALIZED;
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Native resources already initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void free() {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
wc_ed25519_free();
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void makeKey(Rng rng, int size) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ed25519_make_key(rng, size);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void checkKey() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
wc_ed25519_check_key();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPrivate(byte[] privKey, byte[] Key) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ed25519_import_private(privKey, Key);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPrivateOnly(byte[] privKey) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ed25519_import_private_only(privKey);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public void importPublic(byte[] Key) {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
wc_ed25519_import_public(Key);
|
||||
state = WolfCryptState.READY;
|
||||
} else {
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] exportPrivate() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_ed25519_export_private();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] exportPrivateOnly() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_ed25519_export_private_only();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] exportPublic() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_ed25519_export_public();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] sign_msg(byte[] msg_in) {
|
||||
|
||||
byte[] msg_out = null;
|
||||
if (state == WolfCryptState.READY) {
|
||||
msg_out = wc_ed25519_sign_msg(msg_in);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
|
||||
return msg_out;
|
||||
}
|
||||
|
||||
public boolean verify_msg(byte[] msg, byte[] signature) {
|
||||
boolean result = false;
|
||||
|
||||
if (state == WolfCryptState.READY) {
|
||||
result = wc_ed25519_verify_msg(signature, msg);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the operation.");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,214 @@
|
|||
/* Hmac.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import com.wolfssl.wolfcrypt.WolfCrypt;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Hmac implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Hmac extends NativeStruct {
|
||||
|
||||
private enum hashType {
|
||||
typeMD5, typeSHA, typeSHA256, typeSHA384, typeSHA512, typeBLAKE2b;
|
||||
}
|
||||
|
||||
public static final int MD5 = getHashCode(hashType.typeMD5);
|
||||
public static final int SHA = getHashCode(hashType.typeSHA);
|
||||
public static final int SHA256 = getHashCode(hashType.typeSHA256);
|
||||
public static final int SHA384 = getHashCode(hashType.typeSHA384);
|
||||
public static final int SHA512 = getHashCode(hashType.typeSHA512);
|
||||
public static final int BLAKE2b = getHashCode(hashType.typeBLAKE2b);
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
private int type = -1;
|
||||
private byte[] key;
|
||||
|
||||
public Hmac() {
|
||||
}
|
||||
|
||||
public Hmac(int type, byte[] key) {
|
||||
setKey(type, key);
|
||||
}
|
||||
|
||||
private native void wc_HmacSetKey(int type, byte[] key);
|
||||
|
||||
private native void wc_HmacUpdate(byte data);
|
||||
|
||||
private native void wc_HmacUpdate(byte[] data, int offset, int length);
|
||||
|
||||
private native void wc_HmacUpdate(ByteBuffer data, int offset, int length);
|
||||
|
||||
private native byte[] wc_HmacFinal();
|
||||
|
||||
private native int wc_HmacSizeByType(int type);
|
||||
|
||||
private native static int getCodeMd5();
|
||||
|
||||
private native static int getCodeSha();
|
||||
|
||||
private native static int getCodeSha256();
|
||||
|
||||
private native static int getCodeSha384();
|
||||
|
||||
private native static int getCodeSha512();
|
||||
|
||||
private native static int getCodeBlake2b();
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
public void setKey(int type, byte[] key) {
|
||||
wc_HmacSetKey(type, key);
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
|
||||
state = WolfCryptState.READY;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
setKey(type, key);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void update(byte data) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
wc_HmacUpdate(data);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void update(byte[] data) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
wc_HmacUpdate(data, 0, data.length);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void update(byte[] data, int offset, int length) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
wc_HmacUpdate(data, offset, length);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void update(ByteBuffer data) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
int offset = data.position();
|
||||
int length = data.remaining();
|
||||
|
||||
wc_HmacUpdate(data, offset, length);
|
||||
|
||||
data.position(offset + length);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] doFinal() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_HmacFinal();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] doFinal(byte[] data) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
update(data);
|
||||
return wc_HmacFinal();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public String getAlgorithm() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
|
||||
if (type == MD5) {
|
||||
return "HmacMD5";
|
||||
}
|
||||
else if (type == SHA256) {
|
||||
return "HmacSHA256";
|
||||
}
|
||||
else if (type == SHA384) {
|
||||
return "HmacSHA384";
|
||||
}
|
||||
else if (type == SHA512) {
|
||||
return "HmacSHA512";
|
||||
}
|
||||
else if (type == BLAKE2b) {
|
||||
return "HmacBLAKE2b";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
public int getMacLength() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
return wc_HmacSizeByType(type);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
}
|
||||
|
||||
private static int getHashCode(hashType hash) {
|
||||
switch (hash) {
|
||||
case typeMD5:
|
||||
return getCodeMd5();
|
||||
case typeSHA:
|
||||
return getCodeSha();
|
||||
case typeSHA256:
|
||||
return getCodeSha256();
|
||||
case typeSHA384:
|
||||
return getCodeSha384();
|
||||
case typeSHA512:
|
||||
return getCodeSha512();
|
||||
case typeBLAKE2b:
|
||||
return getCodeBlake2b();
|
||||
default:
|
||||
return WolfCrypt.FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/* Md5.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Md5 implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Md5 extends MessageDigest {
|
||||
|
||||
public static final int TYPE = 0; /* hash type unique */
|
||||
public static final int DIGEST_SIZE = 16;
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
protected native void native_init();
|
||||
|
||||
protected native void native_update(ByteBuffer data, int offset, int len);
|
||||
|
||||
protected native void native_update(byte[] data, int offset, int len);
|
||||
|
||||
protected native void native_final(ByteBuffer hash, int offset);
|
||||
|
||||
protected native void native_final(byte[] hash);
|
||||
|
||||
public Md5() {
|
||||
init();
|
||||
}
|
||||
|
||||
public Md5(byte[] data) {
|
||||
init();
|
||||
update(data);
|
||||
}
|
||||
|
||||
public int digestSize() {
|
||||
return DIGEST_SIZE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,142 @@
|
|||
/* MessageDigest.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import javax.crypto.ShortBufferException;
|
||||
|
||||
/**
|
||||
* Common API for Message Digests.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 1.0, March 2017
|
||||
*/
|
||||
public abstract class MessageDigest extends NativeStruct {
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
|
||||
protected abstract void native_init();
|
||||
|
||||
protected abstract void native_update(ByteBuffer data, int offset,
|
||||
int length);
|
||||
|
||||
protected abstract void native_update(byte[] data, int offset, int length);
|
||||
|
||||
protected abstract void native_final(ByteBuffer hash, int offset);
|
||||
|
||||
protected abstract void native_final(byte[] hash);
|
||||
|
||||
public abstract int digestSize();
|
||||
|
||||
public void init() {
|
||||
native_init();
|
||||
state = WolfCryptState.READY;
|
||||
}
|
||||
|
||||
public void update(ByteBuffer data, int length) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
length = Math.min(length, data.remaining());
|
||||
|
||||
native_update(data, data.position(), length);
|
||||
data.position(data.position() + length);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Object must be initialized before use");
|
||||
}
|
||||
}
|
||||
|
||||
public void update(ByteBuffer data) {
|
||||
update(data, data.remaining());
|
||||
}
|
||||
|
||||
public void update(byte[] data, int offset, int len) {
|
||||
if (state == WolfCryptState.READY) {
|
||||
if (offset >= data.length || offset < 0 || len < 0)
|
||||
return;
|
||||
|
||||
if (data.length - offset < len)
|
||||
len = data.length - offset;
|
||||
|
||||
native_update(data, offset, len);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Object must be initialized before use");
|
||||
}
|
||||
}
|
||||
|
||||
public void update(byte[] data, int len) {
|
||||
update(data, 0, len);
|
||||
}
|
||||
|
||||
public void update(byte[] data) {
|
||||
update(data, 0, data.length);
|
||||
}
|
||||
|
||||
public void digest(ByteBuffer hash) throws ShortBufferException {
|
||||
if (state == WolfCryptState.READY) {
|
||||
if (hash.remaining() < digestSize())
|
||||
throw new ShortBufferException(
|
||||
"Input buffer is too small for digest size");
|
||||
|
||||
native_final(hash, hash.position());
|
||||
hash.position(hash.position() + digestSize());
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Object must be initialized before use");
|
||||
}
|
||||
}
|
||||
|
||||
public void digest(byte[] hash) throws ShortBufferException {
|
||||
if (state == WolfCryptState.READY) {
|
||||
if (hash.length < digestSize())
|
||||
throw new ShortBufferException(
|
||||
"Input buffer is too small for digest size");
|
||||
|
||||
native_final(hash);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Object must be initialized before use");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] digest() {
|
||||
if (state == WolfCryptState.READY) {
|
||||
byte[] hash = new byte[digestSize()];
|
||||
|
||||
native_final(hash);
|
||||
|
||||
return hash;
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Object must be initialized before use");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
|
||||
/* reset state first, then free */
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
setNativeStruct(NULL);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
/* Rng.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Rng implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 1.0, March 2015
|
||||
*/
|
||||
public class Rng extends NativeStruct {
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
|
||||
/* native wrappers called by public functions below */
|
||||
private native void initRng();
|
||||
|
||||
private native void freeRng();
|
||||
|
||||
private native void rngGenerateBlock(ByteBuffer buffer, int offset,
|
||||
int length);
|
||||
|
||||
private native void rngGenerateBlock(byte[] buffer, int offset, int length);
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
free();
|
||||
|
||||
super.releaseNativeStruct();
|
||||
}
|
||||
|
||||
public void init() {
|
||||
if (state == WolfCryptState.UNINITIALIZED) {
|
||||
initRng();
|
||||
state = WolfCryptState.INITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void free() {
|
||||
if (state == WolfCryptState.INITIALIZED) {
|
||||
freeRng();
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void generateBlock(ByteBuffer buffer) {
|
||||
init();
|
||||
|
||||
rngGenerateBlock(buffer, buffer.position(), buffer.remaining());
|
||||
buffer.position(buffer.position() + buffer.remaining());
|
||||
}
|
||||
|
||||
public void generateBlock(byte[] buffer, int offset, int length) {
|
||||
init();
|
||||
|
||||
rngGenerateBlock(buffer, offset, length);
|
||||
}
|
||||
|
||||
public void generateBlock(byte[] buffer) {
|
||||
generateBlock(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
public byte[] generateBlock(int length) {
|
||||
byte[] buffer = new byte[length];
|
||||
|
||||
generateBlock(buffer, 0, length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,229 @@
|
|||
/* Rsa.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Rsa implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Rsa extends NativeStruct {
|
||||
|
||||
private WolfCryptState state = WolfCryptState.UNINITIALIZED;
|
||||
private boolean hasPrivateKey = false;
|
||||
private Rng rng;
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
private native void wc_RsaPublicKeyDecodeRaw(ByteBuffer n, long nSize,
|
||||
ByteBuffer e, long eSize);
|
||||
|
||||
private native void wc_RsaPublicKeyDecodeRaw(byte[] n, long nSize, byte[] e,
|
||||
long eSize);
|
||||
|
||||
private native void RsaFlattenPublicKey(ByteBuffer n, ByteBuffer e);
|
||||
|
||||
private native void RsaFlattenPublicKey(byte[] n, long[] nSize, byte[] e,
|
||||
long[] eSize);
|
||||
|
||||
private native void MakeRsaKey(int size, long e, Rng rng);
|
||||
|
||||
private native void wc_InitRsaKey();
|
||||
|
||||
private native void wc_FreeRsaKey();
|
||||
|
||||
private native boolean wc_RsaSetRNG(Rng rng);
|
||||
|
||||
private native void wc_RsaPrivateKeyDecode(byte[] key);
|
||||
|
||||
private native void wc_RsaPrivateKeyDecodePKCS8(byte[] key);
|
||||
|
||||
private native void wc_RsaPublicKeyDecode(byte[] key);
|
||||
|
||||
private native int wc_RsaEncryptSize();
|
||||
|
||||
private native byte[] wc_RsaPublicEncrypt(byte[] data, Rng rng);
|
||||
|
||||
private native byte[] wc_RsaPrivateDecrypt(byte[] data);
|
||||
|
||||
private native byte[] wc_RsaSSL_Sign(byte[] data, Rng rng);
|
||||
|
||||
private native byte[] wc_RsaSSL_Verify(byte[] data);
|
||||
|
||||
public Rsa() {
|
||||
/* Lazy init for Fips compatibility */
|
||||
}
|
||||
|
||||
public Rsa(byte[] key) {
|
||||
decodePrivateKey(key);
|
||||
}
|
||||
|
||||
public Rsa(byte[] n, byte[] e) {
|
||||
decodeRawPublicKey(n, e);
|
||||
}
|
||||
|
||||
public void setRng(Rng rng) {
|
||||
init();
|
||||
|
||||
if (wc_RsaSetRNG(rng))
|
||||
this.rng = rng;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseNativeStruct() {
|
||||
free();
|
||||
|
||||
super.releaseNativeStruct();
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
if (state == WolfCryptState.UNINITIALIZED) {
|
||||
wc_InitRsaKey();
|
||||
state = WolfCryptState.INITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
protected void willSetKey() {
|
||||
init();
|
||||
|
||||
if (state != WolfCryptState.INITIALIZED)
|
||||
throw new IllegalStateException("Object already has a key.");
|
||||
}
|
||||
|
||||
protected void willUseKey(boolean priv) {
|
||||
if (priv && !hasPrivateKey)
|
||||
throw new IllegalStateException(
|
||||
"No available private key to perform the opperation.");
|
||||
|
||||
if (state != WolfCryptState.READY)
|
||||
throw new IllegalStateException(
|
||||
"No available key to perform the opperation.");
|
||||
}
|
||||
|
||||
protected void free() {
|
||||
if (state != WolfCryptState.UNINITIALIZED) {
|
||||
wc_FreeRsaKey();
|
||||
state = WolfCryptState.UNINITIALIZED;
|
||||
}
|
||||
}
|
||||
|
||||
public void makeKey(int size, long e, Rng rng) {
|
||||
willSetKey();
|
||||
|
||||
MakeRsaKey(size, e, rng);
|
||||
|
||||
state = WolfCryptState.READY;
|
||||
hasPrivateKey = true;
|
||||
}
|
||||
|
||||
public void decodePublicKey(byte[] key) {
|
||||
willSetKey();
|
||||
|
||||
wc_RsaPublicKeyDecode(key);
|
||||
state = WolfCryptState.READY;
|
||||
}
|
||||
|
||||
public void decodePrivateKey(byte[] key) {
|
||||
willSetKey();
|
||||
|
||||
wc_RsaPrivateKeyDecode(key);
|
||||
state = WolfCryptState.READY;
|
||||
hasPrivateKey = true;
|
||||
}
|
||||
|
||||
public void decodePrivateKeyPKCS8(byte[] key) {
|
||||
willSetKey();
|
||||
|
||||
wc_RsaPrivateKeyDecodePKCS8(key);
|
||||
|
||||
state = WolfCryptState.READY;
|
||||
hasPrivateKey = true;
|
||||
}
|
||||
|
||||
public void decodeRawPublicKey(byte[] n, byte[] e) {
|
||||
decodeRawPublicKey(n, n.length, e, e.length);
|
||||
}
|
||||
|
||||
public void decodeRawPublicKey(byte[] n, long nSize, byte[] e, long eSize) {
|
||||
willSetKey();
|
||||
|
||||
wc_RsaPublicKeyDecodeRaw(n, nSize, e, eSize);
|
||||
state = WolfCryptState.READY;
|
||||
}
|
||||
|
||||
public void decodeRawPublicKey(ByteBuffer n, ByteBuffer e) {
|
||||
decodeRawPublicKey(n, n.limit(), e, e.limit());
|
||||
}
|
||||
|
||||
public void decodeRawPublicKey(ByteBuffer n, long nSz, ByteBuffer e,
|
||||
long eSz) {
|
||||
willSetKey();
|
||||
|
||||
wc_RsaPublicKeyDecodeRaw(n, nSz, e, eSz);
|
||||
state = WolfCryptState.READY;
|
||||
}
|
||||
|
||||
public void exportRawPublicKey(byte[] n, long[] nSz, byte[] e, long[] eSz) {
|
||||
willUseKey(false);
|
||||
|
||||
RsaFlattenPublicKey(n, nSz, e, eSz);
|
||||
}
|
||||
|
||||
public void exportRawPublicKey(ByteBuffer n, ByteBuffer e) {
|
||||
willUseKey(false);
|
||||
|
||||
RsaFlattenPublicKey(n, e);
|
||||
}
|
||||
|
||||
public int getEncryptSize() {
|
||||
willUseKey(false);
|
||||
|
||||
return wc_RsaEncryptSize();
|
||||
}
|
||||
|
||||
public byte[] encrypt(byte[] plain, Rng rng) {
|
||||
willUseKey(false);
|
||||
|
||||
return wc_RsaPublicEncrypt(plain, rng);
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] ciphertext) {
|
||||
willUseKey(true);
|
||||
|
||||
return wc_RsaPrivateDecrypt(ciphertext);
|
||||
}
|
||||
|
||||
public byte[] sign(byte[] data, Rng rng) {
|
||||
willUseKey(true);
|
||||
|
||||
return wc_RsaSSL_Sign(data, rng);
|
||||
}
|
||||
|
||||
public byte[] verify(byte[] signature) {
|
||||
willUseKey(false);
|
||||
|
||||
return wc_RsaSSL_Verify(signature);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/* Sha.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Sha implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Sha extends MessageDigest {
|
||||
|
||||
public static final int TYPE = 1; /* hash type unique */
|
||||
public static final int DIGEST_SIZE = 20;
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
protected native void native_init();
|
||||
|
||||
protected native void native_update(ByteBuffer data, int offset, int len);
|
||||
|
||||
protected native void native_update(byte[] data, int offset, int len);
|
||||
|
||||
protected native void native_final(ByteBuffer hash, int offset);
|
||||
|
||||
protected native void native_final(byte[] hash);
|
||||
|
||||
public Sha() {
|
||||
init();
|
||||
}
|
||||
|
||||
public Sha(byte[] data) {
|
||||
init();
|
||||
update(data);
|
||||
}
|
||||
|
||||
public int digestSize() {
|
||||
return DIGEST_SIZE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
/* Sha256.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Sha256 implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Sha256 extends MessageDigest {
|
||||
|
||||
public static final int TYPE = 2; /* hash type unique */
|
||||
public static final int DIGEST_SIZE = 32;
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
protected native void native_init();
|
||||
|
||||
protected native void native_update(ByteBuffer data, int offset, int len);
|
||||
|
||||
protected native void native_update(byte[] data, int offset, int len);
|
||||
|
||||
protected native void native_final(ByteBuffer hash, int offset);
|
||||
|
||||
protected native void native_final(byte[] hash);
|
||||
|
||||
public Sha256() {
|
||||
init();
|
||||
}
|
||||
|
||||
public Sha256(byte[] data) {
|
||||
init();
|
||||
update(data);
|
||||
}
|
||||
|
||||
public int digestSize() {
|
||||
return DIGEST_SIZE;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/* Sha384.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Sha384 implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Sha384 extends MessageDigest {
|
||||
|
||||
public static final int TYPE = 5; /* hash type unique */
|
||||
public static final int DIGEST_SIZE = 48;
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
protected native void native_init();
|
||||
|
||||
protected native void native_update(ByteBuffer data, int offset, int len);
|
||||
|
||||
protected native void native_update(byte[] data, int offset, int len);
|
||||
|
||||
protected native void native_final(ByteBuffer hash, int offset);
|
||||
|
||||
protected native void native_final(byte[] hash);
|
||||
|
||||
public Sha384() {
|
||||
init();
|
||||
}
|
||||
|
||||
public Sha384(byte[] data) {
|
||||
init();
|
||||
update(data);
|
||||
}
|
||||
|
||||
public int digestSize() {
|
||||
return DIGEST_SIZE;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/* Sha512.java
|
||||
*
|
||||
* Copyright (C) 2006-2016 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
package com.wolfssl.wolfcrypt;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* Wrapper for the native WolfCrypt Sha512 implementation.
|
||||
*
|
||||
* @author Moisés Guimarães
|
||||
* @version 2.0, March 2017
|
||||
*/
|
||||
public class Sha512 extends MessageDigest {
|
||||
|
||||
public static final int TYPE = 4; /* hash type unique */
|
||||
public static final int DIGEST_SIZE = 64;
|
||||
|
||||
protected native long mallocNativeStruct() throws OutOfMemoryError;
|
||||
|
||||
protected native void native_init();
|
||||
|
||||
protected native void native_update(ByteBuffer data, int offset, int len);
|
||||
|
||||
protected native void native_update(byte[] data, int offset, int len);
|
||||
|
||||
protected native void native_final(ByteBuffer hash, int offset);
|
||||
|
||||
protected native void native_final(byte[] hash);
|
||||
|
||||
public Sha512() {
|
||||
init();
|
||||
}
|
||||
|
||||
public Sha512(byte[] data) {
|
||||
init();
|
||||
update(data);
|
||||
}
|
||||
|
||||
public int digestSize() {
|
||||
return DIGEST_SIZE;
|
||||
}
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ public class AesTest {
|
|||
Assume.assumeNoException(e);
|
||||
}
|
||||
catch (UnsatisfiedLinkError e) {
|
||||
System.out.println("Native code library failed to load.\n" + e.getError());
|
||||
System.out.println("Native code library failed to load.\n" + e.getMessage());
|
||||
Assume.assumeNoException(e);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue