Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JCE Implementation of GmSSL #30

Closed
wants to merge 18 commits into from
Prev Previous commit
Next Next commit
sm4 sm9 jce develop
liyongfei committed Aug 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit e77f7d711eb2e3845e2262306d6c44c7a7786339

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/org/gmssl/crypto/symmetric/SM4.java
Original file line number Diff line number Diff line change
@@ -78,6 +78,8 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[]
return output.length;
}



private void init(){
if ((sm4_key = GmSSLJNI.sm4_key_new()) == 0) {
throw new GmSSLException("");
5 changes: 5 additions & 0 deletions src/main/java/org/gmssl/crypto/symmetric/SM4CBC.java
Original file line number Diff line number Diff line change
@@ -70,6 +70,11 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[]
return outLen;
}

@Override
protected void engineUpdateAAD(byte[] src, int offset, int len) {

}

private void ctx() {
if ((this.sm4_cbc_ctx = GmSSLJNI.sm4_cbc_ctx_new()) == 0) {
throw new GmSSLException("");
5 changes: 5 additions & 0 deletions src/main/java/org/gmssl/crypto/symmetric/SM4CTR.java
Original file line number Diff line number Diff line change
@@ -62,6 +62,11 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[]
return outLen;
}

@Override
protected void engineUpdateAAD(byte[] src, int offset, int len) {

}


public void ctx(){
if ((this.sm4_ctr_ctx = GmSSLJNI.sm4_ctr_ctx_new()) == 0) {
6 changes: 1 addition & 5 deletions src/main/java/org/gmssl/crypto/symmetric/SM4Cipher.java
Original file line number Diff line number Diff line change
@@ -96,11 +96,7 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[]

@Override
protected void engineUpdateAAD(byte[] src, int offset, int len) {
super.engineUpdateAAD(src, offset, len);
sm4Engine.engineUpdateAAD(src, offset, len);
}

@Override
protected void engineUpdateAAD(ByteBuffer src) {
super.engineUpdateAAD(src);
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/gmssl/crypto/symmetric/SM4ECB.java
Original file line number Diff line number Diff line change
@@ -32,4 +32,9 @@ protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] o
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
return 0;
}

@Override
protected void engineUpdateAAD(byte[] src, int offset, int len) {

}
}
3 changes: 3 additions & 0 deletions src/main/java/org/gmssl/crypto/symmetric/SM4Engine.java
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import java.nio.ByteBuffer;
import java.security.Key;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
@@ -28,6 +29,8 @@ public abstract class SM4Engine {

protected abstract int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException;

protected abstract void engineUpdateAAD(byte[] src, int offset, int len);

//String getAlgorithmName();

//int getBlockSize();
29 changes: 22 additions & 7 deletions src/main/java/org/gmssl/crypto/symmetric/SM4GCM.java
Original file line number Diff line number Diff line change
@@ -7,6 +7,9 @@
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.ByteBuffer;
import java.security.Key;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
@@ -30,6 +33,12 @@ public class SM4GCM extends SM4Engine {

private byte[] iv;

private byte[] aad;

private Key key;

private int tLen;

private int offset;

public SM4GCM(){
@@ -39,15 +48,13 @@ public SM4GCM(){

@Override
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) {
if (!(params instanceof AadAlgorithmParameters)){
throw new GmSSLException("need the AadAlgorithmParameters parameter");
if (!(params instanceof GCMParameterSpec)) {
throw new GmSSLException("need the GCMParameterSpec parameter");
}
this.iv = ((AadAlgorithmParameters) params).getIV();
int tLen = ((AadAlgorithmParameters) params).getTLen();
byte[] aad = ((AadAlgorithmParameters) params).getAad();

this.key = key;
this.iv = ((GCMParameterSpec) params).getIV();
this.tLen = ((GCMParameterSpec) params).getTLen();
this.do_encrypt = (opmode == Cipher.ENCRYPT_MODE);
init(key.getEncoded(), iv,aad, tLen, do_encrypt);
}

@Override
@@ -71,6 +78,14 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[]
return outLen;
}

@Override
protected void engineUpdateAAD(byte[] src, int offset, int len) {
this.aad = new byte[len];
System.arraycopy(src, offset, this.aad, 0, len);

init(key.getEncoded(), iv,aad,tLen, do_encrypt);
}

private void ctx(){
if ((this.sm4_gcm_ctx = GmSSLJNI.sm4_gcm_ctx_new()) == 0) {
throw new GmSSLException("");
23 changes: 10 additions & 13 deletions src/test/java/org/gmssl/JceTest.java
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
import org.junit.Test;

import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
@@ -131,8 +132,8 @@ public static void SM3Test() {
}
}

public static void SM4Test() {
try {
@Test
public void SM4_CBC_test() throws Exception{
String text="Hello, GmSSL";
SecureRandom secureRandom = SecureRandom.getInstance("Random", "GmSSL");
byte[] randomBytes = new byte[32];
@@ -170,11 +171,6 @@ public static void SM4Test() {
byte[] plaintext2 =Arrays.copyOfRange(plaintext1,0,decryptedLen);
String plaintextStr=new String(plaintext2);
System.out.println("plaintext: " + plaintextStr);
} catch (Exception e) {
e.printStackTrace();
}


}

@Test
@@ -201,20 +197,21 @@ public void SM4_CTR_test() throws Exception{

@Test
public void SM4_GCM_test() throws Exception {
String text="Hello, GmSSL";
SecureRandom secureRandom = SecureRandom.getInstance("Random", "GmSSL");
Cipher sm4Cipher = Cipher.getInstance("SM4/GCM/ZeroPadding", "GmSSL");
byte[] key = secureRandom.generateSeed(SM4CTR.KEY_SIZE);
byte[] iv = secureRandom.generateSeed(SM4CTR.IV_SIZE);
byte[] key = secureRandom.generateSeed(SM4GCM.KEY_SIZE);
byte[] iv = secureRandom.generateSeed(SM4GCM.DEFAULT_IV_SIZE);
byte[] aad = "Hello: ".getBytes();
sm4Cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "SM4"), new AadAlgorithmParameters(SM4GCM.MAX_TAG_SIZE,iv,aad));
sm4Cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "SM4"), new GCMParameterSpec(SM4GCM.MAX_TAG_SIZE,iv));
sm4Cipher.updateAAD(aad);
//TODO fix aad不需要专门建立参数
byte[] ciphertext = new byte[64];
int cipherlen = sm4Cipher.doFinal("abc".getBytes(), 0, 3, ciphertext, 0);
int cipherlen = sm4Cipher.doFinal(text.getBytes(), 0, text.getBytes().length, ciphertext, 0);
System.out.println("Ciphertext: " + byteToHex(ciphertext));

byte[] plaintext = new byte[64];
sm4Cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "SM4"), new AadAlgorithmParameters(SM4GCM.MAX_TAG_SIZE,iv,aad));
sm4Cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "SM4"), new GCMParameterSpec(SM4GCM.MAX_TAG_SIZE,iv));
sm4Cipher.updateAAD(aad);
int plainlen =sm4Cipher.doFinal(ciphertext, 0, cipherlen, plaintext, 0);
byte[] plaintext1 = Arrays.copyOfRange(plaintext,0,plainlen);
System.out.println("plaintext: " + new String(plaintext1));