File: cipher.cpp

package info (click to toggle)
ucommon 5.2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,156 kB
  • sloc: cpp: 41,584; sh: 11,440; ansic: 511; makefile: 210
file content (363 lines) | stat: -rw-r--r-- 8,503 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (C) 2010 David Sugar, Tycho Softworks.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.

#include "local.h"

static const unsigned char *_salt = NULL;
static unsigned _rounds = 1;

Cipher::Key::Key(const char *cipher)
{
    secure::init();
    set(cipher);
}

Cipher::Key::Key(const char *cipher, const char *digest)
{
    secure::init();
    set(cipher, digest);
}

Cipher::Key::Key(const char *cipher, const char *digest, const char *text, size_t size, const unsigned char *salt, unsigned count)
{
    secure::init();
    set(cipher, digest);
    assign(text, size, salt, count);
}

void Cipher::Key::assign(const char *text, size_t size, const unsigned char *salt, unsigned count)
{
    if(hashid == GCRY_MD_NONE || algoid == GCRY_CIPHER_NONE) {
        keysize = 0;
        return;
    }

    gcry_md_hd_t mdc;
    if(gcry_md_open(&mdc, hashid, 0) != 0) {
        clear();
        return;
    }

    size_t kpos = 0, ivpos = 0;
    size_t mdlen = gcry_md_get_algo_dlen(hashid);
    size_t tlen = strlen(text);

    char previous[MAX_DIGEST_HASHSIZE / 8];
    unsigned prior = 0;
    unsigned loop;

    if(!salt)
        salt = _salt;

    if(!count)
        count = _rounds;

    do {
        gcry_md_reset(mdc);

        if(prior++)
            gcry_md_write(mdc, previous, mdlen);

        gcry_md_write(mdc, (const char *)text, tlen);

        if(salt)
            gcry_md_write(mdc, (const char *)salt, 8);

        gcry_md_final(mdc);
        memcpy(previous, gcry_md_read(mdc, hashid), mdlen);

        for(loop = 1; loop < count; ++loop) {
            gcry_md_reset(mdc);
            gcry_md_write(mdc, previous, mdlen);
            gcry_md_final(mdc);
            memcpy(previous, gcry_md_read(mdc, hashid), mdlen);
        }

        size_t pos = 0;
        while(kpos < keysize && pos < mdlen)
            keybuf[kpos++] = previous[pos++];
        while(ivpos < blksize && pos < mdlen)
            ivbuf[ivpos++] = previous[pos++];
    } while(kpos < keysize || ivpos < blksize);
    gcry_md_close(mdc);
}

void Cipher::Key::assign(const char *text, size_t size)
{
    assign(text, size, _salt, _rounds);
}

Cipher::Key::Key()
{
    secure::init();
    clear();
}

Cipher::Key::~Key()
{
    clear();
}

void Cipher::Key::options(const unsigned char *salt, unsigned rounds)
{
    _salt = salt;
    _rounds = rounds;
}

void Cipher::Key::set(const char *cipher, const char *digest)
{
    set(cipher);

    if(case_eq(digest, "sha"))
        digest = "sha1";

    hashid = gcry_md_map_name(digest);
}

void Cipher::Key::set(const char *cipher)
{
    clear();

    char algoname[64];

    String::set(algoname, sizeof(algoname), cipher);
    char *fpart = strchr(algoname, '-');
    char *lpart = strrchr(algoname, '-');

    modeid = GCRY_CIPHER_MODE_CBC;

    if(lpart && lpart != fpart) {
        *(lpart++) = 0;
        if(case_eq(lpart, "cbc"))
            modeid = GCRY_CIPHER_MODE_CBC;
        else if(case_eq(lpart, "ecb"))
            modeid = GCRY_CIPHER_MODE_ECB;
        else if(case_eq(lpart, "cfb"))
            modeid = GCRY_CIPHER_MODE_CFB;
        else if(case_eq(lpart, "ofb"))
            modeid = GCRY_CIPHER_MODE_OFB;
        else
            modeid = GCRY_CIPHER_MODE_NONE;
    }

    algoid = gcry_cipher_map_name(algoname);

    if(algoid != GCRY_CIPHER_NONE) {
        (void)gcry_cipher_algo_info(algoid, GCRYCTL_GET_KEYLEN, NULL, &keysize);
        (void)gcry_cipher_algo_info(algoid, GCRYCTL_GET_BLKLEN, NULL, &blksize);
    }
}

void Cipher::Key::clear()
{
    algoid = GCRY_CIPHER_NONE;
    hashid = GCRY_MD_NONE;
    keysize = blksize = 0;
    zerofill(keybuf, sizeof(keybuf));
    zerofill(ivbuf, sizeof(ivbuf));
}

Cipher::Cipher(key_t key, mode_t mode, unsigned char *address, size_t size)
{
    bufaddr = NULL;
    bufsize = bufpos = 0;
    context = NULL;
    set(key, mode, address, size);
}

Cipher::Cipher()
{
    bufaddr = NULL;
    bufsize = bufpos = 0;
    context = NULL;
}

Cipher::~Cipher()
{
    flush();
    release();
}

void Cipher::push(unsigned char *address, size_t size)
{
}

void Cipher::release(void)
{
    keys.clear();
    if(context) {
        gcry_cipher_close((CIPHER_CTX)context);
        context = NULL;
    }
}

bool Cipher::is(const char *cipher)
{
    // eliminate issues with algo-size-mode formed algo strings...

    char algoname[64];

    String::set(algoname, sizeof(algoname), cipher);
    char *fpart = strchr(algoname, '-');
    char *lpart = strrchr(algoname, '-');
    if(lpart && lpart != fpart)
        *(lpart++) = 0;

    return gcry_cipher_map_name(algoname) != GCRY_CIPHER_NONE;
}

size_t Cipher::flush(void)
{
    size_t total = bufpos;

    if(bufpos && bufsize) {
        push(bufaddr, bufpos);
        bufpos = 0;
    }
    bufaddr = NULL;
    return total;
}

void Cipher::set(unsigned char *address, size_t size)
{
    flush();
    bufaddr = address;
    bufsize = size;
    bufpos = 0;
}

void Cipher::set(key_t key, mode_t mode, unsigned char *address, size_t size)
{
    release();

    bufsize = size;
    bufmode = mode;
    bufaddr = address;

    memcpy(&keys, key, sizeof(keys));
    if(!keys.keysize)
        return;

    gcry_cipher_open((CIPHER_CTX *)&context, keys.algoid, keys.modeid,0);
    gcry_cipher_setkey((CIPHER_CTX)context, keys.keybuf, keys.keysize);
    gcry_cipher_setiv((CIPHER_CTX)context, keys.ivbuf, keys.blksize);
}

size_t Cipher::puts(const char *text)
{
    char padbuf[64];
    if(!text || !bufaddr)
        return 0;

    size_t len = strlen(text) + 1;
    unsigned pad = len % keys.iosize();

    size_t count = put((const unsigned char *)text, len - pad);
    if(pad) {
        memcpy(padbuf, text + len - pad, pad);
        memset(padbuf + pad, 0, keys.iosize() - pad);
        count += put((const unsigned char *)padbuf, keys.iosize());
        zerofill(padbuf, sizeof(padbuf));
    }
    return flush();
}

size_t Cipher::put(const unsigned char *data, size_t size)
{
    gcry_error_t errcode;

    if(size % keys.iosize() || !bufaddr)
        return 0;

    size_t count = 0;

    while(bufsize && size + bufpos > bufsize) {
        size_t diff = bufsize - bufpos;
        count += put(data, diff);
        data += diff;
        size -= diff;
    }

    switch(bufmode) {
    case Cipher::ENCRYPT:
        errcode = gcry_cipher_encrypt((CIPHER_CTX)context, bufaddr + bufpos, size, data, size);
        break;
    case Cipher::DECRYPT:
        errcode = gcry_cipher_decrypt((CIPHER_CTX)context, bufaddr + bufpos, size, data, size);
    }

    count += size;
    if(!count) {
        release();
        return 0;
    }
    bufpos += size;
    if(bufsize && bufpos >= bufsize) {
        push(bufaddr, bufsize);
        bufpos = 0;
    }
    return count;
}

size_t Cipher::pad(const unsigned char *data, size_t size)
{
    size_t padsz = 0;
    unsigned char padbuf[64];
    const unsigned char *ep;

    if(!bufaddr)
        return 0;

    switch(bufmode) {
    case DECRYPT:
        if(size % keys.iosize())
            return 0;
        put(data, size);
        ep = data + size - 1;
        bufpos -= *ep;
        size -= *ep;
        break;
    case ENCRYPT:
        padsz = size % keys.iosize();
        put(data, size - padsz);
        if(padsz) {
            memcpy(padbuf, data + size - padsz, padsz);
            memset(padbuf + padsz, keys.iosize() - padsz, keys.iosize() - padsz);
            size = (size - padsz) + keys.iosize();
        }
        else {
            size += keys.iosize();
            memset(padbuf, keys.iosize(), keys.iosize());
        }

        put((const unsigned char *)padbuf, keys.iosize());
        zerofill(padbuf, sizeof(padbuf));
    }

    flush();
    return size;
}

size_t Cipher::process(unsigned char *buf, size_t len, bool flag)
{
    set(buf);
    if(flag)
        return pad(buf, len);
    else
        return put(buf, len);
}