-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm4.java
More file actions
61 lines (51 loc) · 1.45 KB
/
Sm4.java
File metadata and controls
61 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package org.gmssl;
public class Sm4 {
public final static int KEY_SIZE = GmSSLJNI.SM4_KEY_SIZE;
public final static int BLOCK_SIZE = GmSSLJNI.SM4_BLOCK_SIZE;
private long sm4_key = 0;
public Sm4(byte[] key, boolean do_encrypt) {
if (key == null) {
throw new GmSSLException("");
}
if (key.length != this.KEY_SIZE) {
throw new GmSSLException("");
}
if ((sm4_key = GmSSLJNI.sm4_key_new()) == 0) {
throw new GmSSLException("");
}
if (do_encrypt == true) {
if (GmSSLJNI.sm4_set_encrypt_key(sm4_key, key) != 1) {
throw new GmSSLException("");
}
} else {
if (GmSSLJNI.sm4_set_decrypt_key(sm4_key, key) != 1) {
throw new GmSSLException("");
}
}
}
public void encrypt(byte[] in, int in_offset, byte[] out, int out_offset) {
if (in == null
|| in_offset < 0
|| in_offset + this.BLOCK_SIZE <= 0
|| in_offset + this.BLOCK_SIZE > in.length) {
throw new GmSSLException("");
}
if (out == null
|| out_offset < 0
|| out_offset + this.BLOCK_SIZE <= 0
|| out_offset + this.BLOCK_SIZE > in.length) {
throw new GmSSLException("");
}
if (GmSSLJNI.sm4_encrypt(sm4_key, in, in_offset, out, out_offset) != 1) {
throw new GmSSLException("");
}
}
}