-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm4Ctr.java
More file actions
90 lines (72 loc) · 1.99 KB
/
Sm4Ctr.java
File metadata and controls
90 lines (72 loc) · 1.99 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* 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 Sm4Ctr {
public final static int KEY_SIZE = GmSSLJNI.SM4_KEY_SIZE;
public final static int IV_SIZE = GmSSLJNI.SM4_BLOCK_SIZE;
public final static int BLOCK_SIZE = GmSSLJNI.SM4_BLOCK_SIZE;
private long sm4_ctr_ctx = 0;
private boolean inited = false;
public Sm4Ctr() {
if ((this.sm4_ctr_ctx = GmSSLJNI.sm4_ctr_ctx_new()) == 0) {
throw new GmSSLException("");
}
this.inited = false;
}
public void init(byte[] key, byte[] iv) {
if (key == null
|| key.length != this.KEY_SIZE
|| iv == null
|| iv.length != this.IV_SIZE) {
throw new GmSSLException("");
}
if (GmSSLJNI.sm4_ctr_encrypt_init(this.sm4_ctr_ctx, key, iv) != 1) {
throw new GmSSLException("");
}
this.inited = true;
}
public int update(byte[] in, int in_offset, int inlen, byte[] out, int out_offset) {
if (this.inited == false) {
throw new GmSSLException("");
}
if (in == null
|| in_offset < 0
|| inlen < 0
|| in_offset + inlen <= 0
|| in.length < in_offset + inlen) {
throw new GmSSLException("");
}
if (out == null
|| out_offset < 0
|| out.length < out_offset) {
throw new GmSSLException("");
}
int outlen;
if ((outlen = GmSSLJNI.sm4_ctr_encrypt_update(this.sm4_ctr_ctx, in, in_offset, inlen, out, out_offset)) < 0) {
throw new GmSSLException("");
}
return outlen;
}
public int doFinal(byte[] out, int out_offset) {
if (this.inited == false) {
throw new GmSSLException("");
}
if (out == null
|| out_offset < 0
|| out.length < out_offset) {
throw new GmSSLException("");
}
int outlen;
if ((outlen = GmSSLJNI.sm4_ctr_encrypt_finish(this.sm4_ctr_ctx, out, out_offset)) < 0) {
throw new GmSSLException("");
}
this.inited = false;
return outlen;
}
}