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
;
152 gcrypt_alg
= GCRY_CIPHER_BLOWFISH
;
154 /* case ENCR_AES_CTR:
155 mode = GCRY_CIPHER_MODE_CTR; */
161 gcrypt_alg
= GCRY_CIPHER_AES128
;
164 gcrypt_alg
= GCRY_CIPHER_AES192
;
167 gcrypt_alg
= GCRY_CIPHER_AES256
;
173 /* case ENCR_CAMELLIA_CTR:
174 mode = GCRY_CIPHER_MODE_CTR; */
176 case ENCR_CAMELLIA_CBC
:
180 gcrypt_alg
= GCRY_CIPHER_CAMELLIA128
;
183 gcrypt_alg
= GCRY_CIPHER_CAMELLIA192
;
186 gcrypt_alg
= GCRY_CIPHER_CAMELLIA256
;
192 case ENCR_SERPENT_CBC
:
196 gcrypt_alg
= GCRY_CIPHER_SERPENT128
;
199 gcrypt_alg
= GCRY_CIPHER_SERPENT192
;
202 gcrypt_alg
= GCRY_CIPHER_SERPENT256
;
208 case ENCR_TWOFISH_CBC
:
212 gcrypt_alg
= GCRY_CIPHER_TWOFISH128
;
215 gcrypt_alg
= GCRY_CIPHER_TWOFISH
;
225 this = malloc_thing(private_gcrypt_crypter_t
);
227 this->alg
= gcrypt_alg
;
228 err
= gcry_cipher_open(&this->h
, gcrypt_alg
, mode
, 0);
231 DBG1("grcy_cipher_open(%N) failed: %s",
232 encryption_algorithm_names
, algo
, gpg_strerror(err
));
237 this->public.crypter_interface
.encrypt
= (void (*) (crypter_t
*, chunk_t
,chunk_t
, chunk_t
*))encrypt
;
238 this->public.crypter_interface
.decrypt
= (void (*) (crypter_t
*, chunk_t
, chunk_t
, chunk_t
*))decrypt
;
239 this->public.crypter_interface
.get_block_size
= (size_t (*) (crypter_t
*))get_block_size
;
240 this->public.crypter_interface
.get_key_size
= (size_t (*) (crypter_t
*))get_key_size
;
241 this->public.crypter_interface
.set_key
= (void (*) (crypter_t
*,chunk_t
))set_key
;
242 this->public.crypter_interface
.destroy
= (void (*) (crypter_t
*))destroy
;
244 return &this->public;