-
Notifications
You must be signed in to change notification settings - Fork 506
/
des_algo_handler.c
112 lines (86 loc) · 1.99 KB
/
des_algo_handler.c
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
/**
* DES encrypt algorithms handler module for Beast
* @author: liexusong
*/
#include <stdlib.h>
#include <string.h>
#include "beast_log.h"
#include "beast_module.h"
#include "des_algo_lib.c"
static char key[8] = {
0x01, 0x1f, 0x01, 0x1f,
0x01, 0x0e, 0x01, 0x0e,
};
int des_encrypt_handler(char *inbuf, int len,
char **outbuf, int *outlen)
{
int blocks, i, fixcnt;
char input[8], output[8];
char *out;
int retlen;
if ((len % 8) == 0) {
fixcnt = 0;
blocks = len / 8;
} else {
fixcnt = len % 8;
blocks = len / 8 + 1;
}
retlen = blocks * 8;
out = malloc(retlen);
if (!out) {
beast_write_log(beast_log_error,
"Out of memory when allocate `%d' size by encrypt(DES)", retlen);
return -1;
}
for (i = 0; i < blocks; i++) {
memset(input, 0, 8);
/* The last block not enough 8 bytes, fix me */
if (i + 1 == blocks && fixcnt > 0) {
memcpy(input, &inbuf[i*8], fixcnt);
} else {
memcpy(input, &inbuf[i*8], 8);
}
DES_encipher(input, output, key);
memcpy(&out[i * 8], output, 8);
}
*outbuf = out;
*outlen = retlen;
return 0;
}
int des_decrypt_handler(char *inbuf, int len,
char **outbuf, int *outlen)
{
int blocks, retlen, i;
char *out;
if (len % 8 == 0) {
blocks = len / 8;
} else {
blocks = len / 8 + 1;
}
retlen = blocks * 8;
out = malloc(retlen);
if (!out) {
beast_write_log(beast_log_error,
"Out of memory when allocate `%d' size by decrypt(DES)", retlen);
return -1;
}
for (i = 0; i < blocks; i++) {
DES_decipher(&inbuf[i*8], &out[i*8], key);
}
*outbuf = out;
*outlen = retlen;
return 0;
}
void des_free_handler(void *ptr)
{
if (ptr) {
free(ptr);
}
}
struct beast_ops des_handler_ops = {
"des-algo",
des_encrypt_handler,
des_decrypt_handler,
des_free_handler,
NULL
};