File: cipher.cpp

package info (click to toggle)
ucommon 7.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,068 kB
  • sloc: cpp: 45,917; ansic: 714; sh: 226; makefile: 173
file content (204 lines) | stat: -rw-r--r-- 5,199 bytes parent folder | download | duplicates (3)
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
// Copyright (C) 2010-2014 David Sugar, Tycho Softworks.
// Copyroght (C) 2015-2020 Cherokees of Idaho.
//
// 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 uint8_t *_salt = NULL;
static unsigned _rounds = 1;

namespace ucommon {

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

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

void Cipher::Key::assign(const char *text, size_t size, const uint8_t *salt, unsigned rounds)
{
    if(!algotype || !hashtype)
        return;

    if(!size)
        size = strlen((const char *)text);

    if(!rounds)
        rounds = _rounds;

    if(!salt)
        salt = _salt;

    if(EVP_BytesToKey((const EVP_CIPHER*)algotype, (const EVP_MD*)hashtype, salt, (const uint8_t *)text, (int)size, rounds, keybuf, ivbuf) < (int)keysize)
        keysize = 0;
}

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

    // never use sha0...
    if(eq_case(digest, "sha") || eq_case(digest, "sha160"))
        digest = "sha1";

    hashtype = EVP_get_digestbyname(digest);
}

void Cipher::Key::set(const char *cipher)
{
    char algoname[64];

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

    if(fpart && fpart == lpart)
        String::set(fpart, sizeof(algoname), fpart + 1);

    algotype = EVP_get_cipherbyname(algoname);

    if(!algotype)
        return;

    keysize = EVP_CIPHER_key_length((const EVP_CIPHER*)algotype);
    blksize = EVP_CIPHER_block_size((const EVP_CIPHER*)algotype);
}


bool Cipher::has(const char *id)
{
    // make sure cipher-bitsize forms without -mode do not fail...
    char algoname[64];
    String::set(algoname, sizeof(algoname), id);
    char *fpart = strchr(algoname, '-');
    char *lpart = strrchr(algoname, '-');

    if(fpart && fpart == lpart)
        String::set(fpart, sizeof(algoname), fpart + 1);

    return (EVP_get_cipherbyname(algoname) != NULL);
}

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

void Cipher::release(void)
{
    keys.clear();
    if(context) {
        EVP_CIPHER_CTX_cleanup((EVP_CIPHER_CTX*)context);
        EVP_CIPHER_CTX_free((EVP_CIPHER_CTX*)context);
        context = NULL;
    }
}

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

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

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

    context = EVP_CIPHER_CTX_new();
    EVP_CIPHER_CTX_init((EVP_CIPHER_CTX *)context);
    EVP_CipherInit_ex((EVP_CIPHER_CTX *)context, (EVP_CIPHER *)keys.algotype, NULL, keys.keybuf, keys.ivbuf, (int)mode);
    EVP_CIPHER_CTX_set_padding((EVP_CIPHER_CTX *)context, 0);
}

size_t Cipher::put(const uint8_t *data, size_t size)
{
    int outlen;
    size_t count = 0;

    if(!bufaddr)
        return 0;

    if(size % keys.iosize())
        return 0;

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

    if(!EVP_CipherUpdate((EVP_CIPHER_CTX *)context, bufaddr + bufpos, &outlen, data, (int)size)) {
        release();
        return count;
    }
    bufpos += outlen;
    count += outlen;
    if(bufsize && bufpos >= bufsize) {
        push(bufaddr, bufsize);
        bufpos = 0;
    }
    return count;
}

size_t Cipher::pad(const uint8_t *data, size_t size)
{
    size_t padsize = 0;
    uint8_t padbuf[64];
    const uint8_t *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:
        padsize = size % keys.iosize();
        put(data, size - padsize);
        if(padsize) {
            memcpy(padbuf, data + size - padsize, padsize);
            memset(padbuf + padsize, (int)(keys.iosize() - padsize), keys.iosize() - padsize);
            size = (size - padsize) + keys.iosize();
        }
        else {
            size += keys.iosize();
            memset(padbuf, (int)keys.iosize(), keys.iosize());
        }

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

    flush();
    return size;
}

} // namespace ucommon