-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.go
111 lines (100 loc) · 3.3 KB
/
digest.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
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 digest_init(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism) {
return (*fl->C_DigestInit)(hSession, pMechanism);
}
CK_RV digest(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR * pDigest, CK_ULONG_PTR pulDigestLen) {
CK_RV rv = (*fl->C_Digest)(hSession, pData, ulDataLen, NULL, pulDigestLen);
if (rv != CKR_OK) {
return rv;
}
*pDigest = calloc(*pulDigestLen, sizeof(CK_BYTE));
if (*pDigest == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_Digest)(hSession, pData, ulDataLen, *pDigest, pulDigestLen);
return rv;
}
CK_RV digest_update(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen) {
return (*fl->C_DigestUpdate)(hSession, pPart, ulPartLen);
}
CK_RV digest_key(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) {
return (*fl->C_DigestKey)(hSession, hKey);
}
CK_RV digest_final(CK_FUNCTION_LIST_PTR fl, CK_SESSION_HANDLE hSession, CK_BYTE_PTR *pDigest, CK_ULONG_PTR pulDigestLen) {
CK_RV rv = (*fl->C_DigestFinal)(hSession, NULL, pulDigestLen);
if (rv != CKR_OK) {
return rv;
}
*pDigest = calloc(*pulDigestLen, sizeof(CK_BYTE));
if (*pDigest == NULL) {
return CKR_HOST_MEMORY;
}
rv = (*fl->C_DigestFinal)(hSession, *pDigest, pulDigestLen);
return rv;
}
*/
import "C"
import (
"fmt"
"unsafe"
)
// Initializes a message-digesting operation.
func (o *Object) DigestInit(m *Mechanism) error {
arena, mech := cMechanism(m)
defer arena.Free()
if rv := C.digest_init(o.fl, o.h, mech); rv != C.CKR_OK {
return fmt.Errorf("digest_init: 0x%08x : %s", rv, returnValues[rv])
}
return nil
}
// Digests message in a single part.
func (o *Object) Digest(message []byte) ([]byte, error) {
var (
hash C.CK_BYTE_PTR
hashlen C.CK_ULONG
)
if rv := C.digest(o.fl, o.h, cData(message), C.CK_ULONG(len(message)), &hash, &hashlen); rv != C.CKR_OK {
return nil, fmt.Errorf("digest: 0x%08x : %s", rv, returnValues[rv])
}
h := C.GoBytes(unsafe.Pointer(hash), C.int(hashlen))
C.free(unsafe.Pointer(hash))
return h, nil
}
// Continues a multiple-part message-digesting operation.
func (o *Object) DigestUpdate(message []byte) error {
if rv := C.digest_update(o.fl, o.h, cData(message), C.CK_ULONG(len(message))); rv != C.CKR_OK {
return fmt.Errorf("digest_update: 0x%08x : %s", rv, returnValues[rv])
}
return nil
}
// Continues a multi-part message-digesting
// operation, by digesting the value of a secret key as part of
// the data already digested.
func (o *Object) DigestKey() error {
if rv := C.digest_key(o.fl, o.h, o.o); rv != C.CKR_OK {
return fmt.Errorf("digest_key: 0x%08x : %s", rv, returnValues[rv])
}
return nil
}
// Finishes a multiple-part message-digesting operation.
func (o *Object) DigestFinal() ([]byte, error) {
var (
hash C.CK_BYTE_PTR
hashlen C.CK_ULONG
)
if rv := C.digest_final(o.fl, o.h, &hash, &hashlen); rv != C.CKR_OK {
return nil, fmt.Errorf("digest_final: 0x%08x : %s", rv, returnValues[rv])
}
h := C.GoBytes(unsafe.Pointer(hash), C.int(hashlen))
C.free(unsafe.Pointer(hash))
return h, nil
}