-
Notifications
You must be signed in to change notification settings - Fork 7
/
sm9.go
486 lines (364 loc) · 10.9 KB
/
sm9.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm9.h>
#include <gmssl/mem.h>
#include <gmssl/error.h>
*/
import "C"
import (
"unsafe"
"errors"
)
const (
Sm9MaxIdSize = 63
Sm9MaxPlaintextSize = 255
Sm9MaxCiphertextSize = 367
Sm9SignatureSize = 104
)
type Sm9EncMasterKey struct {
master_key C.SM9_ENC_MASTER_KEY
has_private_key bool
}
func GenerateSm9EncMasterKey() (*Sm9EncMasterKey, error) {
ret := new(Sm9EncMasterKey)
if C.sm9_enc_master_key_generate(&ret.master_key) != 1{
return nil, errors.New("Libgmssl inner error")
}
ret.has_private_key = true
return ret, nil
}
func ImportEncryptedSm9EncMasterKeyInfoPem(path string, pass string) (*Sm9EncMasterKey, error) {
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("rb"))
if fp == nil {
return nil, errors.New("fopen failure")
}
defer C.fclose(fp)
ret := new(Sm9EncMasterKey)
if C.sm9_enc_master_key_info_decrypt_from_pem(&ret.master_key, pass_str, fp) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.has_private_key = true
return ret, nil
}
func ImportSm9EncMasterPublicKeyPem(path string) (*Sm9EncMasterKey, error) {
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("rb"))
if fp == nil {
return nil, errors.New("fopen failure")
}
defer C.fclose(fp)
ret := new(Sm9EncMasterKey)
if C.sm9_enc_master_public_key_from_pem(&ret.master_key, fp) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.has_private_key = false
return ret, nil
}
func (sm9 *Sm9EncMasterKey) ExportEncryptedMasterKeyInfoPem(path string, pass string) error {
if sm9.has_private_key != true {
return errors.New("Not private key")
}
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("wb"))
if fp == nil {
return errors.New("fopen failure")
}
defer C.fclose(fp)
if C.sm9_enc_master_key_info_encrypt_to_pem(&sm9.master_key, pass_str, fp) != 1 {
return errors.New("Libgmssl inner error")
}
return nil
}
func (sm9 *Sm9EncMasterKey) ExportMasterPublicKeyPem(path string) error {
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("wb"))
if fp == nil {
return errors.New("fopen failure")
}
defer C.fclose(fp)
if C.sm9_enc_master_public_key_to_pem(&sm9.master_key, fp) != 1 {
return errors.New("Libgmssl inner error")
}
return nil
}
func (sm9 *Sm9EncMasterKey) ExtractKey(id string) (*Sm9EncKey, error) {
if sm9.has_private_key != true {
return nil, errors.New("Not private key")
}
id_str := C.CString(id)
defer C.free(unsafe.Pointer(id_str))
ret := new(Sm9EncKey)
if C.sm9_enc_master_key_extract_key(&sm9.master_key, id_str, C.strlen(id_str), &ret.key) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.id = id
return ret, nil
}
func (sm9 *Sm9EncMasterKey) Encrypt(in []byte, to string) ([]byte, error) {
if len(in) > C.SM9_MAX_PLAINTEXT_SIZE {
return nil, errors.New("Plaintext too long")
}
to_str := C.CString(to)
defer C.free(unsafe.Pointer(to_str))
outbuf := make([]byte, C.SM9_MAX_CIPHERTEXT_SIZE)
var outlen C.size_t
if C.sm9_encrypt(&sm9.master_key, to_str, C.strlen(to_str),
(*C.uchar)(&in[0]), C.size_t(len(in)), (*C.uchar)(&outbuf[0]), &outlen) != 1 {
return nil, errors.New("Libgmssl inner error")
}
return outbuf[:outlen], nil
}
type Sm9EncKey struct {
key C.SM9_ENC_KEY
id string
}
func ImportEncryptedSm9EncPrivateKeyInfoPem(path string, pass string, id string) (*Sm9EncKey, error) {
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("rb"))
if fp == nil {
return nil, errors.New("fopen failure")
}
defer C.fclose(fp)
ret := new(Sm9EncKey)
if C.sm9_enc_key_info_decrypt_from_pem(&ret.key, pass_str, fp) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.id = id
return ret, nil
}
func (sm9 *Sm9EncKey) GetId() string {
return sm9.id
}
func (sm9 *Sm9EncKey) ExportEncryptedPrivateKeyInfoPem(path string, pass string) error {
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("wb"))
if fp == nil {
return errors.New("fopen failure")
}
defer C.fclose(fp)
if C.sm9_enc_key_info_encrypt_to_pem(&sm9.key, pass_str, fp) != 1 {
return errors.New("Libgmssl inner error")
}
return nil
}
func (sm9 *Sm9EncKey) Decrypt(in []byte) ([]byte, error) {
id_str := C.CString(sm9.id)
defer C.free(unsafe.Pointer(id_str))
outbuf := make([]byte, C.SM9_MAX_PLAINTEXT_SIZE)
var outlen C.size_t
if C.sm9_decrypt(&sm9.key, id_str, C.strlen(id_str),
(*C.uchar)(&in[0]), C.size_t(len(in)), (*C.uchar)(&outbuf[0]), &outlen) != 1 {
return nil, errors.New("Libgmssl inner error")
}
return outbuf[:outlen], nil
}
type Sm9SignMasterKey struct {
master_key C.SM9_SIGN_MASTER_KEY
has_private_key bool
}
func GenerateSm9SignMasterKey() (*Sm9SignMasterKey, error) {
ret := new(Sm9SignMasterKey)
if C.sm9_sign_master_key_generate(&ret.master_key) != 1{
return nil, errors.New("Libgmssl inner error")
}
ret.has_private_key = true
return ret, nil
}
func ImportEncryptedSm9SignMasterKeyInfoPem(path string, pass string) (*Sm9SignMasterKey, error) {
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("rb"))
if fp == nil {
return nil, errors.New("fopen failure")
}
defer C.fclose(fp)
ret := new(Sm9SignMasterKey)
if C.sm9_sign_master_key_info_decrypt_from_pem(&ret.master_key, pass_str, fp) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.has_private_key = true
return ret, nil
}
func ImportSm9SignMasterPublicKeyPem(path string) (*Sm9SignMasterKey, error) {
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("rb"))
if fp == nil {
return nil, errors.New("fopen failure")
}
defer C.fclose(fp)
ret := new(Sm9SignMasterKey)
if C.sm9_sign_master_public_key_from_pem(&ret.master_key, fp) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.has_private_key = false
return ret, nil
}
func (sm9 *Sm9SignMasterKey) ExportEncryptedMasterKeyInfoPem(path string, pass string) error {
if sm9.has_private_key != true {
return errors.New("Not private key")
}
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("wb"))
if fp == nil {
return errors.New("fopen failure")
}
defer C.fclose(fp)
if C.sm9_sign_master_key_info_encrypt_to_pem(&sm9.master_key, pass_str, fp) != 1 {
return errors.New("Libgmssl inner error")
}
return nil
}
func (sm9 *Sm9SignMasterKey) ExportMasterPublicKeyPem(path string) error {
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("wb"))
if fp == nil {
return errors.New("fopen failure")
}
defer C.fclose(fp)
if C.sm9_sign_master_public_key_to_pem(&sm9.master_key, fp) != 1 {
return errors.New("Libgmssl inner error")
}
return nil
}
func (sm9 *Sm9SignMasterKey) ExtractKey(id string) (*Sm9SignKey, error) {
if sm9.has_private_key != true {
return nil, errors.New("Not private key")
}
id_str := C.CString(id)
defer C.free(unsafe.Pointer(id_str))
ret := new(Sm9SignKey)
if C.sm9_sign_master_key_extract_key(&sm9.master_key, id_str, C.strlen(id_str), &ret.key) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.id = id
return ret, nil
}
type Sm9SignKey struct {
key C.SM9_SIGN_KEY
id string
}
func ImportEncryptedSm9SignPrivateKeyInfoPem(path string, pass string, id string) (*Sm9SignKey, error) {
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("rb"))
if fp == nil {
return nil, errors.New("fopen failure")
}
defer C.fclose(fp)
ret := new(Sm9SignKey)
if C.sm9_sign_key_info_decrypt_from_pem(&ret.key, pass_str, fp) != 1 {
return nil, errors.New("Libgmssl inner error")
}
ret.id = id
return ret, nil
}
func (sm9 *Sm9SignKey) GetId() string {
return sm9.id
}
func (sm9 *Sm9SignKey) ExportEncryptedPrivateKeyInfoPem(path string, pass string) error {
pass_str := C.CString(pass)
defer C.free(unsafe.Pointer(pass_str))
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
fp := C.fopen(path_str, C.CString("wb"))
if fp == nil {
return errors.New("fopen failure")
}
defer C.fclose(fp)
if C.sm9_sign_key_info_encrypt_to_pem(&sm9.key, pass_str, fp) != 1 {
return errors.New("Libgmssl inner error")
}
return nil
}
type Sm9Signature struct {
sm9_sign_ctx C.SM9_SIGN_CTX
sign bool
}
func NewSm9Signature(sign bool) (*Sm9Signature, error) {
ret := new(Sm9Signature)
if sign == true {
if C.sm9_sign_init(&ret.sm9_sign_ctx) != 1 {
return nil, errors.New("Libgmssl inner error")
}
} else {
if C.sm9_verify_init(&ret.sm9_sign_ctx) != 1 {
return nil, errors.New("Libgmssl inner error")
}
}
ret.sign = sign
return ret, nil
}
func (sig *Sm9Signature) Update(data []byte) error {
if len(data) == 0 {
return nil
}
if sig.sign == true {
if C.sm9_sign_update(&sig.sm9_sign_ctx, (*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data))) != 1 {
return errors.New("Libgmssl inner error")
}
} else {
if C.sm9_verify_update(&sig.sm9_sign_ctx, (*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data))) != 1 {
return errors.New("Libgmssl inner error")
}
}
return nil
}
func (sig *Sm9Signature) Sign(sign_key *Sm9SignKey) ([]byte, error) {
if sig.sign != true {
return nil, errors.New("Not signing state")
}
outbuf := make([]byte, C.SM9_SIGNATURE_SIZE)
var outlen C.size_t
if C.sm9_sign_finish(&sig.sm9_sign_ctx, &sign_key.key, (*C.uchar)(unsafe.Pointer(&outbuf[0])), &outlen) != 1 {
return nil, errors.New("Libgmssl inner error")
}
return outbuf[:outlen], nil
}
func (sig *Sm9Signature) Verify(signature []byte, master_public_key *Sm9SignMasterKey, signer_id string) bool {
if sig.sign != false {
return false
}
signer_id_str := C.CString(signer_id)
defer C.free(unsafe.Pointer(signer_id_str))
if C.sm9_verify_finish(&sig.sm9_sign_ctx, (*C.uchar)(unsafe.Pointer(&signature[0])), C.size_t(len(signature)),
&master_public_key.master_key, signer_id_str, C.strlen(signer_id_str)) != 1 {
return false
}
return true
}