bigpoint_crypt.c
3.47 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
162
163
164
165
166
167
168
#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 "bigpoint/bigpoint_crypt.h"
static
void
__construct(struct BIGPOINT_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 BIGPOINT_CRYPT * _this)
{
if (_this->iv) {
free(_this->iv);
}
mcrypt_module_close(_this->mcrypt);
}
static const
struct BIGPOINT_CCLASS _bigpoint_crypt = {
sizeof(struct BIGPOINT_CRYPT),
(ctor)__construct,
NULL,
(dtor)__destruct,
NULL
};
const struct BIGPOINT_CCLASS * const BIGPOINT_CRYPT = &_bigpoint_crypt;
static
void
mcrypt_close(MCRYPT * mcrypt)
{
mcrypt_free(*mcrypt);
*mcrypt = NULL;
}
void *
bigpoint_crypt_createIv(struct BIGPOINT_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 BIGPOINT_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 *
bigpoint_crypt_encrypt(
struct BIGPOINT_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 = bigpoint_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 *
bigpoint_crypt_decrypt(
struct BIGPOINT_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: