File: gm_crypt.c

package info (click to toggle)
mod-gearman 1.3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,936 kB
  • sloc: ansic: 12,250; sh: 1,463; perl: 464; makefile: 320
file content (121 lines) | stat: -rw-r--r-- 3,380 bytes parent folder | download | duplicates (2)
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
/******************************************************************************
 *
 * mod_gearman - distribute checks with gearman
 *
 * Copyright (c) 2010 Sven Nierlein - sven.nierlein@consol.de
 *
 * This file is part of mod_gearman.
 *
 *  mod_gearman is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  mod_gearman 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with mod_gearman.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <gm_crypt.h>
#include "common.h"

int encryption_initialized = 0;
unsigned char key[KEYLENGTH(KEYBITS)];


/* initialize encryption */
void mod_gm_aes_init(char * password) {

    /* pad key till keysize */
    int i;
    for (i = 0; i < 32; i++)
        key[i] = *password != 0 ? *password++ : 0;

    encryption_initialized = 1;
    return;
}


/* encrypt text with given key */
int mod_gm_aes_encrypt(unsigned char ** encrypted, char * text) {
    unsigned long rk[RKLENGTH(KEYBITS)];
    int nrounds;
    int i = 0;
    int k = 0;
    unsigned char *enc;
    int size;
    int totalsize;

    assert(encryption_initialized == 1);

    nrounds   = rijndaelSetupEncrypt(rk, key, KEYBITS);
    size      = strlen(text);
    totalsize = size + BLOCKSIZE-size%BLOCKSIZE;
    if(size%BLOCKSIZE == 0) {
        size += BLOCKSIZE;
    }
    enc       = (unsigned char *) malloc(sizeof(unsigned char)*totalsize);
    while(size > 0) {
        unsigned char plaintext[BLOCKSIZE];
        unsigned char ciphertext[BLOCKSIZE];
        int j;
        for (j = 0; j < BLOCKSIZE; j++) {
            int c = text[i];
            if(c == 0)
                break;
            plaintext[j] = c;
            i++;
        }

        for (; j < BLOCKSIZE; j++)
            plaintext[j] = '\x0';
        rijndaelEncrypt(rk, nrounds, plaintext, ciphertext);
        for (j = 0; j < BLOCKSIZE; j++)
            enc[k++] = ciphertext[j];
        size -=BLOCKSIZE;
    }

    *encrypted = enc;
    return totalsize;
}


/* decrypt text with given key */
void mod_gm_aes_decrypt(char ** text, unsigned char * encrypted, int size) {
    char decr[GM_BUFFERSIZE];
    unsigned long rk[RKLENGTH(KEYBITS)];
    int nrounds;
    int i = 0;

    assert(encryption_initialized == 1);
    nrounds = rijndaelSetupDecrypt(rk, key, KEYBITS);
    decr[0] = '\0';

    while(1) {
        unsigned char plaintext[BLOCKSIZE];
        unsigned char ciphertext[BLOCKSIZE];
        int j;
        for (j = 0; j < BLOCKSIZE; j++) {
            int c = encrypted[i];
            ciphertext[j] = c;
            i++;
        }
        rijndaelDecrypt(rk, nrounds, ciphertext, plaintext);
        strncat(decr, (char*)plaintext, BLOCKSIZE);
        size -= BLOCKSIZE;
        if(size < BLOCKSIZE)
            break;
    }

    strcpy(*text, decr);
    return;
}