-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.go
115 lines (103 loc) · 3.45 KB
/
decrypt.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
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 decrypt_init(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) {
return (*fl->C_DecryptInit)(hSession, pMechanism, hKey);
}
CK_RV decrypt(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen, CK_BYTE_PTR * pData, CK_ULONG_PTR pulDataLen) {
CK_RV rv = (*fl->C_Decrypt)(hSession, pEncryptedData, ulEncryptedDataLen, NULL, pulDataLen);
if (rv != CKR_OK) {
return rv;
}
*pData = calloc(*pulDataLen, sizeof(CK_BYTE));
if (*pData == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_Decrypt)(hSession, pEncryptedData, ulEncryptedDataLen, *pData, pulDataLen);
return rv;
}
CK_RV decrypt_update(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR *pPart, CK_ULONG_PTR pulPartLen) {
CK_RV rv = (*fl->C_DecryptUpdate)(hSession, pEncryptedPart, ulEncryptedPartLen, NULL, pulPartLen);
if (rv != CKR_OK) {
return rv;
}
*pPart = calloc(*pulPartLen, sizeof(CK_BYTE));
if (*pPart == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_DecryptUpdate)(hSession, pEncryptedPart, ulEncryptedPartLen, *pPart, pulPartLen);
return rv;
}
CK_RV decrypt_final(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR *pLastPart, CK_ULONG_PTR pulLastPartLen) {
CK_RV rv = (*fl->C_DecryptFinal)(hSession, NULL, pulLastPartLen);
if (rv != CKR_OK) {
return rv;
}
*pLastPart = calloc(*pulLastPartLen, sizeof(CK_BYTE));
if (*pLastPart == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_DecryptFinal)(hSession, *pLastPart, pulLastPartLen);
return rv;
}
*/
import "C"
import (
"fmt"
"unsafe"
)
// Initializes a decryption operation.
func (o *Object) DecryptInit(m *Mechanism) error {
arena, cm := cMechanism(m)
defer arena.Free()
if rv := C.decrypt_init(o.fl, o.h, cm, o.o); rv != C.CKR_OK {
return fmt.Errorf("decrypt_init: 0x%08x : %s", rv, returnValues[rv])
}
return nil
}
// Decrypts encrypted data in a single part.
func (o *Object) Decrypt(data []byte) ([]byte, error) {
var (
plain C.CK_BYTE_PTR
plainlen C.CK_ULONG
)
if rv := C.decrypt(o.fl, o.h, cData(data), C.CK_ULONG(len(data)), &plain, &plainlen); rv != C.CKR_OK {
return nil, fmt.Errorf("encrypt: 0x%08x : %s", rv, returnValues[rv])
}
s := C.GoBytes(unsafe.Pointer(&plain), C.int(plainlen))
C.free(unsafe.Pointer(plain))
return s, nil
}
// Continues a multiple-part decryption operation.
func (o *Object) DecryptUpdate(cipher []byte) ([]byte, error) {
var (
part C.CK_BYTE_PTR
partlen C.CK_ULONG
)
if rv := C.decrypt_update(o.fl, o.h, cData(cipher), C.CK_ULONG(len(cipher)), &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 decryption operation.
func (o *Object) DecryptFinal() ([]byte, error) {
var (
plain C.CK_BYTE_PTR
plainlen C.CK_ULONG
)
if rv := C.decrypt_final(o.fl, o.h, &plain, &plainlen); rv != C.CKR_OK {
return nil, fmt.Errorf("encrypt_final: 0x%08x : %s", rv, returnValues[rv])
}
h := C.GoBytes(unsafe.Pointer(plain), C.int(plainlen))
C.free(unsafe.Pointer(plain))
return h, nil
}