-
Notifications
You must be signed in to change notification settings - Fork 8
/
sm4.go
354 lines (290 loc) · 8.49 KB
/
sm4.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* 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
*/
/* +build cgo */
package gmssl
/*
#include <stdlib.h>
#include <string.h>
#include <gmssl/sm4.h>
#include <gmssl/mem.h>
#include <gmssl/aead.h>
#include <gmssl/error.h>
*/
import "C"
import (
"errors"
"unsafe"
)
const (
Sm4KeySize = 16
Sm4BlockSize = 16
Sm4CbcIvSize = 16
Sm4CtrIvSize = 16
Sm4GcmMinIvSize = 8
Sm4GcmMaxIvSize = 64
Sm4GcmDefaultIvSize = 64
Sm4GcmDefaultTagSize = 16
Sm4GcmMaxTagSize = 16
)
type Sm4 struct {
sm4_key C.SM4_KEY
encrypt bool
}
func NewSm4(key []byte, encrypt bool) (*Sm4, error) {
if key == nil {
return nil, errors.New("No key")
}
if len(key) != Sm4KeySize {
return nil, errors.New("Invalid key length")
}
sm4 := new(Sm4)
if encrypt == true {
C.sm4_set_encrypt_key(&sm4.sm4_key, (*C.uchar)(&key[0]))
} else {
C.sm4_set_decrypt_key(&sm4.sm4_key, (*C.uchar)(&key[0]))
}
sm4.encrypt = encrypt
return sm4, nil
}
func (sm4 *Sm4) Encrypt(block []byte) ([]byte, error) {
if len(block) != Sm4BlockSize {
return nil, errors.New("Invalid block size")
}
outbuf := make([]byte, Sm4BlockSize)
C.sm4_encrypt(&sm4.sm4_key, (*C.uchar)(&block[0]), (*C.uchar)(unsafe.Pointer(&outbuf[0])))
return outbuf, nil
}
type Sm4Cbc struct {
sm4_cbc_ctx C.SM4_CBC_CTX
encrypt bool
}
func NewSm4Cbc(key []byte, iv []byte, encrypt bool) (*Sm4Cbc, error) {
if key == nil {
return nil, errors.New("No key")
}
if len(key) != Sm4KeySize {
return nil, errors.New("Invalid key length")
}
if len(iv) != Sm4CbcIvSize {
return nil, errors.New("Invalid IV length")
}
sm4_cbc := new(Sm4Cbc)
if encrypt == true {
if 1 != C.sm4_cbc_encrypt_init(&sm4_cbc.sm4_cbc_ctx, (*C.uchar)(&key[0]), (*C.uchar)(&iv[0])) {
return nil, errors.New("Libgmssl inner error")
}
} else {
if 1 != C.sm4_cbc_decrypt_init(&sm4_cbc.sm4_cbc_ctx, (*C.uchar)(&key[0]), (*C.uchar)(&iv[0])) {
return nil, errors.New("Libgmssl inner error")
}
}
sm4_cbc.encrypt = encrypt
return sm4_cbc, nil
}
func (cbc *Sm4Cbc) Reset(key []byte, iv []byte, encrypt bool) error {
if key == nil {
return errors.New("No key")
}
if len(key) != Sm4KeySize {
return errors.New("Invalid key length")
}
if len(iv) != Sm4CbcIvSize {
return errors.New("Invalid IV length")
}
if encrypt == true {
if 1 != C.sm4_cbc_encrypt_init(&cbc.sm4_cbc_ctx, (*C.uchar)(&key[0]), (*C.uchar)(&iv[0])) {
return errors.New("Libgmssl inner error")
}
} else {
if 1 != C.sm4_cbc_decrypt_init(&cbc.sm4_cbc_ctx, (*C.uchar)(&key[0]), (*C.uchar)(&iv[0])) {
return errors.New("Libgmssl inner error")
}
}
cbc.encrypt = encrypt
return nil
}
func (cbc *Sm4Cbc) Update(data []byte) ([]byte, error) {
outbuf := make([]byte, len(data) + Sm4BlockSize)
var outlen C.size_t
if cbc.encrypt {
if 1 != C.sm4_cbc_encrypt_update(&cbc.sm4_cbc_ctx,
(*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data)),
(*C.uchar)(unsafe.Pointer(&outbuf[0])), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
} else {
if 1 != C.sm4_cbc_decrypt_update(&cbc.sm4_cbc_ctx,
(*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data)),
(*C.uchar)(unsafe.Pointer(&outbuf[0])), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
}
return outbuf[:outlen], nil
}
func (cbc *Sm4Cbc) Finish() ([]byte, error) {
outbuf := make([]byte, Sm4BlockSize)
var outlen C.size_t
if cbc.encrypt {
if 1 != C.sm4_cbc_encrypt_finish(&cbc.sm4_cbc_ctx,
(*C.uchar)(unsafe.Pointer(&outbuf[0])), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
} else {
if 1 != C.sm4_cbc_decrypt_finish(&cbc.sm4_cbc_ctx,
(*C.uchar)(unsafe.Pointer(&outbuf[0])), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
}
return outbuf[:outlen], nil
}
type Sm4Ctr struct {
sm4_ctr_ctx C.SM4_CTR_CTX
}
func NewSm4Ctr(key []byte, iv []byte) (*Sm4Ctr, error) {
if key == nil {
return nil, errors.New("No key")
}
if len(key) != Sm4KeySize {
return nil, errors.New("Invalid key length")
}
if len(iv) != Sm4CtrIvSize {
return nil, errors.New("Invalid IV length")
}
ctr := new(Sm4Ctr)
if 1 != C.sm4_ctr_encrypt_init(&ctr.sm4_ctr_ctx,
(*C.uchar)(unsafe.Pointer(&key[0])), (*C.uchar)(unsafe.Pointer(&iv[0]))) {
return nil, errors.New("Libgmssl inner error")
}
return ctr, nil
}
func (ctr *Sm4Ctr) Reset(key []byte, iv []byte) error {
if key == nil {
return errors.New("No key")
}
if len(key) != Sm4KeySize {
return errors.New("Invalid key length")
}
if len(iv) != Sm4CtrIvSize {
return errors.New("Invalid IV length")
}
if 1 != C.sm4_ctr_encrypt_init(&ctr.sm4_ctr_ctx, (*C.uchar)(&key[0]), (*C.uchar)(&iv[0])) {
return errors.New("Libgmssl inner error")
}
return nil
}
func (ctr *Sm4Ctr) Update(data []byte) ([]byte, error) {
outbuf := make([]byte, len(data) + Sm4BlockSize)
var outlen C.size_t
if 1 != C.sm4_ctr_encrypt_update(&ctr.sm4_ctr_ctx,
(*C.uchar)(&data[0]), C.size_t(len(data)), (*C.uchar)(&outbuf[0]), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
return outbuf[:outlen], nil
}
func (ctr *Sm4Ctr) Finish() ([]byte, error) {
outbuf := make([]byte, Sm4BlockSize)
var outlen C.size_t
if 1 != C.sm4_ctr_encrypt_finish(&ctr.sm4_ctr_ctx, (*C.uchar)(&outbuf[0]), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
return outbuf[:outlen], nil
}
type Sm4Gcm struct {
sm4_gcm_ctx C.SM4_GCM_CTX
encrypt bool
}
func NewSm4Gcm(key []byte, iv []byte, aad []byte, taglen int, encrypt bool) (*Sm4Gcm, error) {
if key == nil {
return nil, errors.New("No key")
}
if len(key) != Sm4KeySize {
return nil, errors.New("Invalid key length")
}
if len(iv) < Sm4GcmMinIvSize || len(iv) > Sm4GcmMaxIvSize {
return nil, errors.New("Invalid IV length")
}
if taglen > Sm4GcmMaxTagSize {
return nil, errors.New("Invalid Tag length")
}
gcm := new(Sm4Gcm)
if encrypt == true {
if 1 != C.sm4_gcm_encrypt_init(&gcm.sm4_gcm_ctx,
(*C.uchar)(&key[0]), C.size_t(len(key)), (*C.uchar)(&iv[0]), C.size_t(len(iv)),
(*C.uchar)(&aad[0]), C.size_t(len(aad)), C.size_t(taglen)) {
return nil, errors.New("Libgmssl inner error")
}
} else {
if 1 != C.sm4_gcm_decrypt_init(&gcm.sm4_gcm_ctx, (*C.uchar)(&key[0]), C.size_t(len(key)), (*C.uchar)(&iv[0]), C.size_t(len(iv)),
(*C.uchar)(&aad[0]), C.size_t(len(aad)), C.size_t(taglen)) {
return nil, errors.New("Libgmssl inner error")
}
}
gcm.encrypt = encrypt
return gcm, nil
}
func (gcm *Sm4Gcm) Reset(key []byte, iv []byte, aad []byte, taglen int, encrypt bool) error {
if key == nil {
errors.New("No key")
}
if len(key) != Sm4KeySize {
return errors.New("Invalid key length")
}
if len(iv) < Sm4GcmMinIvSize || len(iv) > Sm4GcmMaxIvSize {
return errors.New("Invalid IV length")
}
if taglen > Sm4GcmMaxTagSize {
return errors.New("Invalid Tag length")
}
if encrypt == true {
if 1 != C.sm4_gcm_encrypt_init(&gcm.sm4_gcm_ctx,
(*C.uchar)(&key[0]), C.size_t(len(key)), (*C.uchar)(&iv[0]), C.size_t(len(iv)),
(*C.uchar)(&aad[0]), C.size_t(len(aad)), C.size_t(taglen)) {
return errors.New("Libgmssl inner error")
}
} else {
if 1 != C.sm4_gcm_decrypt_init(&gcm.sm4_gcm_ctx, (*C.uchar)(&key[0]), C.size_t(len(key)), (*C.uchar)(&iv[0]), C.size_t(len(iv)),
(*C.uchar)(&aad[0]), C.size_t(len(aad)), C.size_t(taglen)) {
return errors.New("Libgmssl inner error")
}
}
gcm.encrypt = encrypt
return nil
}
func (gcm *Sm4Gcm) Update(data []byte) ([]byte, error) {
outbuf := make([]byte, len(data) + Sm4BlockSize)
var outlen C.size_t
if gcm.encrypt {
if 1 != C.sm4_gcm_encrypt_update(&gcm.sm4_gcm_ctx,
(*C.uchar)(&data[0]), C.size_t(len(data)), (*C.uchar)(&outbuf[0]), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
} else {
if 1 != C.sm4_gcm_decrypt_update(&gcm.sm4_gcm_ctx,
(*C.uchar)(&data[0]), C.size_t(len(data)), (*C.uchar)(&outbuf[0]), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
}
return outbuf[:outlen], nil
}
func (gcm *Sm4Gcm) Finish() ([]byte, error) {
var outbuf []byte
var outlen C.size_t
if gcm.encrypt {
outbuf = make([]byte, Sm4BlockSize + Sm4GcmMaxTagSize)
if 1 != C.sm4_gcm_encrypt_finish(&gcm.sm4_gcm_ctx, (*C.uchar)(&outbuf[0]), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
} else {
outbuf = make([]byte, Sm4BlockSize)
if 1 != C.sm4_gcm_decrypt_finish(&gcm.sm4_gcm_ctx, (*C.uchar)(&outbuf[0]), &outlen) {
return nil, errors.New("Libgmssl inner error")
}
}
return outbuf[:outlen], nil
}