2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "gcrypt_crypter.h"
22 typedef struct private_gcrypt_crypter_t private_gcrypt_crypter_t
;
25 * Private data of gcrypt_crypter_t
27 struct private_gcrypt_crypter_t
{
30 * Public part of this class.
32 gcrypt_crypter_t
public;
35 * gcrypt cipher handle
40 * gcrypt algorithm identifier
46 * Implementation of crypter_t.decrypt.
48 static void decrypt(private_gcrypt_crypter_t
*this, chunk_t data
,
49 chunk_t iv
, chunk_t
*dst
)
51 gcry_cipher_setiv(this->h
, iv
.ptr
, iv
.len
);
55 *dst
= chunk_alloc(data
.len
);
56 gcry_cipher_decrypt(this->h
, dst
->ptr
, dst
->len
, data
.ptr
, data
.len
);
60 gcry_cipher_decrypt(this->h
, data
.ptr
, data
.len
, NULL
, 0);
65 * Implementation of crypter_t.encrypt.
67 static void encrypt(private_gcrypt_crypter_t
*this, chunk_t data
,
68 chunk_t iv
, chunk_t
*dst
)
70 gcry_cipher_setiv(this->h
, iv
.ptr
, iv
.len
);
74 *dst
= chunk_alloc(data
.len
);
75 gcry_cipher_encrypt(this->h
, dst
->ptr
, dst
->len
, data
.ptr
, data
.len
);
79 gcry_cipher_encrypt(this->h
, data
.ptr
, data
.len
, NULL
, 0);
84 * Implementation of crypter_t.get_block_size.
86 static size_t get_block_size(private_gcrypt_crypter_t
*this)
90 gcry_cipher_algo_info(this->alg
, GCRYCTL_GET_BLKLEN
, NULL
, &len
);
95 * Implementation of crypter_t.get_key_size.
97 static size_t get_key_size(private_gcrypt_crypter_t
*this)
101 gcry_cipher_algo_info(this->alg
, GCRYCTL_GET_KEYLEN
, NULL
, &len
);
106 * Implementation of crypter_t.set_key.
108 static void set_key(private_gcrypt_crypter_t
*this, chunk_t key
)
110 gcry_cipher_setkey(this->h
, key
.ptr
, key
.len
);
114 * Implementation of crypter_t.destroy.
116 static void destroy (private_gcrypt_crypter_t
*this)
118 gcry_cipher_close(this->h
);
123 * Described in header
125 gcrypt_crypter_t
*gcrypt_crypter_create(encryption_algorithm_t algo
,
128 private_gcrypt_crypter_t
*this;
130 int mode
= GCRY_CIPHER_MODE_CBC
;
136 gcrypt_alg
= GCRY_CIPHER_DES
;
139 gcrypt_alg
= GCRY_CIPHER_DES
;
140 mode
= GCRY_CIPHER_MODE_ECB
;
143 gcrypt_alg
= GCRY_CIPHER_3DES
;
146 gcrypt_alg
= GCRY_CIPHER_IDEA
;
149 gcrypt_alg
= GCRY_CIPHER_CAST5
;
153 { /* gcrypt currently supports 128 bit blowfish only */
156 gcrypt_alg
= GCRY_CIPHER_BLOWFISH
;
158 /* case ENCR_AES_CTR:
159 mode = GCRY_CIPHER_MODE_CTR; */
165 gcrypt_alg
= GCRY_CIPHER_AES128
;
168 gcrypt_alg
= GCRY_CIPHER_AES192
;
171 gcrypt_alg
= GCRY_CIPHER_AES256
;
177 /* case ENCR_CAMELLIA_CTR:
178 mode = GCRY_CIPHER_MODE_CTR; */
180 case ENCR_CAMELLIA_CBC
:
184 gcrypt_alg
= GCRY_CIPHER_CAMELLIA128
;
187 gcrypt_alg
= GCRY_CIPHER_CAMELLIA192
;
190 gcrypt_alg
= GCRY_CIPHER_CAMELLIA256
;
196 case ENCR_SERPENT_CBC
:
200 gcrypt_alg
= GCRY_CIPHER_SERPENT128
;
203 gcrypt_alg
= GCRY_CIPHER_SERPENT192
;
206 gcrypt_alg
= GCRY_CIPHER_SERPENT256
;
212 case ENCR_TWOFISH_CBC
:
216 gcrypt_alg
= GCRY_CIPHER_TWOFISH128
;
219 gcrypt_alg
= GCRY_CIPHER_TWOFISH
;
229 this = malloc_thing(private_gcrypt_crypter_t
);
231 this->alg
= gcrypt_alg
;
232 err
= gcry_cipher_open(&this->h
, gcrypt_alg
, mode
, 0);
235 DBG1("grcy_cipher_open(%N) failed: %s",
236 encryption_algorithm_names
, algo
, gpg_strerror(err
));
241 this->public.crypter_interface
.encrypt
= (void (*) (crypter_t
*, chunk_t
,chunk_t
, chunk_t
*))encrypt
;
242 this->public.crypter_interface
.decrypt
= (void (*) (crypter_t
*, chunk_t
, chunk_t
, chunk_t
*))decrypt
;
243 this->public.crypter_interface
.get_block_size
= (size_t (*) (crypter_t
*))get_block_size
;
244 this->public.crypter_interface
.get_key_size
= (size_t (*) (crypter_t
*))get_key_size
;
245 this->public.crypter_interface
.set_key
= (void (*) (crypter_t
*,chunk_t
))set_key
;
246 this->public.crypter_interface
.destroy
= (void (*) (crypter_t
*))destroy
;
248 return &this->public;