-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
114 lines (103 loc) · 3.51 KB
/
encrypt.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
package certex
/*
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include "./headers/cryptoki.h"
#include "./headers/pkcs11def.h"
#include "./headers/pkcs11t.h"
#include "./headers/PKICertexHSM.h"
CK_RV encrypt_init(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) {
return (*fl->C_EncryptInit)(hSession, pMechanism, hKey);
}
CK_RV encrypt(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR *pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen) {
CK_RV rv = (*fl->C_Encrypt)(hSession, pData, ulDataLen, NULL, pulEncryptedDataLen);
if (rv != CKR_OK) {
return rv;
}
*pEncryptedData = calloc(*pulEncryptedDataLen, sizeof(CK_BYTE));
if (*pEncryptedData == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_Encrypt)(hSession, pData, ulDataLen, *pEncryptedData, pulEncryptedDataLen);
return rv;
}
CK_RV encrypt_update(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen, CK_BYTE_PTR * pEncryptedPart, CK_ULONG_PTR pulEncryptedPartLen) {
CK_RV rv = (*fl->C_EncryptUpdate)(hSession, pPart, ulPartLen, NULL, pulEncryptedPartLen);
if (rv != CKR_OK) {
return rv;
}
*pEncryptedPart = calloc(*pulEncryptedPartLen, sizeof(CK_BYTE));
if (*pEncryptedPart == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_EncryptUpdate)(hSession, pPart, ulPartLen, *pEncryptedPart, pulEncryptedPartLen);
return rv;
}
CK_RV encrypt_final(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR * pLastEncryptedPart, CK_ULONG_PTR pulLastEncryptedPartLen) {
CK_RV rv = (*fl->C_EncryptFinal)(hSession, NULL, pulLastEncryptedPartLen);
if (rv != CKR_OK) {
return rv;
}
*pLastEncryptedPart = calloc(*pulLastEncryptedPartLen, sizeof(CK_BYTE));
if (*pLastEncryptedPart == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_EncryptFinal)(hSession, *pLastEncryptedPart, pulLastEncryptedPartLen);
return rv;
}
*/
import "C"
import (
"fmt"
"unsafe"
)
// Initializes an encryption operation.
func (o *Object) EncryptInit(m *Mechanism) error {
arena, cm := cMechanism(m)
defer arena.Free()
if rv := C.encrypt_init(o.fl, o.h, cm, o.o); rv != C.CKR_OK {
return fmt.Errorf("encrypt_init: 0x%08x : %s", rv, returnValues[rv])
}
return nil
}
// Encrypts single-part data.
func (o *Object) Encrypt(data []byte) ([]byte, error) {
var (
enc C.CK_BYTE_PTR
enclen C.CK_ULONG
)
if rv := C.encrypt(o.fl, o.h, cData(data), C.CK_ULONG(len(data)), &enc, &enclen); rv != C.CKR_OK {
return nil, fmt.Errorf("encrypt: 0x%08x : %s", rv, returnValues[rv])
}
s := C.GoBytes(unsafe.Pointer(&enc), C.int(enclen))
C.free(unsafe.Pointer(enc))
return s, nil
}
// Continues a multiple-part encryption operation.
func (o *Object) EncryptUpdate(plain []byte) ([]byte, error) {
var (
part C.CK_BYTE_PTR
partlen C.CK_ULONG
)
if rv := C.encrypt_update(o.fl, o.h, cData(plain), C.CK_ULONG(len(plain)), &part, &partlen); rv != C.CKR_OK {
return nil, fmt.Errorf("encrypt_final: 0x%08x : %s", rv, returnValues[rv])
}
h := C.GoBytes(unsafe.Pointer(part), C.int(partlen))
C.free(unsafe.Pointer(part))
return h, nil
}
// Finishes a multiple-part encryption operation.
func (o *Object) EncryptFinal() ([]byte, error) {
var (
enc C.CK_BYTE_PTR
enclen C.CK_ULONG
)
if rv := C.encrypt_final(o.fl, o.h, &enc, &enclen); rv != C.CKR_OK {
return nil, fmt.Errorf("encrypt_final: 0x%08x : %s", rv, returnValues[rv])
}
h := C.GoBytes(unsafe.Pointer(enc), C.int(enclen))
C.free(unsafe.Pointer(enc))
return h, nil
}