-
Notifications
You must be signed in to change notification settings - Fork 4
/
mcrypt.go
105 lines (88 loc) · 2.63 KB
/
mcrypt.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
package nscatools
/*
#cgo LDFLAGS: -lmcrypt
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
// MCryptDecrypt uses libmcrypt to decrypt the data received from the send_nsca
// client. When I have some more time I'll dig to find out why I'm not able to
// decrypt directly using the NewCFBDecrypter
func MCryptDecrypt(algo string, blocks, key, iv []byte) error {
algorithm := C.CString(algo)
defer C.free(unsafe.Pointer(algorithm))
mode := C.CString("cfb")
defer C.free(unsafe.Pointer(mode))
td := C.mcrypt_module_open(algorithm, nil, mode, nil)
defer C.mcrypt_module_close(td)
if uintptr(unsafe.Pointer(td)) == C.MCRYPT_FAILED {
return fmt.Errorf("mcrypt module open failed")
}
keySize := C.mcrypt_enc_get_key_size(td)
ivSize := C.mcrypt_enc_get_iv_size(td)
realKey := make([]byte, keySize)
if len(key) > int(keySize) {
copy(realKey, key[:keySize])
} else {
copy(realKey, key)
}
realIv := make([]byte, ivSize)
if len(iv) > int(ivSize) {
copy(realIv, iv[:ivSize])
} else {
copy(realIv, iv)
}
rv := C.mcrypt_generic_init(td, unsafe.Pointer(&realKey[0]), keySize, unsafe.Pointer(&realIv[0]))
defer C.mcrypt_generic_deinit(td)
if rv < 0 {
return fmt.Errorf("mcrypt generic init failed")
}
bufferSize := len(blocks)
for x := 0; x < bufferSize; x++ {
C.mdecrypt_generic(td, unsafe.Pointer(&blocks[x]), C.int(1))
}
return nil
}
// MCryptEncrypt uses libmcrypt to decrypt the data received from the send_nsca
// client. When I have some more time I'll dig to find out why I'm not able to
// decrypt directly using the NewCFBDecrypter
func MCryptEncrypt(algo string, blocks, key, iv []byte) error {
algorithm := C.CString(algo)
defer C.free(unsafe.Pointer(algorithm))
mode := C.CString("cfb")
defer C.free(unsafe.Pointer(mode))
td := C.mcrypt_module_open(algorithm, nil, mode, nil)
defer C.mcrypt_module_close(td)
if uintptr(unsafe.Pointer(td)) == C.MCRYPT_FAILED {
return fmt.Errorf("mcrypt module open failed")
}
keySize := C.mcrypt_enc_get_key_size(td)
ivSize := C.mcrypt_enc_get_iv_size(td)
realKey := make([]byte, keySize)
if len(key) > int(keySize) {
copy(realKey, key[:keySize])
} else {
copy(realKey, key)
}
realIv := make([]byte, ivSize)
if len(iv) > int(ivSize) {
copy(realIv, iv[:ivSize])
} else {
copy(realIv, iv)
}
rv := C.mcrypt_generic_init(td, unsafe.Pointer(&realKey[0]), keySize, unsafe.Pointer(&realIv[0]))
defer C.mcrypt_generic_deinit(td)
if rv < 0 {
return fmt.Errorf("mcrypt generic init failed")
}
bufferSize := len(blocks)
for x := 0; x < bufferSize; x++ {
C.mcrypt_generic(td, unsafe.Pointer(&blocks[x]), C.int(1))
}
return nil
}