Skip to content

Commit e3567cc

Browse files
author
liyongfei
committed
jce develop
1 parent 8dbf393 commit e3567cc

12 files changed

Lines changed: 134 additions & 83 deletions

File tree

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ lint/tmp/
9292

9393
/.idea/
9494
/target/
95-
/sm9enc.mpk
96-
/sm9EncryptData.txt
97-
/sm9sign.mpk
98-
/sm9SignData.txt
95+
/*.mpk
96+
/*.txt
97+
/*.pem

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ GmSSL-Java是采用JNI (Java Native Interface)方式实现的,也就是说所
1212

1313
GmSSL的项目组成主要包括C语言的本地代码、`src`目录下的Java类库代码、`examples`目录下面的例子代码。其中只有本地代码和`src`下面的Java类库代码会参与默认的编译,生成动态库和Jar包,而`examples`下的例子默认不编译也不进入Jar包。
1414

15-
GmSSL-Java提供一个包`org.gmssl`,其中包含如下密码算法类
15+
GmSSL-Java提供两种实现,基于JCE的实现和基于java本身的基础实现。
16+
JCE实现内容在包`org.gmssl.crypto`,可按照JCE调用方式完成各种算法功能,JCE调用可参考项目test目录下的JceTest类。因cipher属于Oracle java的受限“服务”,因此JCE调用前提必须使用[openJDK](https://jdk.java.net/archive/)
17+
基础实现内容在包`org.gmssl`,JDK来源不限制,其中包含如下密码算法类
1618

1719
* org.gmssl.Random
1820
* org.gmssl.Sm3
@@ -57,7 +59,7 @@ GmSSL-Java提供一个包`org.gmssl`,其中包含如下密码算法类
5759
## 编译和安装
5860

5961
### 编译安装GmSSL
60-
GmSSL-Java依赖GmSSL项目,在编译前需要先在系统上编译、安装并测试通过GmSSL库及工具。请在https://github.com/guanzhi/GmSSL 项目上下载最新发布的GmSSL代码,并完成编译、测试和安装。
62+
GmSSL-Java依赖GmSSL项目,在编译前需要先在系统上编译、安装并测试通过GmSSL库及工具。请在https://github.com/guanzhi/GmSSL 项目上下载同一版本的GmSSL代码,并完成编译、测试和安装。
6163

6264
### 通过Maven编译安装GmSSL-java
6365

@@ -84,7 +86,7 @@ mvn clean install
8486
最终会执行单元测试并在target目录下生成相应版本jar包。
8587

8688
## 使用
87-
在其他项目中使用GmSSL-java,只需在pom.xml中添加如下依赖:
89+
以上步骤操作完成后会在本地Maven仓库生成项目相应jar包,在其他项目中使用GmSSL-java,只需在pom.xml中添加如下依赖:
8890
```xml
8991
<dependency>
9092
<groupId>com.gmssl</groupId>

src/main/java/org/gmssl/crypto/Random.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,33 @@ public Random() {
2828

2929
@Override
3030
protected void engineSetSeed(byte[] seed) {
31-
31+
if (seed == null || seed.length == 0) {
32+
throw new IllegalArgumentException("Seed cannot be null or empty");
33+
}
34+
//rand_seed
35+
throw new GmSSLException("The current method is not supported.");
3236
}
3337

3438
@Override
3539
protected void engineNextBytes(byte[] bytes) {
40+
if (bytes == null) {
41+
throw new IllegalArgumentException("Output buffer cannot be null");
42+
}
3643
randBytes(bytes,0, bytes.length);
3744
}
3845

3946
@Override
4047
protected byte[] engineGenerateSeed(int numBytes) {
48+
if (numBytes <= 0) {
49+
throw new IllegalArgumentException("Number of bytes must be positive");
50+
}
4151
return randBytes(numBytes);
4252
}
4353

4454
public byte[] randBytes(int len) {
4555
byte[] out = new byte[len];
4656
if (GmSSLJNI.rand_bytes(out, 0, len) != 1) {
47-
throw new GmSSLException("");
57+
throw new GmSSLException("Failed to generate seed");
4858
}
4959
return out;
5060
}

src/main/java/org/gmssl/crypto/digest/SM3Hmac.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected void engineUpdate(byte input) {
7979

8080
@Override
8181
protected void engineUpdate(byte[] input, int offset, int len) {
82-
this.update(input, 0, len);
82+
this.update(input, offset, len);
8383
}
8484

8585
@Override

src/main/java/org/gmssl/crypto/symmetric/SM4CBC.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,21 @@ protected void init(int opmode, Key key, AlgorithmParameterSpec params, SecureRa
7171

7272
@Override
7373
protected byte[] processUpdate(byte[] input, int inputOffset, int inputLen) {
74-
byte[] tempByteArray=new byte[outputByteArray.length+inputLen];
75-
System.arraycopy(outputByteArray,0,tempByteArray,0,outputByteArray.length);
76-
outputByteArray=tempByteArray;
74+
int newOutputLength = BLOCK_SIZE + offset + inputLen;
75+
if (outputByteArray.length < newOutputLength) {
76+
int newSize = Math.max(outputByteArray.length * 3 / 2 + BLOCK_SIZE, newOutputLength);
77+
outputByteArray = Arrays.copyOf(outputByteArray, newSize);
78+
}
7779

7880
int outLen = processUpdate(input, inputOffset, inputLen, outputByteArray, offset);
79-
return Arrays.copyOfRange(outputByteArray,0,outLen);
81+
return Arrays.copyOfRange(outputByteArray,offset,offset + outLen);
8082
}
8183

8284
@Override
8385
protected int processUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset){
8486
int outLen = update(input, inputOffset, inputLen, output, outputOffset);
8587
this.offset+=outLen;
86-
return offset;
88+
return outLen;
8789
}
8890

8991
@Override
@@ -100,11 +102,11 @@ protected byte[] processBlock(byte[] input, int inputOffset, int inputLen) {
100102

101103
@Override
102104
protected int processBlock(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) {
105+
int outLen = 0;
103106
if(null!=input){
104-
this.processUpdate(input, inputOffset, inputLen, output, outputOffset);
107+
outLen=this.processUpdate(input, inputOffset, inputLen, output, outputOffset);
105108
}
106-
int outLen = doFinal(output, this.offset);
107-
outLen = outLen + this.offset;
109+
outLen += doFinal(output, this.offset);
108110
this.offset = 0;
109111
return outLen;
110112
}

src/main/java/org/gmssl/crypto/symmetric/SM4CTR.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,21 @@ protected byte[] engineGetIV() {
6969

7070
@Override
7171
protected byte[] processUpdate(byte[] input, int inputOffset, int inputLen) {
72-
byte[] tempByteArray=new byte[outputByteArray.length+inputLen];
73-
System.arraycopy(outputByteArray,0,tempByteArray,0,outputByteArray.length);
74-
outputByteArray=tempByteArray;
72+
int newOutputLength = BLOCK_SIZE + offset + inputLen;
73+
if (outputByteArray.length < newOutputLength) {
74+
int newSize = Math.max(outputByteArray.length * 3 / 2 + BLOCK_SIZE, newOutputLength);
75+
outputByteArray = Arrays.copyOf(outputByteArray, newSize);
76+
}
7577

7678
int outLen = processUpdate(input, inputOffset, inputLen, outputByteArray, offset);
77-
return Arrays.copyOfRange(outputByteArray,0,outLen);
79+
return Arrays.copyOfRange(outputByteArray,offset,offset + outLen);
7880
}
7981

8082
@Override
8183
protected int processUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) {
8284
int outLen = update(input, inputOffset, inputLen, output, outputOffset);
8385
this.offset += outLen;
84-
return offset;
86+
return outLen;
8587
}
8688

8789
@Override
@@ -98,11 +100,11 @@ protected byte[] processBlock(byte[] input, int inputOffset, int inputLen) throw
98100

99101
@Override
100102
protected int processBlock(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
101-
if(null!=input) {
102-
processUpdate(input, inputOffset, inputLen, output, outputOffset);
103+
int outLen = 0;
104+
if(null!=input){
105+
outLen=this.processUpdate(input, inputOffset, inputLen, output, outputOffset);
103106
}
104-
int outLen = doFinal(output, this.offset);
105-
outLen = outLen + this.offset;
107+
outLen += doFinal(output, this.offset);
106108
this.offset = 0;
107109
return outLen;
108110
}

src/main/java/org/gmssl/crypto/symmetric/SM4Cipher.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
package org.gmssl.crypto.symmetric;
1010

1111
import org.gmssl.GmSSLJNI;
12-
import org.gmssl.crypto.CipherPaddingEnum;
13-
import org.gmssl.crypto.PKCS7PaddingScheme;
1412

1513
import javax.crypto.*;
1614
import java.security.*;
@@ -21,7 +19,7 @@
2119
* @email 290836576@qq.com
2220
* @date 2024/07/27
2321
* @description
24-
*
22+
* CBC、CTR、ECB、GCM
2523
*/
2624
public class SM4Cipher extends CipherSpi {
2725

@@ -42,9 +40,7 @@ protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
4240

4341
@Override
4442
protected void engineSetPadding(String padding) throws NoSuchPaddingException {
45-
if(CipherPaddingEnum.PKCS7Padding.name().equals(padding)){
46-
this.sm4Engine.paddingScheme=new PKCS7PaddingScheme();
47-
}
43+
SM4CipherFactory.setPaddingScheme(sm4Engine, padding);
4844
}
4945

5046
@Override
@@ -54,7 +50,6 @@ protected int engineGetBlockSize() {
5450

5551
@Override
5652
protected int engineGetOutputSize(int inputLen) {
57-
// 输出大小根据模式和填充计算
5853
return 0;
5954
}
6055

src/main/java/org/gmssl/crypto/symmetric/SM4CipherFactory.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
package org.gmssl.crypto.symmetric;
1010

11+
import org.gmssl.crypto.CipherPaddingEnum;
12+
import org.gmssl.crypto.PKCS7PaddingScheme;
13+
1114
import java.security.NoSuchAlgorithmException;
1215

1316
/**
@@ -19,6 +22,11 @@
1922
*/
2023
public class SM4CipherFactory {
2124

25+
/**
26+
* Create an SM4 encryption and decryption engine that supports ECB, CBC, CTR, and GCM modes.
27+
* @param mode
28+
* @return
29+
*/
2230
public static SM4Engine createCipher(String mode){
2331
SM4Engine cipher;
2432
try {
@@ -44,4 +52,15 @@ public static SM4Engine createCipher(String mode){
4452
return cipher;
4553
}
4654

55+
/**
56+
* Currently, only the PKCS7Padding padding mode for ECB algorithm mode is set. Other algorithms (SM4/CBC/PKCS5Padding, SM4/CTR/NoPadding, SM4/GCM/NoPadding) have their default padding implemented.
57+
* @param engine
58+
* @param padding
59+
*/
60+
public static void setPaddingScheme(SM4Engine engine, String padding) {
61+
if(CipherPaddingEnum.PKCS7Padding.name().equals(padding)){
62+
engine.paddingScheme=new PKCS7PaddingScheme();
63+
}
64+
}
65+
4766
}

src/main/java/org/gmssl/crypto/symmetric/SM4GCM.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,21 @@ protected byte[] engineGetIV() {
8585

8686
@Override
8787
protected byte[] processUpdate(byte[] input, int inputOffset, int inputLen) {
88-
byte[] tempByteArray=new byte[outputByteArray.length+inputLen];
89-
System.arraycopy(outputByteArray,0,tempByteArray,0,outputByteArray.length);
90-
outputByteArray=tempByteArray;
88+
int newOutputLength = BLOCK_SIZE + offset + tLen + inputLen;
89+
if (outputByteArray.length < newOutputLength) {
90+
int newSize = Math.max(outputByteArray.length * 3 / 2 + BLOCK_SIZE + tLen, newOutputLength);
91+
outputByteArray = Arrays.copyOf(outputByteArray, newSize);
92+
}
9193

9294
int outLen = processUpdate(input, inputOffset, inputLen, outputByteArray, offset);
93-
return Arrays.copyOfRange(outputByteArray,0,outLen);
95+
return Arrays.copyOfRange(outputByteArray,offset,offset + outLen);
9496
}
9597

9698
@Override
9799
protected int processUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) {
98100
int outLen = update(input, inputOffset, inputLen, output, outputOffset);
99101
this.offset+=outLen;
100-
return offset;
102+
return outLen;
101103
}
102104

103105
@Override
@@ -114,11 +116,11 @@ protected byte[] processBlock(byte[] input, int inputOffset, int inputLen) throw
114116

115117
@Override
116118
protected int processBlock(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
117-
if(null!=input) {
118-
processUpdate(input, inputOffset, inputLen, output, outputOffset);
119+
int outLen = 0;
120+
if(null!=input){
121+
outLen=this.processUpdate(input, inputOffset, inputLen, output, outputOffset);
119122
}
120-
int outLen = doFinal(output, this.offset);
121-
outLen = outLen + this.offset;
123+
outLen += doFinal(output, this.offset);
122124
this.offset = 0;
123125
return outLen;
124126
}

src/main/java/org/gmssl/crypto/symmetric/ZucCipher.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ZucCipher extends CipherSpi {
4141

4242
private byte[] outputByteArray;
4343

44-
protected ZucCipher(){
44+
public ZucCipher(){
4545
ctx();
4646
}
4747

@@ -109,19 +109,21 @@ protected void engineInit(int opmode, Key key, AlgorithmParameters params, Secur
109109

110110
@Override
111111
protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
112-
byte[] tempByteArray=new byte[outputByteArray.length+inputLen];
113-
System.arraycopy(outputByteArray,0,tempByteArray,0,outputByteArray.length);
114-
outputByteArray=tempByteArray;
112+
int newOutputLength = BLOCK_SIZE + offset + inputLen;
113+
if (outputByteArray.length < newOutputLength) {
114+
int newSize = Math.max(outputByteArray.length * 3 / 2 + BLOCK_SIZE, newOutputLength);
115+
outputByteArray = Arrays.copyOf(outputByteArray, newSize);
116+
}
115117

116118
int outLen = engineUpdate(input, inputOffset, inputLen, outputByteArray, offset);
117-
return Arrays.copyOfRange(outputByteArray,0,outLen);
119+
return Arrays.copyOfRange(outputByteArray,offset,offset + outLen);
118120
}
119121

120122
@Override
121123
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset){
122124
int outLen = update(input, inputOffset, inputLen, output, outputOffset);
123125
this.offset+=outLen;
124-
return offset;
126+
return outLen;
125127
}
126128

127129
@Override
@@ -138,11 +140,11 @@ protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) thro
138140

139141
@Override
140142
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
141-
if(null!=input) {
142-
engineUpdate(input, inputOffset, inputLen, output, outputOffset);
143+
int outLen = 0;
144+
if(null!=input){
145+
outLen=this.engineUpdate(input, inputOffset, inputLen, output, outputOffset);
143146
}
144-
int outLen = doFinal(output, this.offset);
145-
outLen = outLen + this.offset;
147+
outLen += doFinal(output, this.offset);
146148
this.offset = 0;
147149
return outLen;
148150
}

0 commit comments

Comments
 (0)