crypt.c
3.25 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdarg.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <mcrypt.h>
#include <mhash.h>
#include "token/cclass.h"
#include "token/crypt.h"
static
void
__construct(struct CRYPT * _this, va_list * params)
{
_this->algorithm = va_arg(* params, const char * const);
_this->mode = va_arg(* params, const char * const);
_this->mcrypt = mcrypt_module_open(
(char *)_this->algorithm,
NULL,
(char *)_this->mode,
NULL);
_this->ivsize = mcrypt_enc_get_iv_size(_this->mcrypt);
_this->keysize = mcrypt_enc_get_key_size(_this->mcrypt);
}
static
void
__destruct(struct CRYPT * _this)
{
if (_this->iv) {
free(_this->iv);
}
mcrypt_module_close(_this->mcrypt);
}
static const
struct CCLASS _crypt = {
sizeof(struct CRYPT),
(ctor)__construct,
NULL,
(dtor)__destruct,
NULL
};
const struct CCLASS * const CRYPT = &_crypt;
void *
crypt_createIv(struct CRYPT * _this)
{
int urandom;
size_t rsize = 0;
void * iv = NULL;
iv = calloc(_this->ivsize, sizeof(char));
urandom = open("/dev/urandom", O_RDONLY);
rsize = read(urandom, iv, _this->ivsize);
if (_this->ivsize != rsize) {
free(iv);
iv = NULL;
}
return iv;
}
static
void *
createKey(struct CRYPT * _this, const char * const password)
{
void * key = NULL;
key = calloc(_this->keysize, sizeof(char));
mhash_keygen(
KEYGEN_MCRYPT,
MHASH_SHA256,
mhash_keygen_count(),
key,
_this->keysize,
NULL,
0,
(char *)password, // @TODO: bad karma...now this might change password.
strlen(password));
return key;
}
void *
crypt_encrypt(
struct CRYPT * _this,
const void * const data,
const char * const password,
size_t * length)
{
char * encrypted;
void * iv;
void * key;
key = createKey(_this, password);
if(_this->iv) {
iv = _this->iv;
} else {
iv = crypt_createIv(_this);
}
mcrypt_generic_init(_this->mcrypt, key, _this->keysize, iv);
encrypted = calloc(_this->ivsize + *length, sizeof(char));
memcpy(encrypted, iv, _this->ivsize);
memcpy(encrypted + _this->ivsize, data, *length);
mcrypt_generic(_this->mcrypt, encrypted + _this->ivsize, *length);
mcrypt_generic_deinit(_this->mcrypt);
*length += _this->ivsize;
free(key);
if (_this->iv != iv) {
free(iv);
}
return encrypted;
}
void *
crypt_decrypt(
struct CRYPT * _this,
const void * const data,
const char * const password,
size_t * length)
{
char * decrypted;
void * iv;
void * key;
key = createKey(_this, password);
iv = calloc(_this->ivsize, sizeof(char));
memcpy(iv, data, _this->ivsize);
mcrypt_generic_init(_this->mcrypt, key, _this->keysize, iv);
*length -= _this->ivsize;
decrypted = calloc(*length, sizeof(char));
memcpy(decrypted, data + _this->ivsize, *length);
mdecrypt_generic(_this->mcrypt, decrypted, *length);
mcrypt_generic_deinit(_this->mcrypt);
free(key);
free(iv);
return decrypted;
}
// vim: set et ts=4 sw=4: