remove jni

This commit is contained in:
Zhi Guan
2017-01-18 19:34:00 +08:00
parent fee0874734
commit c6be773b92
8 changed files with 0 additions and 2911 deletions

View File

@@ -1,726 +0,0 @@
/* jni/GmSSL.c */
/* ====================================================================
* Copyright (c) 2015 - 2016 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include "GmSSL.h"
#define PRINT_ERROR() \
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__)
static int alg2pktype(const char *alg)
{
return 0;
}
JNIEXPORT
jint JNICALL JNI_onload(JavaVM *jvm, void *reserved)
{
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
return JNI_VERSION_1_2;
}
JNIEXPORT
void JNICALL JNI_onunload(JavaVM *vm, void *reserved)
{
ERR_free_strings();
EVP_cleanup();
}
JNIEXPORT
jbyteArray JNICALL Java_GmSSL_symmetricEncrypt(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in, jbyteArray key, jbyteArray iv)
{
jbyteArray ret = NULL;
const char *alg = NULL;
unsigned char *inbuf = NULL;
unsigned char *keybuf = NULL;
unsigned char *ivbuf = NULL;
unsigned char *outbuf = NULL;
size_t inlen, keylen, ivlen, outlen;
unsigned char *p;
int len;
const EVP_CIPHER *cipher;
EVP_CIPHER_CTX *cctx = NULL;
if (!(alg = (*env)->GetStringUTFChars(env, algor, 0))) {
PRINT_ERROR();
goto end;
}
if (!(cipher = EVP_get_cipherbyname(alg))) {
PRINT_ERROR();
goto end;
}
if (!(inbuf = (unsigned char *)(*env)->GetByteArrayElements(env, in, 0))) {
PRINT_ERROR();
goto end;
}
inlen = (size_t)(*env)->GetArrayLength(env, in);
if (inlen <= 0) {
PRINT_ERROR();
goto end;
}
if (!(keybuf = (unsigned char *)(*env)->GetByteArrayElements(env, key, 0))) {
PRINT_ERROR();
goto end;
}
keylen = (size_t)(*env)->GetArrayLength(env, key);
if (keylen < EVP_CIPHER_key_length(cipher)) {
PRINT_ERROR();
goto end;
}
if (!(ivbuf = (unsigned char *)(*env)->GetByteArrayElements(env, iv, 0))) {
PRINT_ERROR();
goto end;
}
ivlen = (size_t)(*env)->GetArrayLength(env, iv);
if (ivlen != EVP_CIPHER_iv_length(cipher)) {
PRINT_ERROR();
goto end;
}
outlen = inlen + EVP_CIPHER_block_size(cipher) * 2;
if (!(outbuf = malloc(outlen))) {
PRINT_ERROR();
goto end;
}
bzero(outbuf, outlen);
if (!(cctx = EVP_CIPHER_CTX_new())) {
PRINT_ERROR();
goto end;
}
if (!EVP_EncryptInit_ex(cctx, cipher, NULL, keybuf, ivbuf)) {
PRINT_ERROR();
goto end;
}
p = outbuf;
len = outlen;
if (!EVP_EncryptUpdate(cctx, p, &len, inbuf, inlen)) {
PRINT_ERROR();
goto end;
}
p += len;
len = outlen - len;
if (!EVP_EncryptFinal_ex(cctx, p, &len)) {
PRINT_ERROR();
goto end;
}
p += len;
if (!(ret = (*env)->NewByteArray(env, outlen))) {
PRINT_ERROR();
goto end;
}
outlen = p - outbuf;
(*env)->SetByteArrayRegion(env, ret, 0, outlen, (jbyte *)outbuf);
end:
if (alg) (*env)->ReleaseStringUTFChars(env, algor, alg);
if (inbuf) (*env)->ReleaseByteArrayElements(env, in, (jbyte *)inbuf, JNI_ABORT);
if (keybuf) (*env)->ReleaseByteArrayElements(env, key, (jbyte *)keybuf, JNI_ABORT);
if (ivbuf) (*env)->ReleaseByteArrayElements(env, iv, (jbyte *)ivbuf, JNI_ABORT);
if (outbuf) free(outbuf);
EVP_CIPHER_CTX_free(cctx);
return ret;
}
JNIEXPORT
jbyteArray JNICALL Java_GmSSL_symmetricDecrypt(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in, jbyteArray key, jbyteArray iv)
{
jbyteArray ret = NULL;
const char *alg = NULL;
unsigned char *inbuf = NULL;
unsigned char *keybuf = NULL;
unsigned char *ivbuf = NULL;
unsigned char *outbuf = NULL;
size_t inlen, keylen, ivlen, outlen;
unsigned char *p;
int len;
const EVP_CIPHER *cipher;
EVP_CIPHER_CTX *cctx = NULL;
if (!(alg = (*env)->GetStringUTFChars(env, algor, 0))) {
PRINT_ERROR();
goto end;
}
if (!(cipher = EVP_get_cipherbyname(alg))) {
PRINT_ERROR();
goto end;
}
if (!(inbuf = (unsigned char *)(*env)->GetByteArrayElements(env, in, 0))) {
PRINT_ERROR();
goto end;
}
inlen = (size_t)(*env)->GetArrayLength(env, in);
if (inlen <= 0) {
PRINT_ERROR();
goto end;
}
if (!(keybuf = (unsigned char *)(*env)->GetByteArrayElements(env, key, 0))) {
PRINT_ERROR();
goto end;
}
keylen = (size_t)(*env)->GetArrayLength(env, key);
if (keylen < EVP_CIPHER_key_length(cipher)) {
PRINT_ERROR();
goto end;
}
if (!(ivbuf = (unsigned char *)(*env)->GetByteArrayElements(env, iv, 0))) {
PRINT_ERROR();
goto end;
}
ivlen = (size_t)(*env)->GetArrayLength(env, iv);
if (ivlen != EVP_CIPHER_iv_length(cipher)) {
PRINT_ERROR();
goto end;
}
outlen = inlen + EVP_CIPHER_block_size(cipher) * 2;
if (!(outbuf = malloc(outlen))) {
PRINT_ERROR();
goto end;
}
bzero(outbuf, outlen);
if (!(cctx = EVP_CIPHER_CTX_new())) {
PRINT_ERROR();
goto end;
}
if (!EVP_DecryptInit_ex(cctx, cipher, NULL, keybuf, ivbuf)) {
PRINT_ERROR();
goto end;
}
p = outbuf;
len = outlen;
if (!EVP_DecryptUpdate(cctx, p, &len, inbuf, inlen)) {
PRINT_ERROR();
goto end;
}
p += len;
len = outlen - len;
if (!EVP_DecryptFinal_ex(cctx, p, &len)) {
PRINT_ERROR();
goto end;
}
p += len;
if (!(ret = (*env)->NewByteArray(env, outlen))) {
PRINT_ERROR();
goto end;
}
outlen = p - outbuf;
(*env)->SetByteArrayRegion(env, ret, 0, outlen, (jbyte *)outbuf);
end:
if (alg) (*env)->ReleaseStringUTFChars(env, algor, alg);
if (inbuf) (*env)->ReleaseByteArrayElements(env, in, (jbyte *)inbuf, JNI_ABORT);
if (keybuf) (*env)->ReleaseByteArrayElements(env, key, (jbyte *)keybuf, JNI_ABORT);
if (ivbuf) (*env)->ReleaseByteArrayElements(env, iv, (jbyte *)ivbuf, JNI_ABORT);
if (outbuf) free(outbuf);
EVP_CIPHER_CTX_free(cctx);
return ret;
}
JNIEXPORT
jbyteArray JNICALL Java_GmSSL_digest(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in)
{
jbyteArray ret = NULL;
const char *alg = NULL;
unsigned char *inbuf = NULL;
unsigned char *outbuf = NULL;
size_t inlen, outlen;
unsigned int len;
const EVP_MD *md;
if (!(alg = (*env)->GetStringUTFChars(env, algor, 0))) {
PRINT_ERROR();
goto end;
}
if (!(md = EVP_get_digestbyname(alg))) {
PRINT_ERROR();
goto end;
}
if (!(inbuf = (unsigned char *)(*env)->GetByteArrayElements(env, in, 0))) {
PRINT_ERROR();
goto end;
}
inlen = (size_t)(*env)->GetArrayLength(env, in);
if (inlen <= 0) {
PRINT_ERROR();
goto end;
}
outlen = EVP_MD_size(md);
if (!(outbuf = malloc(outlen))) {
PRINT_ERROR();
goto end;
}
bzero(outbuf, outlen);
if (!EVP_Digest(inbuf, inlen, outbuf, &len, md, NULL)) {
PRINT_ERROR();
goto end;
}
if (!(ret = (*env)->NewByteArray(env, outlen))) {
PRINT_ERROR();
goto end;
}
outlen = len;
(*env)->SetByteArrayRegion(env, ret, 0, outlen, (jbyte *)outbuf);
end:
if (alg) (*env)->ReleaseStringUTFChars(env, algor, alg);
if (inbuf) (*env)->ReleaseByteArrayElements(env, in, (jbyte *)inbuf, JNI_ABORT);
if (outbuf) free(outbuf);
return ret;
}
JNIEXPORT
jbyteArray JNICALL Java_GmSSL_mac(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in, jbyteArray key)
{
jbyteArray ret = NULL;
return ret;
}
JNIEXPORT
jbyteArray JNICALL Java_GmSSL_sign(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in, jbyteArray key)
{
jbyteArray ret = NULL;
const char *alg = NULL;
unsigned char *inbuf = NULL;
unsigned char *keybuf = NULL;
unsigned char *outbuf = NULL;
size_t inlen, keylen, outlen;
const unsigned char *p;
int type;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *pkctx = NULL;
if (!(alg = (*env)->GetStringUTFChars(env, algor, 0))) {
PRINT_ERROR();
goto end;
}
if ((type = alg2pktype(alg)) == NID_undef) {
PRINT_ERROR();
goto end;
}
if (!(inbuf = (unsigned char *)(*env)->GetByteArrayElements(env, in, 0))) {
PRINT_ERROR();
goto end;
}
inlen = (size_t)(*env)->GetArrayLength(env, in);
if (inlen < SHA_DIGEST_LENGTH) {
PRINT_ERROR();
goto end;
}
if (!(keybuf = (unsigned char *)(*env)->GetByteArrayElements(env, key, 0))) {
PRINT_ERROR();
goto end;
}
keylen = (size_t)(*env)->GetArrayLength(env, key);
p = keybuf;
if (!(pkey = d2i_AutoPrivateKey(NULL, &p, keylen))) {
PRINT_ERROR();
goto end;
}
outlen = EVP_PKEY_size(pkey);
if (!(outbuf = malloc(outlen))) {
PRINT_ERROR();
goto end;
}
if (!(pkctx = EVP_PKEY_CTX_new(pkey, NULL))) {
PRINT_ERROR();
goto end;
}
if (!EVP_PKEY_sign_init(pkctx)) {
PRINT_ERROR();
goto end;
}
if (EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) {
if (!EVP_PKEY_CTX_ctrl_str(pkctx, "ec_sign_algor", alg)) {
PRINT_ERROR();
goto end;
}
}
if (!EVP_PKEY_sign(pkctx, outbuf, &outlen, inbuf, inlen)) {
PRINT_ERROR();
goto end;
}
if (!(ret = (*env)->NewByteArray(env, outlen))) {
PRINT_ERROR();
goto end;
}
(*env)->SetByteArrayRegion(env, ret, 0, outlen, (jbyte *)outbuf);
end:
if (alg) (*env)->ReleaseStringUTFChars(env, algor, alg);
if (inbuf) (*env)->ReleaseByteArrayElements(env, in, (jbyte *)inbuf, JNI_ABORT);
if (keybuf) (*env)->ReleaseByteArrayElements(env, key, (jbyte *)keybuf, JNI_ABORT);
if (outbuf) free(outbuf);
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pkctx);
return ret;
}
JNIEXPORT
jint JNICALL Java_GmSSL_verify(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in, jbyteArray signature,
jbyteArray key)
{
jint ret = 0;
const char *alg = NULL;
unsigned char *inbuf = NULL;
unsigned char *sigbuf = NULL;
unsigned char *keybuf = NULL;
size_t inlen, siglen, keylen;
const unsigned char *p;
int type;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *pkctx = NULL;
if (!(alg = (*env)->GetStringUTFChars(env, algor, 0))) {
PRINT_ERROR();
goto end;
}
if ((type = alg2pktype(alg)) == NID_undef) {
PRINT_ERROR();
goto end;
}
if (!(inbuf = (unsigned char *)(*env)->GetByteArrayElements(env, in, 0))) {
PRINT_ERROR();
goto end;
}
inlen = (size_t)(*env)->GetArrayLength(env, in);
if (inlen < SHA_DIGEST_LENGTH) {
PRINT_ERROR();
goto end;
}
if (!(sigbuf = (unsigned char *)(*env)->GetByteArrayElements(env, signature, 0))) {
PRINT_ERROR();
goto end;
}
siglen = (size_t)(*env)->GetArrayLength(env, signature);
if (siglen < 40) {
PRINT_ERROR();
goto end;
}
if (!(keybuf = (unsigned char *)(*env)->GetByteArrayElements(env, key, 0))) {
PRINT_ERROR();
goto end;
}
keylen = (size_t)(*env)->GetArrayLength(env, key);
p = keybuf;
if (!(pkey = d2i_PublicKey(type, NULL, &p, keylen))) {
PRINT_ERROR();
goto end;
}
if (!(pkctx = EVP_PKEY_CTX_new(pkey, NULL))) {
PRINT_ERROR();
goto end;
}
if (!EVP_PKEY_verify_init(pkctx)) {
PRINT_ERROR();
goto end;
}
if (EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) {
if (!EVP_PKEY_CTX_ctrl_str(pkctx, "ec_sign_algor", alg)) {
PRINT_ERROR();
goto end;
}
}
if ((ret = EVP_PKEY_verify(pkctx, sigbuf, siglen, inbuf, inlen)) != 1) {
PRINT_ERROR();
goto end;
}
end:
if (alg) (*env)->ReleaseStringUTFChars(env, algor, alg);
if (inbuf) (*env)->ReleaseByteArrayElements(env, in, (jbyte *)inbuf, JNI_ABORT);
if (sigbuf) (*env)->ReleaseByteArrayElements(env, signature, (jbyte *)sigbuf, JNI_ABORT);
if (keybuf) (*env)->ReleaseByteArrayElements(env, key, (jbyte *)keybuf, JNI_ABORT);
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pkctx);
return ret;
}
JNIEXPORT
jbyteArray JNICALL Java_GmSSL_publicKeyEncrypt(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in, jbyteArray key)
{
jbyteArray ret = NULL;
const char *alg = NULL;
unsigned char *inbuf = NULL;
unsigned char *keybuf = NULL;
unsigned char *outbuf = NULL;
size_t inlen, keylen, outlen;
const unsigned char *p;
int type;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *pkctx = NULL;
if (!(alg = (*env)->GetStringUTFChars(env, algor, 0))) {
PRINT_ERROR();
goto end;
}
if ((type = alg2pktype(alg)) == NID_undef) {
PRINT_ERROR();
goto end;
}
if (!(inbuf = (unsigned char *)(*env)->GetByteArrayElements(env, in, 0))) {
PRINT_ERROR();
goto end;
}
inlen = (size_t)(*env)->GetArrayLength(env, in);
if (inlen <= 0) {
PRINT_ERROR();
goto end;
}
if (!(keybuf = (unsigned char *)(*env)->GetByteArrayElements(env, key, 0))) {
PRINT_ERROR();
goto end;
}
keylen = (size_t)(*env)->GetArrayLength(env, key);
p = keybuf;
if (!(pkey = d2i_PublicKey(type, NULL, &p, keylen))) {
PRINT_ERROR();
goto end;
}
/* we can not get ciphertext length from plaintext
* so malloc the max buffer
*/
outlen = inlen + 2048;
if (!(outbuf = malloc(outlen))) {
PRINT_ERROR();
goto end;
}
if (!(pkctx = EVP_PKEY_CTX_new(pkey, NULL))) {
PRINT_ERROR();
goto end;
}
if (!EVP_PKEY_encrypt_init(pkctx)) {
PRINT_ERROR();
goto end;
}
if (EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) {
if (!EVP_PKEY_CTX_ctrl_str(pkctx, "ec_encrypt_algor", alg)) {
PRINT_ERROR();
goto end;
}
}
if (!EVP_PKEY_encrypt(pkctx, outbuf, &outlen, inbuf, inlen)) {
PRINT_ERROR();
goto end;
}
if (!(ret = (*env)->NewByteArray(env, outlen))) {
PRINT_ERROR();
goto end;
}
(*env)->SetByteArrayRegion(env, ret, 0, outlen, (jbyte *)outbuf);
end:
if (alg) (*env)->ReleaseStringUTFChars(env, algor, alg);
if (inbuf) (*env)->ReleaseByteArrayElements(env, in, (jbyte *)inbuf, JNI_ABORT);
if (keybuf) (*env)->ReleaseByteArrayElements(env, key, (jbyte *)keybuf, JNI_ABORT);
if (outbuf) free(outbuf);
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pkctx);
return ret;
}
JNIEXPORT
jbyteArray JNICALL Java_GmSSL_publicKeyDecrypt(JNIEnv *env, jobject this,
jstring algor, jint flag, jbyteArray in, jbyteArray key)
{
jbyteArray ret = NULL;
const char *alg = NULL;
unsigned char *inbuf = NULL;
unsigned char *keybuf = NULL;
unsigned char *outbuf = NULL;
size_t inlen, keylen, outlen;
const unsigned char *p;
int type;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *pkctx = NULL;
if (!(alg = (*env)->GetStringUTFChars(env, algor, 0))) {
PRINT_ERROR();
goto end;
}
if ((type = alg2pktype(alg)) == NID_undef) {
PRINT_ERROR();
goto end;
}
if (!(inbuf = (unsigned char *)(*env)->GetByteArrayElements(env, in, 0))) {
PRINT_ERROR();
goto end;
}
inlen = (size_t)(*env)->GetArrayLength(env, in);
if (inlen <= 0) {
PRINT_ERROR();
goto end;
}
if (!(keybuf = (unsigned char *)(*env)->GetByteArrayElements(env, key, 0))) {
PRINT_ERROR();
goto end;
}
keylen = (size_t)(*env)->GetArrayLength(env, key);
p = keybuf;
if (!(pkey = d2i_AutoPrivateKey(NULL, &p, keylen))) {
PRINT_ERROR();
goto end;
}
outlen = inlen;
if (!(outbuf = malloc(outlen))) {
PRINT_ERROR();
goto end;
}
if (!(pkctx = EVP_PKEY_CTX_new(pkey, NULL))) {
PRINT_ERROR();
goto end;
}
if (!EVP_PKEY_decrypt_init(pkctx)) {
PRINT_ERROR();
goto end;
}
if (EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) {
if (!EVP_PKEY_CTX_ctrl_str(pkctx, "ec_encrypt_algor", alg)) {
PRINT_ERROR();
goto end;
}
}
if (!EVP_PKEY_decrypt(pkctx, outbuf, &outlen, inbuf, inlen)) {
PRINT_ERROR();
goto end;
}
if (!(ret = (*env)->NewByteArray(env, outlen))) {
PRINT_ERROR();
goto end;
}
(*env)->SetByteArrayRegion(env, ret, 0, outlen, (jbyte *)outbuf);
end:
if (alg) (*env)->ReleaseStringUTFChars(env, algor, alg);
if (inbuf) (*env)->ReleaseByteArrayElements(env, in, (jbyte *)inbuf, JNI_ABORT);
if (keybuf) (*env)->ReleaseByteArrayElements(env, key, (jbyte *)keybuf, JNI_ABORT);
if (outbuf) free(outbuf);
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pkctx);
return ret;
}

Binary file not shown.

View File

@@ -1,77 +0,0 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class GmSSL */
#ifndef _Included_GmSSL
#define _Included_GmSSL
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: GmSSL
* Method: symmetricEncrypt
* Signature: (Ljava/lang/String;I[B[B[B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_GmSSL_symmetricEncrypt
(JNIEnv *, jobject, jstring, jint, jbyteArray, jbyteArray, jbyteArray);
/*
* Class: GmSSL
* Method: symmetricDecrypt
* Signature: (Ljava/lang/String;I[B[B[B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_GmSSL_symmetricDecrypt
(JNIEnv *, jobject, jstring, jint, jbyteArray, jbyteArray, jbyteArray);
/*
* Class: GmSSL
* Method: digest
* Signature: (Ljava/lang/String;I[B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_GmSSL_digest
(JNIEnv *, jobject, jstring, jint, jbyteArray);
/*
* Class: GmSSL
* Method: mac
* Signature: (Ljava/lang/String;I[B[B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_GmSSL_mac
(JNIEnv *, jobject, jstring, jint, jbyteArray, jbyteArray);
/*
* Class: GmSSL
* Method: sign
* Signature: (Ljava/lang/String;I[B[B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_GmSSL_sign
(JNIEnv *, jobject, jstring, jint, jbyteArray, jbyteArray);
/*
* Class: GmSSL
* Method: verify
* Signature: (Ljava/lang/String;I[B[B[B)I
*/
JNIEXPORT jint JNICALL Java_GmSSL_verify
(JNIEnv *, jobject, jstring, jint, jbyteArray, jbyteArray, jbyteArray);
/*
* Class: GmSSL
* Method: publicKeyEncrypt
* Signature: (Ljava/lang/String;I[B[B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_GmSSL_publicKeyEncrypt
(JNIEnv *, jobject, jstring, jint, jbyteArray, jbyteArray);
/*
* Class: GmSSL
* Method: publicKeyDecrypt
* Signature: (Ljava/lang/String;I[B[B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_GmSSL_publicKeyDecrypt
(JNIEnv *, jobject, jstring, jint, jbyteArray, jbyteArray);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,88 +0,0 @@
/* jni/GmSSL.java */
/* ====================================================================
* Copyright (c) 2015 - 2016 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
public class GmSSL {
public native byte [] symmetricEncrypt(String algor, int flag, byte [] in, byte [] key, byte [] iv);
public native byte [] symmetricDecrypt(String algor, int flag, byte [] in, byte [] key, byte [] iv);
public native byte [] digest(String algor, int flag, byte [] data);
public native byte [] mac(String algor, int flag, byte [] data, byte [] key);
public native byte [] sign(String algor, int flag, byte [] digest, byte [] privateKey);
public native int verify(String algor, int flag, byte [] digest, byte [] signature, byte [] publicKey);
public native byte [] publicKeyEncrypt(String algor, int flag, byte [] in, byte [] publicKey);
public native byte [] publicKeyDecrypt(String algor, int falg, byte [] in, byte [] privateKey);
public static void main(String[] args) {
final GmSSL gmssl = new GmSSL();
final byte [] key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
final byte [] iv = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
final byte [] data = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
/* here we need tests */
final byte [] ciphertext = gmssl.symmetricEncrypt("SMS4-CBC", 0, data, key, iv);
}
static {
System.loadLibrary("gmssl");
}
}

View File

@@ -1,24 +0,0 @@
# maybe a config is required to cross platform
all: libgmssl.jnilib
libgmssl.jnilib: GmSSL.h
gcc -shared -fPIC -Wall -I./jni/ GmSSL.c ../libcrypto.a -o libgmssl.jnilib
GmSSL.h: GmSSL.class
javah -jni GmSSL
GmSSL.class:
javac GmSSL.java
clean:
rm -f GmSSL.h
rm -f *.class
rm -f *.jnilib
rm -f *.o
rm -f a.out
test:
java -Djava.library.path=. GmSSL

View File

@@ -1,12 +0,0 @@
## GmSSL Java Wrapper
Thi purpose of this module is to provide a simple Java API to access GmSSL
crypto library. To be simple, no key schedule or context is used. So the
functions will not be very efficient for processing large files or stream data.
And this module is not intend to be integrated with Java crypto frameworks such
as JCE.
The implementation is based on the Java Native Interface (JNI). The JNI header
files are also included, but you can replace them with version from you own
compiling environment.

File diff suppressed because it is too large Load Diff

View File

@@ -1,23 +0,0 @@
/*
* @(#)jni_md.h 1.19 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
#ifndef _JAVASOFT_JNI_MD_H_
#define _JAVASOFT_JNI_MD_H_
#define JNIEXPORT __attribute__((visibility("default")))
#define JNIIMPORT
#define JNICALL
#if __LP64__
typedef int jint;
#else
typedef long jint;
#endif
typedef long long jlong;
typedef signed char jbyte;
#endif /* !_JAVASOFT_JNI_MD_H_ */