~ubuntu-branches/ubuntu/quantal/remote-login-service/quantal

« back to all changes in this revision

Viewing changes to src/crypt.c

  • Committer: Package Import Robot
  • Author(s): Ted Gould
  • Date: 2012-09-20 23:55:05 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120920235505-ndvmwqqso7qrlqhp
Tags: 1.0.0-0ubuntu1
* New upstream release.
  * Support caching the recently used server
* debian/control: Add build dep on libgcrypt11-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3, as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include <glib.h>
 
19
 
 
20
#include <gcrypt.h>
 
21
#include <math.h>
 
22
 
 
23
#include "crypt.h"
 
24
 
 
25
static gcry_cipher_hd_t
 
26
setup_cipher (const gchar * password)
 
27
{
 
28
        gcry_error_t     gcryError;
 
29
        gcry_cipher_hd_t gcryHandle;
 
30
        
 
31
        const size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES);
 
32
        const size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES);
 
33
        
 
34
        // We are assuming keyLength and blkLength are the same, check it
 
35
        if (keyLength != blkLength)
 
36
                return NULL;
 
37
 
 
38
        char * aesSymKey = malloc(blkLength);
 
39
        const size_t passwordLength = strlen(password);
 
40
        strncpy(aesSymKey, password, blkLength);
 
41
        size_t i;
 
42
        for (i = passwordLength; i < blkLength; ++i)
 
43
                aesSymKey[i] = 0;
 
44
        
 
45
        gcryError = gcry_cipher_open(&gcryHandle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 0);
 
46
        if (gcryError) {
 
47
                g_warning("gcry_cipher_open failed: %s/%s\n", gcry_strsource(gcryError), gcry_strerror(gcryError));
 
48
                return NULL;
 
49
        }
 
50
 
 
51
        gcryError = gcry_cipher_setkey(gcryHandle, aesSymKey, keyLength);
 
52
        if (gcryError) {
 
53
                g_warning("gcry_cipher_setkey failed: %s/%s\n", gcry_strsource(gcryError), gcry_strerror(gcryError));
 
54
                gcry_cipher_close(gcryHandle);
 
55
                return NULL;
 
56
        }
 
57
 
 
58
        // Use the key as IV too
 
59
        gcryError = gcry_cipher_setiv(gcryHandle, aesSymKey, blkLength);
 
60
        if (gcryError) {
 
61
                g_warning("gcry_cipher_setiv failed: %s/%s\n", gcry_strsource(gcryError), gcry_strerror(gcryError));
 
62
                gcry_cipher_close(gcryHandle);
 
63
                return NULL;
 
64
        }
 
65
 
 
66
        return gcryHandle;
 
67
}
 
68
 
 
69
/**
 
70
 * do_aes_encrypt:
 
71
 * @origBuffer: text to encrypt. Needs to be null terminated
 
72
 * @password: password to use. Will be cut/padded with 0 if it exceeds/does not reach the needed length
 
73
 * @outBufferLength: (out) On success contains the length of the returned buffer
 
74
 *
 
75
 * Returns the AES encrypted version of the text. It is responsability of the caller to free it
 
76
 */
 
77
gchar *
 
78
do_aes_encrypt(const gchar *origBuffer, const gchar * password, size_t *outBufferLength)
 
79
{
 
80
        gcry_error_t     gcryError;
 
81
        gcry_cipher_hd_t gcryHandle;
 
82
        
 
83
        gcryHandle = setup_cipher (password);
 
84
        if (gcryHandle == NULL) {
 
85
                return NULL;
 
86
        }
 
87
        
 
88
        const size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES);
 
89
        const size_t origBufferLength = strlen(origBuffer);
 
90
        const size_t bufferLength = ceil((double)origBufferLength / blkLength) * blkLength;
 
91
        gchar *buffer = malloc(bufferLength);
 
92
        memcpy(buffer, origBuffer, origBufferLength);
 
93
        int i;
 
94
        for (i = origBufferLength; i < bufferLength; ++i)
 
95
                buffer[i] = 0;
 
96
        
 
97
        char * encBuffer = malloc(bufferLength);
 
98
        size_t lengthDone = 0;
 
99
        while (lengthDone < bufferLength) {
 
100
                gcryError = gcry_cipher_encrypt(gcryHandle, &encBuffer[lengthDone], blkLength, &buffer[lengthDone], blkLength);
 
101
                if (gcryError) {
 
102
                        g_warning("gcry_cipher_encrypt failed: %s/%s\n", gcry_strsource(gcryError), gcry_strerror(gcryError));
 
103
                        gcry_cipher_close(gcryHandle);
 
104
                        free(encBuffer);
 
105
                        return NULL;
 
106
                }
 
107
                lengthDone += blkLength;
 
108
        }
 
109
        
 
110
        gcry_cipher_close(gcryHandle);
 
111
        
 
112
        *outBufferLength = bufferLength;
 
113
        return encBuffer;
 
114
}
 
115
 
 
116
/**
 
117
 * do_aes_encrypt:
 
118
 * @encBuffer: encrypted data
 
119
 * @password: password to use. Will be cut/padded with 0 if it exceeds/does not reach the needed length
 
120
 * @encBufferLength: Length of encBuffer
 
121
 *
 
122
 * Returns the AES decrypted version of the data. It is null terminated. It is responsability of the caller to free it
 
123
 */
 
124
gchar *
 
125
do_aes_decrypt(const gchar *encBuffer, const gchar * password, const size_t encBufferLength)
 
126
{
 
127
        gcry_error_t     gcryError;
 
128
        gcry_cipher_hd_t gcryHandle;
 
129
        
 
130
        gcryHandle = setup_cipher (password);
 
131
        if (gcryHandle == NULL) {
 
132
                return NULL;
 
133
        }
 
134
        
 
135
        const size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);       
 
136
        const size_t bufferLength = encBufferLength;
 
137
        char * outBuffer = malloc(bufferLength);
 
138
        size_t lengthDone = 0;
 
139
        while (lengthDone < bufferLength) {
 
140
                gcryError = gcry_cipher_decrypt(gcryHandle, &outBuffer[lengthDone], 16, &encBuffer[lengthDone], 16);
 
141
                if (gcryError)
 
142
                {
 
143
                        g_warning("gcry_cipher_decrypt failed: %s/%s\n", gcry_strsource(gcryError), gcry_strerror(gcryError));
 
144
                        return NULL;
 
145
                }
 
146
                lengthDone += blkLength;
 
147
        }
 
148
        
 
149
        gcry_cipher_close(gcryHandle);
 
150
        char *result = g_strndup(outBuffer, bufferLength);
 
151
        free(outBuffer);
 
152
        return result;
 
153
}