Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gmssl</groupId>
<artifactId>GmSSL-Java</artifactId>
<version>2.1.0-develop</version>
<name>GmSSL-Java</name>
<description>GmSSL Java SDK</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<skipTests>false</skipTests>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>org.gmssl</groupId>
<artifactId>GmSSL-Java</artifactId>
<version>2.1.0-develop</version>
<name>GmSSL-Java</name>
<description>GmSSL Java SDK</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<skipTests>false</skipTests>

<cmake.compile.config>Debug</cmake.compile.config>
</properties>

<dependencies>
<dependency>
Expand Down Expand Up @@ -59,6 +61,8 @@
<winLibPath>C:/Program Files/GmSSL/lib</winLibPath>
<linuxIncludePath>/usr/local/include</linuxIncludePath>
<linuxLibPath>/usr/local/lib</linuxLibPath>

<libSubFolder>${cmake.compile.config}</libSubFolder>
</environmentVariables>
<options>
<!--
Expand All @@ -69,7 +73,6 @@
</options>
</configuration>
</execution>

<execution>
<id>cmake-compile</id>
<goals>
Expand Down Expand Up @@ -117,11 +120,18 @@
<fileset>
<directory>${basedir}/src/main/resources/lib</directory>
<includes>
<include>**/**.dll</include>
<include>*.dll</include>
<include>*.so</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
<fileset>
<directory>${basedir}</directory>
<includes>
<include>ROOTCA.pem</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
Expand All @@ -142,21 +152,28 @@
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/build</directory>
<directory>${project.build.directory}/build/${cmake.compile.config}</directory>
<includes>
<include>**/**.dll</include>
<include>*.dll</include>
<include>*.so</include>
</includes>
<targetPath>${basedir}/target/classes/lib</targetPath>
</resource>
<resource>
<directory>${project.build.directory}/build</directory>
<directory>${project.build.directory}/build/${cmake.compile.config}</directory>
<includes>
<include>**/**.dll</include>
<include>*.dll</include>
<include>*.so</include>
</includes>
<targetPath>${basedir}/src/main/resources/lib</targetPath>
</resource>
<resource>
<directory>${project.build.directory}/build</directory>
<includes>
<include>ROOTCA.pem</include>
</includes>
<targetPath>${basedir}</targetPath>
</resource>
</resources>
</configuration>
</execution>
Expand Down
6 changes: 4 additions & 2 deletions src/main/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ elseif(APPLE)
message(STATUS "->Now is Apple systems.")
elseif(UNIX)
message(STATUS "->Now is UNIX-like OS's.")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$ENV{libSubFolder})
add_library(gmssljni-native SHARED gmssljni.c)
target_link_libraries(gmssljni-native -L"$ENV{linuxLibPath}")
include_directories(jni)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
include_directories("$ENV{linuxIncludePath}")
target_link_libraries(gmssljni-native gmssl)
set_target_properties(gmssljni-native PROPERTIES OUTPUT_NAME gmssljni)
Expand All @@ -38,4 +40,4 @@ set(certfile
"53W+CKOPl2PknmWEq/lMhtn25HkCIQDaHDgWxWFtnCrBjH16/W3Ezn7/U/Vjo5xI\n"
"pDoiVhsLwg==\n"
"-----END CERTIFICATE-----\n")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/ROOTCA.pem ${certfile})
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/ROOTCA.pem ${certfile})
3 changes: 0 additions & 3 deletions src/main/java/org/gmssl/NativeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public synchronized static void load (String... libaray){
}else{
//load custom jni library,the default path is resources/lib of this project,load the corresponding class library according to the operating system.
String resourceLibPath=RESOURCELIB_PREFIXPATH;
if(osType().equals("win")){
resourceLibPath+="/Debug";
}
resourceLibPath+="/"+lib+"."+libExtension();
URL resourceUrl = NativeLoader.class.getClassLoader().getResource(resourceLibPath);
if(null !=resourceUrl){
Expand Down
144 changes: 55 additions & 89 deletions src/test/java/org/gmssl/Sm2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,144 +22,110 @@
public class Sm2Test {

Sm2Key sm2_key;
byte[] privateKeyInfo;
byte[] publicKeyInfo;
Sm2Key priKey,pubKey;
byte[] dgst;

@Before
public void beforeTest(){
sm2_key = new Sm2Key();
sm2_key.generateKey();

//byte[] privateKeyInfo=sm2_key.exportPrivateKeyInfoDer();
String privateKeyInfoHex="308193020100301306072a8648ce3d020106082a811ccf5501822d0479307702010104207fef3e258348873c47117c15093266e9dad99e131f1778e53d362b2b70649f85a00a06082a811ccf5501822da14403420004f94c0abb6cd00c6f0918cb9c54162213501d5cc278f5d3fcf63886f4e1dc6322b1b110e33a25216f258c4cce5fd52ab320d3b086ee5390f7387218c92578c3ab";
privateKeyInfo = HexUtil.hexToByte(privateKeyInfoHex);

//byte[] publicKeyInfo = sm2_key.exportPublicKeyInfoDer();
String publicKeyInfoHex = "3059301306072a8648ce3d020106082a811ccf5501822d03420004f94c0abb6cd00c6f0918cb9c54162213501d5cc278f5d3fcf63886f4e1dc6322b1b110e33a25216f258c4cce5fd52ab320d3b086ee5390f7387218c92578c3ab";
publicKeyInfo = HexUtil.hexToByte(publicKeyInfoHex);

priKey = new Sm2Key();
priKey.importPrivateKeyInfoDer(privateKeyInfo);
priKey.exportEncryptedPrivateKeyInfoPem("Password", "sm2.pem");
priKey.importEncryptedPrivateKeyInfoPem("Password", "sm2.pem");

pubKey = new Sm2Key();
pubKey.importPublicKeyInfoDer(publicKeyInfo);
pubKey.exportPublicKeyInfoPem("sm2.pem");
pubKey.importPublicKeyInfoPem("sm2.pem");

//byte[] dgst = rng.randBytes(Sm3.DIGEST_SIZE);
String dgstHex="372a28b963da9733515640f163dd017ae8544cafa78097d5765e4169348c030b";
dgst=HexUtil.hexToByte(dgstHex);

}

@Test
public void computeZTest(){
byte[] z = sm2_key.computeZ(Sm2Key.DEFAULT_ID);
byte[] z = pubKey.computeZ(Sm2Key.DEFAULT_ID);

String hexZ= HexUtil.byteToHex(z);
//System.out.println("z:"+hexZ);
Assert.assertNotNull("data is empty exception!",hexZ);
}

@Test
public void verifyTest(){
Random rng = new Random();
byte[] dgst = rng.randBytes(Sm3.DIGEST_SIZE);
byte[] sig = sm2_key.sign(dgst);
boolean verify_ret = sm2_key.verify(dgst, sig);
public void signTest(){
byte[] sig = priKey.sign(dgst);
String sigHex = HexUtil.byteToHex(sig);
//System.out.println("sigHex : "+sigHex);
Assert.assertNotNull("data is empty exception!",sig);
}

@Test
public void verifySignTest(){
String sigHex="3046022100c2a92338bf430b0bd1ed68ea9910168cbd6bbb6f8de0992e1350e894296273b1022100e4814ac9ea6dab86334f47b2de6122923a0abbb7ec0687a2a1974773eb9a9542";
byte[] sig=HexUtil.hexToByte(sigHex);

boolean verify_ret = pubKey.verify(dgst, sig);
//System.out.println("Verify result = " + verify_ret);
Assert.assertTrue("Verification of the signature failed!",verify_ret);
}

@Test
public void encryptTest(){
String testStr="gmssl";
byte[] ciphertext = sm2_key.encrypt(testStr.getBytes());

System.out.println("ciphertext : "+HexUtil.byteToHex(ciphertext));
byte[] ciphertext = pubKey.encrypt(testStr.getBytes());
//System.out.println("ciphertext : "+HexUtil.byteToHex(ciphertext));
Assert.assertNotNull("data is empty exception!",ciphertext);
}

//@Test
@Test
public void decryptTest(){
String ciphertextHex="306e022100ebf23c4b85c461a6fbe33cfb9d81b96edad432f07be2f293b7a7ea027ee65dfd02201b903f2694cc614fb445f2968861f1c017844db5162fe4e55477ec167fd04e78042088513b72d6f7fdb4532cf0684f5bb15c505263559fb38de1694cb2951b9adc2d0405f7b6bf4c18";
String ciphertextHex="306e022100d5d193d99876b6b2a2456356c09db06074aceb3ad6ae736d415d6988bdd4392902207ffbef363ae9584703d10b799609aff0fcb7a026b04aeec14021c9e12d22d2470420bbcc0a0bd07ffde6d0f5d5ee6e81eb47debbd9c6c0fca55107b1891cea29f742040526af292b75";
byte[] ciphertext = HexUtil.hexToByte(ciphertextHex);
byte[] plaintext = sm2_key.decrypt(ciphertext);
//TODO 加密和解密放到一起连续执行能够成功,拆带单独执行有问题

byte[] plaintext = priKey.decrypt(ciphertext);
String plaintextStr= new String(plaintext);
System.out.printf("Plaintext : "+plaintextStr);
//System.out.printf("Plaintext : "+plaintextStr);
Assert.assertEquals("The original value is not equal to the expected value after decryption!","gmssl",plaintextStr);
}

@Test
public void signatureTest(){
String signatureContentStr="gmssl";
Sm2Signature sign = new Sm2Signature(sm2_key, Sm2Key.DEFAULT_ID, true);

Sm2Signature sign = new Sm2Signature(priKey, Sm2Key.DEFAULT_ID, true);
sign.update(signatureContentStr.getBytes());
byte[] sig = sign.sign();
String sigHex = HexUtil.byteToHex(sig);
System.out.println("sig : "+sigHex);
//System.out.println("signatureContentHex : "+sigHex);
Assert.assertNotNull("data is empty exception!",sig);
}

//@Test
@Test
public void verifySignatureTest(){
String signatureContentStr = "gmssl";
String signatureContentHex = "304402207ad7865844e3e798525c084b83ec9d5318b4e3551190f65d4160ef82c280ccb802204097283db2652c74520b4f29bf80d70bf922115a2e148825613c2dd4603fd970";
String signatureContentHex = "3046022100cf526564d0964225f857856bc6ef181df5fcf1c87d630ccf6b992d4371772ed3022100915a309279e90ed00a02e84617991aaf1baa70586cc6cce395e52b7105bb73fa";
byte[] sig=HexUtil.hexToByte(signatureContentHex);
//TODO fix 签名和验签的方法放到一起连续执行能执行,拆开单独执行有问题
Sm2Signature verify = new Sm2Signature(sm2_key, Sm2Key.DEFAULT_ID, false);

Sm2Signature verify = new Sm2Signature(pubKey, Sm2Key.DEFAULT_ID, false);
verify.update(signatureContentStr.getBytes());
boolean verify_ret = verify.verify(sig);
//System.out.println("Verify result = " + verify_ret);
Assert.assertTrue("Verification of the signature failed!",verify_ret);
}


public static void main(String[] args) {
int i;

Sm2Key sm2_key = new Sm2Key();

sm2_key.generateKey();

byte[] privateKeyInfo = sm2_key.exportPrivateKeyInfoDer();
System.out.printf("PrivateKeyInfo: ");
for (i = 0; i < privateKeyInfo.length; i++) {
System.out.printf("%02x", privateKeyInfo[i]);
}
System.out.print("\n");

byte[] publicKeyInfo = sm2_key.exportPublicKeyInfoDer();
System.out.printf("PrivateKeyInfo: ");
for (i = 0; i < publicKeyInfo.length; i++) {
System.out.printf("%02x", publicKeyInfo[i]);
}
System.out.print("\n");


Sm2Key priKey = new Sm2Key();
priKey.importPrivateKeyInfoDer(privateKeyInfo);

Sm2Key pubKey = new Sm2Key();
pubKey.importPublicKeyInfoDer(publicKeyInfo);

priKey.exportEncryptedPrivateKeyInfoPem("Password", "sm2.pem");
pubKey.exportPublicKeyInfoPem("sm2pub.pem");

priKey.importEncryptedPrivateKeyInfoPem("Password", "sm2.pem");
pubKey.importPublicKeyInfoPem("sm2pub.pem");


byte[] z = pubKey.computeZ(Sm2Key.DEFAULT_ID);

System.out.printf("Z: ");
for (i = 0; i < z.length; i++) {
System.out.printf("%02x", z[i]);
}
System.out.print("\n");


Random rng = new Random();
byte[] dgst = rng.randBytes(Sm3.DIGEST_SIZE);
byte[] sig = priKey.sign(dgst);
boolean verify_ret = pubKey.verify(dgst, sig);
System.out.println("Verify result = " + verify_ret);

byte[] ciphertext = pubKey.encrypt("abc".getBytes());
byte[] plaintext = priKey.decrypt(ciphertext);
System.out.printf("Plaintext : ");
for (i = 0; i < plaintext.length; i++) {
System.out.printf("%02x", plaintext[i]);
}
System.out.print("\n");

Sm2Signature sign = new Sm2Signature(priKey, Sm2Key.DEFAULT_ID, true);
sign.update("abc".getBytes());
sig = sign.sign();

Sm2Signature verify = new Sm2Signature(pubKey, Sm2Key.DEFAULT_ID, false);
verify.update("abc".getBytes());
verify_ret = verify.verify(sig);
System.out.println("Verify result = " + verify_ret);

}

}
Loading